[SERVER-81614] Projection form of $slice only works with find, not with $project Created: 02/Oct/23  Updated: 27/Oct/23  Resolved: 10/Oct/23

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

Type: Bug Priority: Major - P3
Reporter: Robert Stam Assignee: David Storch
Resolution: Works as Designed Votes: 0
Labels: None
Remaining Estimate: Not Specified
Time Spent: Not Specified
Original Estimate: Not Specified

Issue Links:
Related
related to CSHARP-4804 Slice projection must be rendered dif... Closed
Assigned Teams:
Query Execution
Operating System: ALL
Steps To Reproduce:

Enterprise test> db.test.find()
[ { _id: 1, a: [ 1, 2, 3, 4, 5 ] } ]
Enterprise test> db.test.find({}, { a : { $slice : [0, 3] } })
[ { _id: 1, a: [ 1, 2, 3 ] } ]
Enterprise test> db.test.aggregate([{ $project : { a : { $slice : [0, 5 ] } } }])
MongoServerError: Failed to optimize pipeline :: caused by :: First argument to $slice must be an array, but is of type: int
Enterprise test> 

Participants:

 Description   

The projection form of $slice:

https://www.mongodb.com/docs/manual/reference/operator/projection/slice/

only works with find, not with $project.

I understand there is another form of $slice:

https://www.mongodb.com/docs/manual/reference/operator/aggregation/slice/

But the issue here is why the first form doesn't work with $project.



 Comments   
Comment by David Storch [ 10/Oct/23 ]

This is intentional. It's an artifact of how MQL evolved over time. Originally, find projection and aggregation projection were entirely separate. The aggregation version which could be placed within a $project stage was richer because it supported all aggregation expressions in addition to basic field inclusion and exclusion. The find version, in contrast, did not support arbitrary expressions, but instead had three special language features of its own:

These three find-specific projection features are documented together here: https://www.mongodb.com/docs/manual/reference/operator/projection/.

In a previous version (MongoDB 4.4, if I'm not mistaken), we added support for the full expression language inside find command projections in an effort to unify find projection and aggregation projection. However, we retained find's $slice, positional ($), and $elemMatch projection for backwards compatibility. These three operators are special in terms of their syntax and behaviors as well as their underlying implementation, so we have no plans to make them available in the context of an aggregation $project stage. While the find-specific projection operators are not formally deprecated, the hope is that application use cases can always be satisfied using the aggregation language. I'd argue that new uses of the find-specific operators are discouraged.

While the documentation could arguably be improved (cc sarah.olson@mongodb.com), the "disambiguation" section in the find $slice documentation does try to differentiate this version from the aggregate version:

For aggregation operator, see $slice aggregation operator instead.

I'm closing this ticket as Works as Designed.

Comment by Robert Stam [ 03/Oct/23 ]

As background, we encountered this issue because we have an API in the C# driver to build projections, and the API assumes that any projection that works in find would also work in $project. That's always been true before this, and the server documentation is silent on whether the projection from of $slice works with $project or not.

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