[SERVER-10030] Empty parent returned where projection key doesn't exist Created: 26/Jun/13  Updated: 10/Dec/14  Resolved: 13/Aug/13

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

Type: Bug Priority: Major - P3
Reporter: Matt Campbell Assignee: Stennie Steneker (Inactive)
Resolution: Done Votes: 0
Labels: None
Remaining Estimate: Not Specified
Time Spent: Not Specified
Original Estimate: Not Specified

Operating System: ALL
Steps To Reproduce:

// Returns immediate parent which exists
db.test.insert({'level1':{'level2':

{'key':'value'}

}})
db.test.find({},

{'level1.level2.key2':1,'_id':0}

)
Result: { "level1" : { "level2" : { } } }
Expected: Nothing

// Returns 2 parents up which exists
db.test.insert({'level1':{'key':'value'}})
db.test.find({},

{'level1.level2.key2':1,'_id':0}

)
Result: { "level1" : { } }
Expected: Nothing

It should be noted that if there was a projection for keys within the respective docs returned they would be showed. However, I'm not sure why an empty sub doc is returned when no other projections are made for it.

Is this an intended design or a bug?

Participants:

 Description   

When using dot notation to construct a projection on sub document keys, if the key specified in the project doesn't exist it returns the next ancestor which does exist, but as an empty document.



 Comments   
Comment by Stennie Steneker (Inactive) [ 13/Aug/13 ]

Hi Matt,

This is the expected outcome given your query and projection.

If you wanted to get nil results you can use $exists in the criteria so you only match documents where that key exists:

db.test.find({'level1.level2.key2': { $exists:true}}, {'level1.level2.key2':1,'_id':0})

Regards,
Stephen

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