[SERVER-15430] $exist: false check failed for nested document Created: 27/Sep/14  Updated: 02/Dec/14  Resolved: 02/Dec/14

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

Type: Bug Priority: Major - P3
Reporter: Senz Assignee: Unassigned
Resolution: Done Votes: 0
Labels: None
Remaining Estimate: Not Specified
Time Spent: Not Specified
Original Estimate: Not Specified

Operating System: ALL
Steps To Reproduce:

Document:

db.testStat.insert({_id: ObjectId("5426a05d9fc673d74e4ed001"), msn: []});
db.testStat.update({_id: ObjectId("5426a05d9fc673d74e4ed001")}, {$push: { msn:
{$each: [
    {
        "mid": ObjectId("5426a05d9fc673d74e4ed101"),
        "tasks": [
            {"tid": ObjectId("5426a05d9fc673d74e4ed301"), "cptd": 1411817713000},
            {"tid": ObjectId("5426a05d9fc673d74e4ed302")},
            {"tid": ObjectId("5426a05d9fc673d74e4ed303"), "cptd": 1411817713000},
            {"tid": ObjectId("5426a05d9fc673d74e4ed304")}
        ]
    },
    {
        "mid": ObjectId("5426a05d9fc673d74e4ed102"),
        "tasks": [
            {"tid": ObjectId("5426a05d9fc673d74e4ed305"), "cptd": 1411817713000},
            {"tid": ObjectId("5426a05d9fc673d74e4ed306")},
            {"tid": ObjectId("5426a05d9fc673d74e4ed307"), "cptd": 1411817713000},
            {"tid": ObjectId("5426a05d9fc673d74e4ed308")}
        ],
        "cptd": 1411817713000
    }
 
]}
}});

Query:

db.testStat.find({"msn.cptd": {$exists: false}})

Workaround:

db.testStat.find({"msn.cptd": {$gt: 0}})

Participants:

 Description   

> db.version()
2.6.4

$exist: false check for nested document field is worked inproper (according to documentation).
Was anticipating document that does not contain this fiend in embedded document.
$exist: true returns expected result.



 Comments   
Comment by Ramon Fernandez Marina [ 02/Dec/14 ]

Thanks for your report @senz. This is the expected behavior, as per the $exists documentation:

If <boolean> is false, the query returns only the documents that do not contain the field.

In this case your document does contain a "msn.cptd" field (the first element in the "msn" array doesn't contain "cptd", but the second element in "msn" does), so it is not returned in your query.

If you want to find documents where at least one element of the "msn" array does not contain a "cptd" field you may want to try $elemMatch:

db.testStat.find({msn: {$elemMatch: {cptd : {$exists : false}}}})

Regards,
Ramón.

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