[SERVER-42464] Find query fails with filter on field of a hash *in specified array element* Created: 27/Jul/19  Updated: 29/Jul/19  Resolved: 29/Jul/19

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

Type: Bug Priority: Major - P3
Reporter: Kelianne Mathiot Assignee: Danny Hatcher (Inactive)
Resolution: Duplicate Votes: 0
Labels: None
Remaining Estimate: Not Specified
Time Spent: Not Specified
Original Estimate: Not Specified
Environment:

Ubuntu 16.04.6 with MongoDB 4.0.9
MongoDB Atlas with MongoDB 4.0.10


Issue Links:
Duplicate
duplicates SERVER-36681 Change {$ne: null} semantics to be mo... Backlog
Operating System: ALL
Steps To Reproduce:

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!");

Participants:

 Description   

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.



 Comments   
Comment by Danny Hatcher (Inactive) [ 29/Jul/19 ]

Thanks for your report. {$ne:null} can be unintuitive in its current implementation. As you pointed out, {$ne:null} is not equivalent to {$exists:true}. We have SERVER-36681 open to track future improvements to this operator so I will close this ticket as a duplicate. I highly recommend you watch that ticket for any changes we do make.

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