skip to Main Content

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


  1. Chosen as BEST ANSWER

    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

    _dbContext?.Documents?.FindAsync(model => model.Id == id && model.IsDeleted == false)
    

    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 index

    db.Documents.createIndex(
     { "IsDeleted" : 1 },
     { partialFilterExpression: { IsDeleted: false } } 
    );
    

  2. Partial filter expression doesn’t support $ne. Use $eq, like this:

    db.Documents.createIndex(
       { "IsDeleted" : 1 },
       { partialFilterExpression: { IsDeleted: false } } 
    );
    

    Check the supported list of expressions here.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search