[SERVER-24052] Add an option to $unwind to output only unique values Created: 04/May/16  Updated: 06/Dec/22  Resolved: 17/Jan/18

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

Type: Improvement Priority: Minor - P4
Reporter: Nic Cottrell (Personal) Assignee: Backlog - Query Team (Inactive)
Resolution: Won't Fix Votes: 1
Labels: None
Remaining Estimate: Not Specified
Time Spent: Not Specified
Original Estimate: Not Specified

Assigned Teams:
Query
Backwards Compatibility: Fully Compatible
Participants:

 Description   

Currently an extra $group with $addToSet step is required which is both unfriendly for developers, but I imagine is much less performant.



 Comments   
Comment by Asya Kamsky [ 17/Jan/18 ]

This should be accomplished using $set expressions before $unwind.

Comment by Asya Kamsky [ 09/May/16 ]

Our goal is to reduce the number of stages by the aggregation pipeline doing internal optimizations to combine stages whenever possible.

The problem with this option as proposed is when there are two "terms" that are the same, if the other fields in the document being unwound that aren't the same, which ones should be kept? When you perform $group you can specify if you want to keep the $first or $max or $sum of the other field(s), etc.

Comment by Nic Cottrell (Personal) [ 09/May/16 ]

Ok, cool! Still two steps though....

Comment by Asya Kamsky [ 09/May/16 ]

You can achieve what you want via:

{$project:{term:{$setUnion:["$indices.lower"]}}},{$unwind:"$term"}

Comment by Nic Cottrell (Personal) [ 08/May/16 ]

I have documents like this:

  {_id: ..., 
 "text": "The cat sat on the mat",
 "indices": [
     {term: "The",
      lower: "the",
      pos:0 },
     ...
    {term: "the",
     lower: "the",
     pos: 15}
   ]
 }
  

and I want to $unwind indices.lower

  {$project: {"term": "$indices.lower"},
  {"$unwind": "$term"}
   ...
  

right now I have to use a $group to get unique lower case terms, but would definitely be good to $unwind unique..

Comment by Asya Kamsky [ 07/May/16 ]

Could you clarify what you mean by unique values?

Is this for case where some of array elements have duplicates? Note that such duplicates can already be eliminated with one of the $set expression in $project (before $unwind).

Generated at Thu Feb 08 04:05:15 UTC 2024 using Jira 9.7.1#970001-sha1:2222b88b221c4928ef0de3161136cc90c8356a66.