在Mongoose/MongoDB中创建多字段索引 [英] Creating Multifield Indexes in Mongoose / MongoDB

查看:184
本文介绍了在Mongoose/MongoDB中创建多字段索引的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在寻找关于如何在Mongoosejs中创建多字段索引的文档,但无济于事.特别是我有两个需要索引的字段并且是唯一的.将两个字段索引在一起的Mongoose模式示例是什么?

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屋!

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