[SERVER-32417] Add syntax to express $lookup-$unwind with a single aggregation stage Created: 20/Dec/17 Updated: 06/Dec/22 Resolved: 05/Jan/18 |
|
| Status: | Closed |
| Project: | Core Server |
| Component/s: | Aggregation Framework |
| Affects Version/s: | None |
| Fix Version/s: | None |
| Type: | New Feature | Priority: | Major - P3 |
| Reporter: | Andrew Harris | Assignee: | Backlog - Query Team (Inactive) |
| Resolution: | Duplicate | Votes: | 0 |
| Labels: | None | ||
| Remaining Estimate: | Not Specified | ||
| Time Spent: | Not Specified | ||
| Original Estimate: | Not Specified | ||
| Issue Links: |
|
||||||||||||
| Assigned Teams: |
Query
|
||||||||||||
| Participants: | |||||||||||||
| Description |
|
When performing a $lookup to pull in data from single document in another collection the result is persisted as an array (a single element array). A subsequent $unwind is often necessary to make working with this array easier. It would be good if $lookup had a switch that allowed it to automatically do this unwind if a single result is detected. |
| Comments |
| Comment by Asya Kamsky [ 28/Dec/17 ] |
this means that you would need to always check whether the result is a singleton non-array or an array of multiple elements - that seems much more user-unfriendly. aharris I'm inclined to close this as WAD or duplicate of $lookupOne request unless you have some additional clarification of this use case. |
| Comment by Asya Kamsky [ 21/Dec/17 ] |
|
When you do a $lookup and then $unwind, it already only executes a single stage (see explain). Is your request for syntactic sugar to avoid writing two stages? In other words, doing {$lookup:{ }},{$unwind:<as-field>} already executes as a single $lookup stage. |
| Comment by Charlie Swanson [ 20/Dec/17 ] |
|
Can you explain what the differences are then? I don't see any differences aside from syntax, which we'd have to workshop a bit either way. |
| Comment by Andrew Harris [ 20/Dec/17 ] |
|
It is not quite a duplicate of 22384 as I understand it but yes essentially $lookupOne looks like it is meant to offer what I'm after. |
| Comment by Charlie Swanson [ 20/Dec/17 ] |
|
If I'm understanding that correctly, it sounds like this is a duplicate of SERVER-22384. Would a $lookupOne or $lookup: {placeInArray: false} work for this use case? Such a switch would likely error if it got more than one result from a lookup, unless I'm missing something. |
| Comment by Andrew Harris [ 20/Dec/17 ] |
|
This is why I suggested a switch/parameter in the $lookup so that whether the single element array is unwound or not can be based on the application use case. If it helps the usual use case we have is a $lookup to a document using the _id of the remote document. It will return only a single result in $lookup but as a single element array. A parameter like "collapseArray:true" perhaps would be useful in this case. Does that make it clearer? Obviously, if you are expected 1 or more documents to be returned by the $lookup then this collapsing would not be done/possible. |
| Comment by Charlie Swanson [ 20/Dec/17 ] |
|
aharris this looks similar to SERVER-22384. Is there a reason you might want it to preserve arrays for some documents, but take out the array if there's only one? It seems more common to only ever expect one or 0 results. If you ever expect more than one, isn't it strictly easier if it's presented as an array, so all later stages/code don't have to conditionalize on the structure of the document? |