
OPERATIONAL DEFECT DATABASE
...

...
When using the $slice projection operator on the output field below, the input and txid fields are included in the projection. When using the $elemMatch projection operator on the output field, the txid and input fields are not included in the projection. I believe that the correct behavior is exhibited by the $elemMatch operator, and should be the same with $slice.
lineus@gmail.com commented on Mon, 16 Jul 2018 22:57:33 +0000: Thanks for the education I can't believe I missed that in the docs! nick.brewer commented on Mon, 16 Jul 2018 21:18:17 +0000: lineus@gmail.com, I believe this is a duplicate of an older ticket (SERVER-3378), which contains an explanation of the expected behavior. Our documentation has some additional information related the use of projection against arrays - as it mentions, $elemMatch is the appropriate tool for this use case. Regards, Nick
>db.tests.find().pretty() { "_id" : ObjectId("5b487b56f00b9013cff5a4dd"), "txid" : 0, "input" : [ { "_id" : ObjectId("5b487b56f00b9013cff5a4de"), "pid" : 0, "value" : 42 } ], "output" : [ { "_id" : ObjectId("5b487b56f00b9013cff5a4e2"), "value" : 123, "n" : 0 }, { "_id" : ObjectId("5b487b56f00b9013cff5a4e1"), "value" : 345, "n" : 1 }, { "_id" : ObjectId("5b487b56f00b9013cff5a4e0"), "value" : 567, "n" : 2 }, { "_id" : ObjectId("5b487b56f00b9013cff5a4df"), "value" : 789, "n" : 3 } ], "__v" : 0 } >var cond = {txid: 0} >var update = { $set: { ['output.0.value']: 777 } }; >var opts = { projection: { _id: 0, output: { $slice: [0,1] } }, returnNewDocument: true }; >db.tests.findOneAndUpdate(cond, update, opts); { "txid" : 0, "input" : [ { "_id" : ObjectId("5b487b56f00b9013cff5a4de"), "pid" : 0, "value" : 42 } ], "output" : [ { "_id" : ObjectId("5b487b56f00b9013cff5a4e2"), "value" : 777, "n" : 0 } ], "__v" : 0 } >var update = { $set: { ['output.0.value']: 123 } }; >var opts = { projection: { _id: 0, output: { $elemMatch: { n: 0 } } }, returnNewDocument: true }; >db.tests.findOneAndUpdate(cond, update, opts); { "output" : [ { "_id" : ObjectId("5b487b56f00b9013cff5a4e2"), "value" : 123, "n" : 0 } ] } >
MongoDB Integration
Learn more about where this data comes from
Bug Scrub Advisor
Streamline upgrades with automated vendor bug scrubs
BugZero Enterprise
Wish you caught this bug sooner? Get proactive today.