Jui*_*icy 7 javascript mongoose mongodb node.js
我正在使用以下Mongoose模型:
var Test = db.model('Test', db.Schema({
        one: { type: String, required: true },
        two: { type: String, required: true }
    },
    { strict: false })
);
它需要两个字段,one并且two,和strict:false因为可以有其他的领域有不确定的名字.
我想组合的one和two是唯一的.这意味着可能有多个文档具有相同one或相同two,但它们都不应具有相同one 和 two组合.
这可以用Mongoose实现吗?
chr*_*dam 15
您可以对复合索引强制执行唯一约束,并且可以使用index()模式方法在Mongoose中执行此操作,模式方法在模式级别定义索引:
var testSchema = db.Schema({
    "one": { "type": String, "required": true },
    "two": { "type": String, "required": true }
}, { "strict": false });
testSchema.index({ "one": 1, "two": 1}, { "unique": true });
var Test = db.model("Test", testSchema );
| 归档时间: | 
 | 
| 查看次数: | 4017 次 | 
| 最近记录: |