Discussion:
mongodb $slice array value repeat use skip limit not take effect?
陈龙
2014-04-24 01:55:21 UTC
Permalink
for example
document {'id':1,'array':[1,2,3,4,4,4,4]}
use db.test.find({'id':1},{'array':{'$slice':[-1,3]}}) ,but result array
[4],why?
--
You received this message because you are subscribed to the Google Groups "mongodb-user"
group.

For other MongoDB technical support options, see: http://www.mongodb.org/about/support/.
---
You received this message because you are subscribed to the Google Groups "mongodb-user" group.
To unsubscribe from this group and stop receiving emails from it, send an email to mongodb-user+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/***@public.gmane.org
To post to this group, send email to mongodb-user-/JYPxA39Uh5TLH3MbocFF+G/***@public.gmane.org
Visit this group at http://groups.google.com/group/mongodb-user.
To view this discussion on the web visit https://groups.google.com/d/msgid/mongodb-user/50cc0ab9-278c-41c3-b523-0e4b238f0b13%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
Stephen Steneker
2014-04-24 02:53:14 UTC
Permalink
Post by 陈龙
for example
document {'id':1,'array':[1,2,3,4,4,4,4]}
use db.test.find({'id':1},{'array':{'$slice':[-1,3]}}) ,but result array
[4],why?
Hi,

The negative slice is returning items with the skip counting from the end
of the array:
http://docs.mongodb.org/manual/reference/operator/projection/slice/

Your example is confusing since you have multiple values of "4" in your
array.

What you've asked for is effectively "skip to the item that is 1 position
from the end of the array, and return the next 3 array items including that
one".

To make it clearer try using unique values in your test document:
db.test.insert({id:1, array:[1,2,3,4,5,6,7]})


Now you will see that your slice with a skip of -1 and limit of 3 returns
the last element (as expected):
db.test.find({'id':1},{'array':{'$slice':[-1,3]}})
{
"_id" : ObjectId("535879ae930332c0db35e886"),
"id" : 1,
"array" : [
7
]
}

The limit of 3 isn't useful here as there are no elements following the
last one in the array.

For comparison, try taking a slice skipping to the 3rd-to-last element in
the array and limiting to the following 2 items:
db.test.find({'id':1},{'array':{'$slice':[-3,2]}})
{
"_id" : ObjectId("535879ae930332c0db35e886"),
"id" : 1,
"array" : [
5,
6
]
}

Regards,
Stephen
--
You received this message because you are subscribed to the Google Groups "mongodb-user"
group.

For other MongoDB technical support options, see: http://www.mongodb.org/about/support/.
---
You received this message because you are subscribed to the Google Groups "mongodb-user" group.
To unsubscribe from this group and stop receiving emails from it, send an email to mongodb-user+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/***@public.gmane.org
To post to this group, send email to mongodb-user-/JYPxA39Uh5TLH3MbocFF+G/***@public.gmane.org
Visit this group at http://groups.google.com/group/mongodb-user.
To view this discussion on the web visit https://groups.google.com/d/msgid/mongodb-user/6c4e8848-fe0a-4419-886c-dcbe2a50f677%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
陈龙
2014-04-24 09:23:40 UTC
Permalink
Thank you!
array elements not repeat is ok, but i test array repeat state, I did not
expect,why array element repeat is that

圚 2014幎4月24日星期四UTC+8䞊午10时53分14秒Stephen Steneker写道
Post by Stephen Steneker
Post by 陈龙
for example
document {'id':1,'array':[1,2,3,4,4,4,4]}
use db.test.find({'id':1},{'array':{'$slice':[-1,3]}}) ,but result array
[4],why?
Hi,
The negative slice is returning items with the skip counting from the end
http://docs.mongodb.org/manual/reference/operator/projection/slice/
Your example is confusing since you have multiple values of "4" in your
array.
What you've asked for is effectively "skip to the item that is 1 position
from the end of the array, and return the next 3 array items including that
one".
db.test.insert({id:1, array:[1,2,3,4,5,6,7]})
Now you will see that your slice with a skip of -1 and limit of 3 returns
db.test.find({'id':1},{'array':{'$slice':[-1,3]}})
{
"_id" : ObjectId("535879ae930332c0db35e886"),
"id" : 1,
"array" : [
7
]
}
The limit of 3 isn't useful here as there are no elements following the
last one in the array.
For comparison, try taking a slice skipping to the 3rd-to-last element in
db.test.find({'id':1},{'array':{'$slice':[-3,2]}})
{
"_id" : ObjectId("535879ae930332c0db35e886"),
"id" : 1,
"array" : [
5,
6
]
}
Regards,
Stephen
--
You received this message because you are subscribed to the Google Groups "mongodb-user"
group.

For other MongoDB technical support options, see: http://www.mongodb.org/about/support/.
---
You received this message because you are subscribed to the Google Groups "mongodb-user" group.
To unsubscribe from this group and stop receiving emails from it, send an email to mongodb-user+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/***@public.gmane.org
To post to this group, send email to mongodb-user-/JYPxA39Uh5TLH3MbocFF+G/***@public.gmane.org
Visit this group at http://groups.google.com/group/mongodb-user.
To view this discussion on the web visit https://groups.google.com/d/msgid/mongodb-user/3c05f8ff-7ce9-466a-a01b-375a7aad5e23%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
Loading...