Hi there, I’m seeing unexpected behavior with the $ne: null
expression for document fields inside of arrays. Here’s an example:
db.inventory.insertMany( [
{ item: "journal", instock: [ { warehouse: "A", qty: 5 }, { warehouse: "B", qty: 15 } ] },
{ item: "notebook", instock: [ { warehouse: "D", qty: 30 }, { warehouse: "E", qty: null } ] },
]);
// Query 1:
db.inventory.find({ 'instock.qty': { $gt: 10 } })
// Working as expected: returns both the "journal" and the "notebook"
// Query 2:
db.inventory.find({ 'instock.qty': { $ne: null } })
// Unexpected behavior: returns ONLY the "journal"
In the above example, Query 1 is working as expected. But I would expect Query 2 to return BOTH “journal” and “notebook”, because both have at least one document in the instock
array whose qty
field is non-null.
I’m on MongoDB 8.0.3 and am accessing it through Compass. This is the documentation I’m going off of: https://www.mongodb.com/docs/manual/tutorial/query-array-of-documents/#specify-a-query-condition-on-a-field-in-an-array-of-documents.
Can anyone help to explain to me why Query 2 does not seem to behave the same as Query 1?