mongodb/mongoose 中的部分索引 [英] Partial indexes in mongodb / mongoose

查看:30
本文介绍了mongodb/mongoose 中的部分索引的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在稀疏索引文档中,我发现了关于 mongodb 3.2 部分索引的注释

In the sparse index documentation I found note about mongodb 3.2 partial indexes

在 3.2 版更改:从 MongoDB 3.2 开始,MongoDB 提供了创建部分索引的选项.部分索引提供了一个超集稀疏索引的功能.如果您使用的是 MongoDB 3.2 或之后,部分索引应该优先于稀疏索引.

Changed in version 3.2: Starting in MongoDB 3.2, MongoDB provides the option to create partial indexes. Partial indexes offer a superset of the functionality of sparse indexes. If you are using MongoDB 3.2 or later, partial indexes should be preferred over sparse indexes.

部分索引非常有用且我想在我的项目中使用它们.可以和猫鼬一起使用吗?

Partial indexes are very helpfull and I want to use them in my project. Is it possible use them with mongoose?

推荐答案

在目前的 Mongoose 4.3.7 版本中,您无法在方案中定义部分索引,但您仍然可以使用 MongoDB 3.2 的部分索引.

In the current Mongoose version 4.3.7 you cannot define partial indexes in the scheme, but you can still use Partial Indexes of MongoDB 3.2.

您只需使用本机驱动程序创建索引即可.

You just have to create the indexes using the native driver.

// ScheduleModel is a Mongoose Model
ScheduleModel.collection.createIndex({"type" : 1 } , {background:true , partialFilterExpression : { type :"g" }} , function(err , result){
     console.log(err , result);
});

之后,每个与 partialFilterExpression 匹配的查询都会被索引.

After that, every query that matches the partialFilterExpression will be indexed.

这篇关于mongodb/mongoose 中的部分索引的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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