[SERVER-36112] disparate behavior of $slice and $elemMatch projection operators Created: 13/Jul/18  Updated: 14/Aug/18  Resolved: 16/Jul/18

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

Type: Bug Priority: Minor - P4
Reporter: Kevin Huff Assignee: Nick Brewer
Resolution: Duplicate Votes: 0
Labels: None
Remaining Estimate: Not Specified
Time Spent: Not Specified
Original Estimate: Not Specified

Issue Links:
Duplicate
duplicates SERVER-3378 $slice projection returns all fields Closed
Operating System: ALL
Steps To Reproduce:

 >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
                }
        ]
}
>

Participants:

 Description   

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.



 Comments   
Comment by Kevin Huff [ 16/Jul/18 ]

Thanks for the education I can't believe I missed that in the docs!

Comment by Nick Brewer [ 16/Jul/18 ]

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

 

Generated at Thu Feb 08 04:42:04 UTC 2024 using Jira 9.7.1#970001-sha1:2222b88b221c4928ef0de3161136cc90c8356a66.