猫鼬:如何定义一个唯一的字段组合? [英] Mongoose: how to define a combination of fields to be unique?
本文介绍了猫鼬:如何定义一个唯一的字段组合?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
如果我有这样的架构:
var person = new Schema({
firstName: String,
lastName: String,
});
我想确保只有一个具有相同的firstName和lastName的文档.
I'd like to ensure that there is only one document with the same firstName and lastName.
我该怎么做?
推荐答案
您可以在架构上使用index
调用来定义唯一的复合索引:
You can define a unique compound index using an index
call on your schema:
person.index({ firstName: 1, lastName: 1}, { unique: true });
这篇关于猫鼬:如何定义一个唯一的字段组合?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文