Adding index with array index (A.0.B) changes query result

XMLWordPrintableJSON

    • Type: Bug
    • Resolution: Works as Designed
    • Priority: Minor - P4
    • None
    • Affects Version/s: 8.0.19
    • Component/s: None
    • None
    • ALL
    • Hide
      db.version() // 8.0.19
      db.test.drop();
      
      db.test.insertMany([
        { "A": [ { "B": "value" } ] },
        { "A": [ { "B": "value" } ] }
      ]);
      db.test.find({ "A.0.B": { $ne: null } }); // no result
      
      db.test.createIndex({"A.0.B": 1});
      db.test.find({ "A.0.B": { $ne: null } }); // both documents
      
      db.test.insertOne({ "A": [] });
      db.test.find({ "A.0.B": { $ne: null } }); // only the document just inserted
      
      db.test.deleteOne({ "A": [] }); // delete the document just inserted
      db.test.find({ "A.0.B": { $ne: null } }); // no result
      Show
      db.version() // 8.0.19 db.test.drop(); db.test.insertMany([ { "A" : [ { "B" : "value" } ] }, { "A" : [ { "B" : "value" } ] } ]); db.test.find({ "A.0.B" : { $ne: null } }); // no result db.test.createIndex({ "A.0.B" : 1}); db.test.find({ "A.0.B" : { $ne: null } }); // both documents db.test.insertOne({ "A" : [] }); db.test.find({ "A.0.B" : { $ne: null } }); // only the document just inserted db.test.deleteOne({ "A" : [] }); // delete the document just inserted db.test.find({ "A.0.B" : { $ne: null } }); // no result
    • None
    • None
    • None
    • None
    • None
    • None
    • None

      Hi,

      I'm trying to create an index to quickly check whether an array is empty or not.

      I was expecting that the filter { "A.0.B":

      { $ne: null }

      } would do what I want and return all documents where A is an array with at least one element where B is not null, but it doesn't.

      Weirdly, adding an index on that field changed the filtering behaviour to the one I was expecting but then inserting a document where A is en empty array changed the behaviour again!

      I'm not sure what's the intended behaviour for

      { $ne: null }

      in this case, but definitely adding an index shouldn't make the behaviour change.

            Assignee:
            Kenan Ali
            Reporter:
            Nicola Pirritano
            Votes:
            0 Vote for this issue
            Watchers:
            3 Start watching this issue

              Created:
              Updated:
              Resolved: