[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: |
|
||||||||
| Assigned Teams: |
Query Execution
|
||||||||
| Operating System: | ALL | ||||||||
| Steps To Reproduce: |
|
||||||||
| 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:
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. |