I was hoping for some advice in terms of the creating of a partial index on a boolean value IsDeleted: false
.
Our existing .net service, using the MongoDb.Driver
makes the call _dbContext?.Documents?.FindAsync(model => model.Id == id && !model.IsDeleted)
the equivalent which is..
db.Documents.find({
"UserId": NumberLong(1234),
"IsDeleted": { $ne: true }
}
as example.
The issue is that trying to create the partial index
db.Documents.createIndex(
{ "IsDeleted" : 1 },
{ partialFilterExpression: { IsDeleted: { $ne: true } } }
);
results in the error unsupported expression in partial index: $notn IsDeleted $eq truen
I need to create an index on this value. What is the best alternative to the partial index?
2
Answers
So just for clarity, and the subsequent answer. Partial indexes are not supported for the expression
{ IsDeleted: { $ne: true } }
but is supported for{ IsDeleted: false }
So updating the C# code to
as opposed to using
!model.IsDeleted
solves the issue as it translates in to{ IsDeleted: false }
for the mongo query enabling the use of a partial indexPartial filter expression doesn’t support
$ne
. Use$eq
, like this:Check the supported list of expressions here.