在 Mongoose/MongoDB 中创建多字段索引 [英] Creating Multifield Indexes in Mongoose / MongoDB
本文介绍了在 Mongoose/MongoDB 中创建多字段索引的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在尝试查找有关如何在 Mongoosejs 中创建多字段索引的文档,但无济于事.特别是我有两个需要索引和唯一的字段.将两个字段一起索引的示例猫鼬模式是什么?
I'm trying to find documentation, to no avail, on how to create multi-field indexes in Mongoosejs. In particular I have two fields that need to be indexed and unique. What is an example mongoose schema that indexes two fields together?
推荐答案
您在 Schema
对象上调用 index
方法来执行此操作,如图所示此处.对于您的情况,它类似于:
You call the index
method on your Schema
object to do that as shown here. For your case it would be something like:
mySchema.index({field1: 1, field2: 1}, {unique: true});
这篇关于在 Mongoose/MongoDB 中创建多字段索引的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文