Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to create a partial index with $exists: false condition?

I want to create this partial index:

db.mycollection.createIndex( 
  {"firstname": 1, "lastname": 1}, 
  { partialFilterExpression: { "status": {$exists: false}, "quantity": { $lt: -1 } } } ); 

but i receive this error:

unsupported expression in partial index: $not\n status exists\n"

How can I do ?

like image 508
zeus Avatar asked Jun 13 '20 22:06

zeus


People also ask

Can MongoDB use part of a compound index?

MongoDB can use the intersection of indexes to fulfill queries. For queries that specify compound query conditions, if one index can fulfill a part of a query condition, and another index can fulfill another part of the query condition, then MongoDB can use the intersection of the two indexes to fulfill the query.

How do you create an index in PyMongo?

Create an index for a MongoDB collection, and specify the order. When you use PyMongo to create an index, you can pair the index name with either a 1 or a -1 integer value to explicitly set the sort order to ascending or descending, respectively.


Video Answer


2 Answers

Unfortunately, you don't.

That feature request has been around for quite a while: https://jira.mongodb.org/browse/SERVER-17853

Hopefully they get to it soon, that would be quite useful.

like image 133
Joe Avatar answered Oct 19 '22 18:10

Joe


If the status field doesn't have null values, you can use

{ partialFilterExpression: { "status": null, "quantity": { $lt: -1 } } } ); 
like image 40
Denis Konovalchik Avatar answered Oct 19 '22 17:10

Denis Konovalchik