[SERVER-55739] Incorrect filter result when using a null match inside an indexed array field Created: 02/Apr/21  Updated: 02/Apr/21  Resolved: 02/Apr/21

Status: Closed
Project: Core Server
Component/s: None
Affects Version/s: 4.4.4
Fix Version/s: None

Type: Bug Priority: Major - P3
Reporter: Giacomo Gregoletto Assignee: Eric Sedor
Resolution: Duplicate Votes: 0
Labels: None
Remaining Estimate: Not Specified
Time Spent: Not Specified
Original Estimate: Not Specified

Issue Links:
Depends
depends on SERVER-27442 Positional path component in match la... Backlog
Operating System: ALL
Steps To Reproduce:

Create the following document inside a random collection:

 

{"_id":{"$oid":"606717762ce596a43dde9c7b"},"array":[{"value":"MyValue"}]}

 

Run this query:

{ _id: ObjectId('606717762ce596a43dde9c7b'), "array.0.value": null }

The document will be returned by the database.

The next query works as expected (no match):

{ _id: ObjectId('606717762ce596a43dde9c7b'), "array.0.value": { $type: "null" } }

 

Participants:

 Description   

MongoDB v4.4.4 (Atlas).

 

The database will match more documents than expected when using a null match against an array's item.

 

Tested with Compass, Robo3T, Mongo shell, and Node.js driver.



 Comments   
Comment by Giacomo Gregoletto [ 02/Apr/21 ]

Ok, really interesting to hear this. Thank you for the fast response.

Comment by Eric Sedor [ 02/Apr/21 ]

Hi giacomo.gregoletto@evologi.it;

Thanks for writing. This unexpected behavior is the result of SERVER-27442, which discusses the known ambiguity of MQL for querying array elements vs. nested documents with keys that are also numbers.

In short, the document matches "array.0.value":null because:
1) "array.0.value": null logically includes documents where array.0.value does not exist
2 Even though the 0th value of array isn't null, the array does not contain a document with key "0".

But $type avoids this ambiguity because it explicitly filters on the BSON type for existing fields, as opposed to the "is null or doesn't exist" criteria of just querying for being equal to null.

I'm going to close this ticket as a duplicate of SERVER-27442. Can you comment there if you have any questions about the issue?

Gratefully,
Eric

Generated at Thu Feb 08 05:37:19 UTC 2024 using Jira 9.7.1#970001-sha1:2222b88b221c4928ef0de3161136cc90c8356a66.