-
Type:
Bug
-
Resolution: Works as Designed
-
Priority:
Major - P3
-
None
-
Affects Version/s: None
-
Component/s: None
-
None
-
None
-
None
-
None
-
None
-
None
-
None
-
None
Problem Description
Consider the below Mongodb Query.
{{ db.groups.aggregate([{ $match: { _id: { $in: [220]
}, active: true }
},
{ $project: { myArr: { $slice: ["$arr", -1, 0]
}, name: 1 }
}]).pretty()}}
When I execute this query I encounter error:
{{2020-12-05T19:16:43.002-0500 E QUERY [js] uncaught exception: Error: command failed: { "operationTime" : Timestamp(1607213793, 1), "ok" : 0, "errmsg" : "Third argument to $slice must be positive: 0", "code" : 28729, "codeName" : "Location28729", "$clusterTime" : { "clusterTime" : Timestamp(1607213793, 1), "signature" : { "hash" : BinData(0,"AAAAAAAAAAAAAAAAAAAAAAAAAAA="), "keyId" : NumberLong(0)
}
}
} : aggregate failed :}}
As per the $slice documentation, https://docs.mongodb.com/manual/reference/operator/aggregation/slice/ the third argument ('n') to the $slice aggregate operator cannot be negative if the second argument 'position' is specified. I calculate the third argument value to 0 which means, I expect 0 array elements as the result. However, I encounter errmsg" : "Third argument to $slice must be positive: 0".
Please suggest if my understanding is incorrect as the query doesn't execute as I expect.
Steps to Reproduce
{{ db.groups.aggregate([{ $match: { _id: { $in: [220]
}, active: true }
},
{ $project: { myArr: { $slice: ["$arr", -1, 0]
}, name: 1 }
}]).pretty()}}
Expected Results
Expecting myArr should be empty array.
Actual Results
"errmsg" : "Third argument to $slice must be positive: 0",