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

查看:199
本文介绍了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.

部分索引非常有用,我想在我的项目中使用它们。是否可以将它们与mongoose一起使用?

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天全站免登陆