如何通过mongoose在mongodb中设置复合主键 [英] How can I set composite primary key in mongodb through mongoose

查看:1382
本文介绍了如何通过mongoose在mongodb中设置复合主键的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想为mongodbmongoose中的集合中的两个字段设置主键.我知道将mongodb中的复合主键设置为

I want to set primary key for two fields in a collection in mongodb through mongoose. I know to set composite primary key in mongodb as

db.yourcollection.ensureIndex( { fieldname1: 1, fieldname2: 1 }, { unique: true } )

但是正在使用mongoose处理mongodb我不知道如何从mongoose

but am using mongoose to handle mongodb I don't know how to set composite primary key from mongoose

更新

我用过mySchema.index({ ColorScaleID: 1, UserName: 1}, { unique: true }); 看到我的代码

var mongoose = require('mongoose')

var uristring ='mongodb://localhost/fresh';
var mongoOptions = { db: { safe: true } };

// Connect to Database
mongoose.connect(uristring, mongoOptions, function (err, res) {
    if (err) {
        console.log ('ERROR connecting to: remote' + uristring + '. ' + err);
    } else {
        console.log ('Successfully connected to: remote' + uristring);
    }
});
var mySchema = mongoose.Schema({
        ColorScaleID:String,
        UserName:String,
        Range1:Number,
    })
mySchema.index({ ColorScaleID: 1, UserName: 1}, { unique: true });
var freshtime= mongoose.model("FreshTimeColorScaleInfo",mySchema)
var myVar = new freshtime({
        ColorScaleID:'red',
        UserName:'tab',
        Range1:10
    })
myVar.save()
mongoose.connection.close();

当我第一次执行此代码时,我在mongodb的新数据库中看到一行{"_id":...,ColorScaleID:'red',UserName:'tab',Range1:10 }.当我第二次执行相同的代码时,我看到两条相同的行.

When I execute this code for first time I see a line {"_id":...,ColorScaleID:'red',UserName:'tab',Range1:10 } in mongodb's fresh database. When I execute the same code for second time I see two same lines.

{"_id":...,ColorScaleID:'red',UserName:'tab',Range1:10 }
{"_id":...,ColorScaleID:'red',UserName:'tab',Range1:10 }

如果composite primary key工作正常,则不允许我第二次插入相同的数据.有什么问题吗?

If composite primary key worked then it shouldn't allow me to insert same data for second time. what would be the problem?

推荐答案

定义架构的方式是正确的,并且可以使用.您可能会遇到的情况是,数据库已经创建,并且即使该集合可能为空,该集合也可能已经存在.猫鼬不会适应指数.

The way that you have defined your schema is correct and will work. What you are probably experiencing is that the database has already been created and that collection probably already exists even though it might be empty. Mongoose won't retro fit the index.

作为实验,将数据库设置为不存在的数据库.例如:

As an experiment, set your database to a DB that does not exist. e.g.:

var uristring ='mongodb://localhost/randomname';

,然后尝试在此数据库上运行这两行,看看是否仍然可以插入这两个文档.

and then try running those two lines against this database and see if you can still insert those two documents.

然后比较每个集合中"system.indexes"集合的内容.您应该看到randomname db的复合索引设置正确.

Then compare the contents of the "system.indexes" collection in each of those collections. You should see that the randomname db has the composite index correctly set.

这篇关于如何通过mongoose在mongodb中设置复合主键的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆