[SERVER-3378] $slice projection returns all fields Created: 06/Jul/11  Updated: 16/Jul/18  Resolved: 02/Sep/11

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

Type: Bug Priority: Minor - P4
Reporter: Kyle Banker Assignee: Mathias Stearn
Resolution: Done Votes: 0
Labels: None
Remaining Estimate: Not Specified
Time Spent: Not Specified
Original Estimate: Not Specified

Issue Links:
Duplicate
is duplicated by SERVER-2259 Limiting data by $slice returns adjac... Closed
is duplicated by SERVER-36112 disparate behavior of $slice and $ele... Closed
Operating System: ALL
Participants:

 Description   

db.foo.save(

{a: 1, b: [1, 2, 3]}

)

db.foo.find({}, {b: {$slice: [1, 2]}})

{ "_id" : ObjectId("4e146daea715701bb7716a85"), "a" : 1, "b" : [ 2, 3 ] }

Why does this return the 'a' field too?



 Comments   
Comment by wang yuanlong [ 14/Oct/11 ]

should it be {comments:{$only:1,$slice:10}} sometimes we just need the array . Or if used $slice then the field will be return.

Comment by Mathias Stearn [ 02/Sep/11 ]

Currently $slice does not effect whether the projection does inclusion or exclusion. Otherwise one of the following two projections would be invalid {a:1, b:{$slice:0}} or {a:0, b:{$slice:0}}. This is similar to our handling of {_id:0} so that we can allow {_id:0, a:1} but we still can't allow

{a:1, b:0}

because that doesn't make sense.

Also consider the common case of wanting to get a full blog post with just the first 10 comments. Right now it is just {comments:{$slice:10}}, but we wouldn't easily be able to get the rest of the blog post if we used your semantics. With the current semantics it is easy to get what you want by doing {_id:1, b:{$slice:10}}. The only thing you can't do with current semantics. is get only the slice of the array and not _id.

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