Mongoose:如何定义字段的组合是唯一的? [英] Mongoose: how to define a combination of fields to be unique?
本文介绍了Mongoose:如何定义字段的组合是唯一的?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
如果我有这样的架构:
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.
我怎样才能做到这一点?
How can I accomplish this?
推荐答案
您可以使用对架构的 index
调用来定义唯一的复合索引:
You can define a unique compound index using an index
call on your schema:
person.index({ firstName: 1, lastName: 1}, { unique: true });
这篇关于Mongoose:如何定义字段的组合是唯一的?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文