[SERVER-2259] Limiting data by $slice returns adjacent items in a dict. Created: 20/Dec/10  Updated: 10/Dec/14  Resolved: 11/Nov/13

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

Type: Bug Priority: Minor - P4
Reporter: Heewa Barfchin Assignee: Unassigned
Resolution: Duplicate Votes: 3
Labels: query_triage
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
Participants:

 Description   

When asking for just an array inside a dict, only that array is returned (good so far). However, when you ask for a slice of that array, other items in that dict are also returned.

> db.a.save({_id:1, vals: {first:[1,2,3,], second:[1,2,3]}})
> db.a.find()
{ "_id" : 1, "vals" :

{ "first" : [ 1, 2, 3 ], "second" : [ 1, 2, 3 ] }

}
> db.a.find({},{"vals.first":{$slice:2}})
{ "_id" : 1, "vals" :

{ "first" : [ 1, 2 ], "second" : [ 1, 2, 3 ] }

}

//I would have expected this to work... might call that a bug.

> db.a.find({},{"vals.first":1, "vals.first":{$slice:2}})
{ "_id" : 1, "vals" :

{ "first" : [ 1, 2 ], "second" : [ 1, 2, 3 ] }

}

> db.a.find({},{"vals":0, "vals.first":{$slice:2}})
{ "_id" : 1, "vals" :

{ "first" : [ 1, 2 ] }

}

(thanks skot on the #mongodb IRC channel for the example)



 Comments   
Comment by David Storch [ 11/Nov/13 ]

I believe this is a duplicate of "$slice projection returns all fields", which was resolved as Works as Designed. The behavior in 2.5.3 appears to be the same as it was when this issue was initially reported.

Comment by David King [ 27/Apr/11 ]

Similar issue:

I want to specify multiple $slice actions on an indexed nested array, eg:

> db.a.save({ _id:1, vals:

{ first:[1,2,3,], second:[1,2,3], third:[1,2,3,4,5,6] }

});
> db.a.find({}, { vals: 0, 'vals.first': {$slice: 2} });
{ "_id" : 1, "vals" :

{ "first" : [ 1, 2 ] }

}

Works as expected, however:

> db.a.find({}, { vals: 0, 'vals.first': {$slice: 2}, 'vals.second': 1 })
uncaught exception: error {
"$err" : "You cannot currently mix including and excluding fields. Contact us if this is an issue.",
"code" : 10053
}

Or

> db.a.find({}, { vals: 0, 'vals.first': {$slice: 2}, 'vals.second': {$slice: 2} })
uncaught exception: error {
"$err" : "You cannot currently mix including and excluding fields. Contact us if this is an issue.",
"code" : 10053
}

Comment by Heewa Barfchin [ 20/Dec/10 ]

Also, it's not exactly a workaround to explicitly turn off the rest of that containing dict, because everything else in the record is still returned. So, if you had:

{'a': ...., 'b':

{'c': [1, 2, 3], 'd': [4, 5, 6]}

, 'e': ....}

And you asked for {'b': 0, 'b.c': {$slice: 2}}, you'd still get:

{'a': ...., 'b':

{'c': [1, 2]}

, 'e': ....}

Comment by Scott Hernandez (Inactive) [ 20/Dec/10 ]

Just to be clear, this was expected to work... by not returning second

> db.a.find({},{"vals.first":{$slice:2}})
{ "_id" : 1, "vals" :

{ "first" : [ 1, 2 ], "second" : [ 1, 2, 3 ] }

}

Generated at Thu Feb 08 02:59:25 UTC 2024 using Jira 9.7.1#970001-sha1:2222b88b221c4928ef0de3161136cc90c8356a66.