Partial indexes in mongodb / mongoose

橙三吉。 提交于 2019-11-29 13:52:32
Angelos Veglektsis

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);
});

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

Now it's possible natively with Mongoose +4.6.1

Book.index({user: 1, author: 1, complete: 1}, {unique: true, partialFilterExpression: {complete: true}});

For Mongoid users:

index(
  { user_id: 1, author_id: 1, complete: 1 },
  background: true,
  partial_filter_expression:
    {
      complete: { :$eq => true }
    }
)

Couldn't find any docs, but this PR.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!