mongo $ slice查询反向索引超出范围 [英] mongo $slice query reverse index out of range
问题描述
以下在mongo中的查询的行为很奇怪:
The following query in mongo, behaves strange :
db.items.findOne({},{ "List": { "$slice": [ skip, 3 ] }})
首先: 它不返回仅使用["_id","List"]键的对象,而是返回完整的对象.
First: Instead of returning one object with ["_id","List"] keys only, it returns a full object.
第二:
如果skip
为负数且|skip|
高于list.length,则它返回前三个元素,就像skip==0
Second:
if skip
is negative and |skip|
is higher than list.length then it returns the first three elements as though skip==0
我期望:
{
"_id" : ObjectId("542babf265f5de9a0d5c2928"),
"List" : [
1,
2,
3,
4,
5
]
"other" : "not_important"
}
查询:
db.items.findOne({},{ "List": { "$slice": [-10, 3 ] }})
获得:
{
"_id" : ObjectId("542babf265f5de9a0d5c2928"),
"List" : []
}
相反,我得到:
{
"_id" : ObjectId("542babf265f5de9a0d5c2928"),
"List" : [
1,
2,
3
]
"other" : "not_important"
}
为什么?
我使用mongoDB 2.4.10
I use mongoDB 2.4.10
推荐答案
第二个:如果skip为负且| skip |高于list.length,则返回前三个元素,就好像skip == 0
是的.这就是mongodb内部使用的javascript Array.prototype.slice()
方法的工作方式.
Yes. That is how the javascript Array.prototype.slice()
method works, which is internally used by mongodb.
根据ECMAScript®语言规范 ,
如果relativeStart为负,则令k为max((len + relativeStart),0); 否则让k为min(relativeStart,len).
If relativeStart is negative, let k be max((len + relativeStart),0); else let k be min(relativeStart, len).
对于您的情况relativeStart is -10
,
k = max((-10+5),0), k = 0;
(其中5
是数组的长度).
In your case relativeStart is -10
,
k = max((-10+5),0), k = 0;
(where, 5
is the length of your array).
因此k
或skip
将始终为0
.
首先:它不返回仅使用["_id","List"]键的对象,而是返回完整的对象.
是的,投影运算符以这种方式工作.除非在projection参数中明确指定了inclusion
或exclusion
,否则将使用诸如$slice
,$elemmatch
之类的投影运算符来检索整个文档.
Yes, the projection operator works that way. Unless a inclusion
or exclusion
is explicitly specified in the projection parameter, the whole document is retrieved with the projection operators such as $slice
,$elemmatch
being applied.
db.items.findOne({},{"_id":1,"List": { "$slice": [-10, 3 ] }})
将返回:
{ "_id" : ObjectId("542babf265f5de9a0d5c2928"), "List" : [ 1, 2, 3 ] }
findOne()
方法的第二个参数是not only for simple projection
用途,仅当field
名称中的任何一个名称具有0
或projection operator
,则为applied
和projected
.
The second parameter to the findOne()
method is not only for simple projection
purpose, fields are not projected, only if any one of the field
names have a value of 0
or 1
against them. If not the whole document is returned. If any field has a projection operator
to be applied, it would be applied
and projected
.
只要涉及$slice
运算符,投射机制似乎就会以以下方式发生.
The projection mechanism seems to happen in the below manner, whenever the $slice
operator is involved.
- 默认情况下,所有字段都将包含在内以进行投影.
- 默认情况下,始终显示其值基于投影运算符
$slice
,如果为真而导出的所有字段,而与以下内容无关.
- By default all the fields would be included for projection.
- By Default all the fields whose values are derived based on the projection operator,
$slice
, if truthy, are always displayed, irrespective of the below.
- projection参数中指定的字段列表按其指定顺序累积.
- 仅遇到第一个值为'0'或'1'的字段: 如果 字段的值为'0'-然后将其排除,其余所有字段 字段被标记为包括在内. 如果字段具有"1"-则将其包括在内,其余所有字段 被标记为排除.
- 对于所有后续字段,根据以下内容排除或包含它们 他们的价值观.
- The list of fields specified in the projection parameter are accumulated in their specified order.
- For only the first field encountered with value '0' or '1': If the field has a value '0' - then it is excluded, and all the remaining fields are marked to be included. If a field has '1' - then it is included, and all the remaining fields are marked to be excluded.
- For all the subsequent fields, they are excluded or included based on their values.
这篇关于mongo $ slice查询反向索引超出范围的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!