Uploaded image for project: 'Core Server'
  1. Core Server
  2. SERVER-42464

Find query fails with filter on field of a hash *in specified array element*

    • Type: Icon: Bug Bug
    • Resolution: Duplicate
    • Priority: Icon: Major - P3 Major - P3
    • None
    • Affects Version/s: None
    • Component/s: Querying
    • None
    • Environment:
      Ubuntu 16.04.6 with MongoDB 4.0.9
      MongoDB Atlas with MongoDB 4.0.10
    • ALL
    • Hide
      db.bughunt.drop;
      db.bughunt.insertOne({
       prices : [ {
       max : 250,
       min : 200
       } ]
      });
      
      var doc;
      
      ////////////////////////////////////////////////////////////////////
      // Documents that we can find by using `prices.0.min` in filter;
      // No bug noted here, so see "NO BUG" in print output
      ////////////////////////////////////////////////////////////////////
      
      doc = db.bughunt.findOne(\{'prices.0.min' : 200 });
      if (doc) {
       print ("1) NO BUG: doc found");
       print (tojson(doc));
      } else {
       print ("1) BUGBUG: doc NOT found!");
      }
      
      doc = db.bughunt.findOne(\{'prices.0.min' : { $exists : true } });
      if (doc) {
       print ("2) NO BUG: doc found");
       print (tojson(doc));
      } else {
       print ("2) BUGBUG: doc NOT found!");
      }
      
      doc = db.bughunt.findOne(\{ 'prices.0.min' : { $gt : 100 } });
      if (doc) {
       print ("3) NO BUG: doc found");
       print (tojson(doc));
      } else {
       print ("3) BUGBUG: doc NOT found!");
      }
      
      doc = db.bughunt.findOne(\{ 'prices.0.min' : { $exists : true, $ne : 123 } });
      if (doc) {
       print ("4) NO BUG: doc found");
       print (tojson(doc));
      } else {
       print ("4) BUGBUG: doc NOT found!");
      }
      
      ////////////////////////////////////////////////////////////////////
      // Documents that we CANNOT find using `prices.0.min` in filter;
      // BUG noted here, so see "BUGBUG" in print output
      ////////////////////////////////////////////////////////////////////
      
      doc = db.bughunt.findOne(\{ 'prices.0.min' : { $exists : true, $ne : null } });
      if (doc) {
       print ("5) NO BUG: doc found");
       print (tojson(doc));
      } else {
       print ("5) BUGBUG: doc NOT found!");
      }
      
      doc = db.bughunt.findOne(\{ 'prices.0.min' : { $ne : null } });
      if (doc) {
       print ("6) NO BUG: doc found");
       print (tojson(doc));
      } else {
       print ("6) BUGBUG: doc NOT found!");
      }
      
      print ("Bughunt finished!");
      
      Show
      db.bughunt.drop; db.bughunt.insertOne({ prices : [ { max : 250, min : 200 } ] }); var doc; //////////////////////////////////////////////////////////////////// // Documents that we can find by using `prices.0.min` in filter; // No bug noted here, so see "NO BUG" in print output //////////////////////////////////////////////////////////////////// doc = db.bughunt.findOne(\{'prices.0.min' : 200 }); if (doc) { print ("1) NO BUG: doc found"); print (tojson(doc)); } else { print ("1) BUGBUG: doc NOT found!"); } doc = db.bughunt.findOne(\{'prices.0.min' : { $exists : true } }); if (doc) { print ("2) NO BUG: doc found"); print (tojson(doc)); } else { print ("2) BUGBUG: doc NOT found!"); } doc = db.bughunt.findOne(\{ 'prices.0.min' : { $gt : 100 } }); if (doc) { print ("3) NO BUG: doc found"); print (tojson(doc)); } else { print ("3) BUGBUG: doc NOT found!"); } doc = db.bughunt.findOne(\{ 'prices.0.min' : { $exists : true, $ne : 123 } }); if (doc) { print ("4) NO BUG: doc found"); print (tojson(doc)); } else { print ("4) BUGBUG: doc NOT found!"); } //////////////////////////////////////////////////////////////////// // Documents that we CANNOT find using `prices.0.min` in filter; // BUG noted here, so see "BUGBUG" in print output //////////////////////////////////////////////////////////////////// doc = db.bughunt.findOne(\{ 'prices.0.min' : { $exists : true, $ne : null } }); if (doc) { print ("5) NO BUG: doc found"); print (tojson(doc)); } else { print ("5) BUGBUG: doc NOT found!"); } doc = db.bughunt.findOne(\{ 'prices.0.min' : { $ne : null } }); if (doc) { print ("6) NO BUG: doc found"); print (tojson(doc)); } else { print ("6) BUGBUG: doc NOT found!"); } print ("Bughunt finished!");

      If you have in your collection a document:

      {
          _id: ...,
          prices : [ {
              max : 250,
              min : 200
         } ]
      }
      

      You can successfully query it by using the following filters:

      1)

      {'prices.0.min' : 200 }

      2)

      {'prices.0.min' : { $exists : true } }

      3)

      { 'prices.0.min' : { $gt : 100 } }

      4)

      { 'prices.0.min' : { $exists : true, $ne : 123 } }

      BUT you CANNOT do it using the below filters:

      1)

      { 'prices.0.min' : { $exists : true, $ne : null } }

      2)

      { 'prices.0.min' : { $ne : null } }

      The problem noted is with only `$ne : null` in filter.

            Assignee:
            daniel.hatcher@mongodb.com Danny Hatcher (Inactive)
            Reporter:
            office@namesmaster.com Kelianne Mathiot
            Votes:
            0 Vote for this issue
            Watchers:
            3 Start watching this issue

              Created:
              Updated:
              Resolved: