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

Query for null on a field in embedded documents using array index does not work as expected

    • Type: Icon: Bug Bug
    • Resolution: Duplicate
    • Priority: Icon: Major - P3 Major - P3
    • None
    • Affects Version/s: 3.6.10
    • Component/s: Querying
    • Labels:
      None
    • ALL
    • Hide
          db.inventory.insertMany([
            { item: "journal", instock: [ { warehouse: "A", qty: 5 } , { warehouse: "C", qty: 15 } ] },
            { item: "notebook", instock: [ { warehouse: "C", qty: 5 } ] },
            { item: "paper", instock: [ { warehouse: null, qty: 60 } , { warehouse: "B", qty: 15 } ] },
            { item: "planner", instock: [ { warehouse: "A", qty: 40 } , { warehouse: "B", qty: 5 } ] },
            { item: "postcard", instock: [ {                qty: 15 } , { warehouse: "C", qty: 35 } ] }
         ]);
      

       

      • Note that third document has warehouse:null in the first embedded doc and the fifth document has the first embedded doc with no warehouse field.

       

      db.inventory.find( { 'instock.0.warehouse': null } )

      Returns all five documents where I would expect it to return only third and fifth documents.

       

       

      Show
      db.inventory.insertMany([ { item: "journal" , instock: [ { warehouse: "A" , qty: 5 } , { warehouse: "C" , qty: 15 } ] }, { item: "notebook" , instock: [ { warehouse: "C" , qty: 5 } ] },   { item: "paper" , instock: [ { warehouse: null , qty: 60 } , { warehouse: "B" , qty: 15 } ] }, { item: "planner" , instock: [ { warehouse: "A" , qty: 40 } , { warehouse: "B" , qty: 5 } ] }, { item: "postcard" , instock: [ {                qty: 15 } , { warehouse: "C" , qty: 35 } ] } ]);   Note that third document has warehouse:null in the first embedded doc and the fifth document has the first embedded doc with no warehouse field.   db.inventory.find( { 'instock.0.warehouse': null } ) Returns all five documents where I would expect it to return only third and fifth documents.    

      Matching for null value for a field in embedded documents using array index does not return only the documents that have null value on the field of interest or those that don't have said field present as one would expect... or am I doing something stupid?

            Assignee:
            eric.sedor@mongodb.com Eric Sedor
            Reporter:
            danirod Dani
            Votes:
            0 Vote for this issue
            Watchers:
            6 Start watching this issue

              Created:
              Updated:
              Resolved: