[SERVER-5947] Add ability to project key names as values and values as key names Created: 28/May/12 Updated: 06/Dec/22 Resolved: 29/Mar/16 |
|
| Status: | Closed |
| Project: | Core Server |
| Component/s: | Aggregation Framework |
| Affects Version/s: | None |
| Fix Version/s: | None |
| Type: | New Feature | Priority: | Major - P3 |
| Reporter: | Asya Kamsky | Assignee: | Backlog - Query Team (Inactive) |
| Resolution: | Duplicate | Votes: | 28 |
| Labels: | aggregation, expression, usability | ||
| Remaining Estimate: | Not Specified | ||
| Time Spent: | Not Specified | ||
| Original Estimate: | Not Specified | ||
| Issue Links: |
|
||||||||||||||||||||||||||||||||||||
| Assigned Teams: |
Query
|
||||||||||||||||||||||||||||||||||||
| Participants: | |||||||||||||||||||||||||||||||||||||
| Description |
|
Aggregation framework could be a powerful way to transform one shape of document schema into another if values could be turned into keys and vice versa. Example schema: { userId: "xxx", attributeName: "yyy", attributeValue: "zzz"}could be projected into { userId: "xxx", "yyy": "zzz"}for a subset of attribute names where now we can do simple $match for our criteria. Conversely, ability to turn keys into values would allow aggregation based on existence of certain keys. |
| Comments |
| Comment by Charlie Swanson [ 29/Mar/16 ] | |||||||||||||||||
|
I'm closing this ticket, since I believe it can be accomplished by two new expressions proposed in For example, you could do
If you have a use case that you do not believe to be satisfied by these expressions, please post here and describe your use case, and we'll consider re-opening or filing a new ticket. | |||||||||||||||||
| Comment by John A. De Goes [ 07/Nov/15 ] | |||||||||||||||||
|
These are indeed primitive operations: the ability to peek into the key names, and create new key / values dynamically. A $keys operator could pull out the keys into an array. A $values operator could pull out the values into an array. And a corresponding $makeObject operator could create a document from an array of key / value pairs (or from two arrays). These give you the ability to solve all problems listed in this ticket. | |||||||||||||||||
| Comment by Amit Gosavi [ 07/Oct/15 ] | |||||||||||||||||
|
Is there any activity on this JIRA? I am facing similar issue and projecting key makes sense in my scenario as well. | |||||||||||||||||
| Comment by Billy Tetrud [ 06/Aug/14 ] | |||||||||||||||||
|
I found another case where I'd want to use this: http://stackoverflow.com/questions/11189243/unwind-an-object-in-aggregation-framework . I believe this feature would allow me to basically unwind an object by turning an object into an array then using normal unwind. | |||||||||||||||||
| Comment by Billy Tetrud [ 25/Jul/14 ] | |||||||||||||||||
|
Yeah, I don't know the full set of default values. The values are arbitrary - user defined. | |||||||||||||||||
| Comment by Asya Kamsky [ 25/Jul/14 ] | |||||||||||||||||
|
Actually, never mind - I was thinking you might be able to use $redact but as long as you're storing the default field you want to keep as the key rather than the value, you can't do this, unless you know the full set of possible default values. | |||||||||||||||||
| Comment by Asya Kamsky [ 25/Jul/14 ] | |||||||||||||||||
|
You may not need new functionality to be able to do what you ask for, as long as you know in advance all possible values of "default" - do you? | |||||||||||||||||
| Comment by Billy Tetrud [ 25/Jul/14 ] | |||||||||||||||||
|
I'd also like to see this. Here's my use case: http://stackoverflow.com/questions/24950215 | |||||||||||||||||
| Comment by David Murphy [ 09/Oct/13 ] | |||||||||||||||||
|
I think the perfect use case is system.profiler, where you cant use mapReduce but you might need to pull off something like:
As a summary of query's run similar to pt-query-digest or mysqlsla | |||||||||||||||||
| Comment by Asya Kamsky [ 02/Feb/13 ] | |||||||||||||||||
|
Encountered another use case:
If you need to sort by fieldN across differently named fields, or get a sum of values stored in more than one field in a single query, it would be simpler if this feature was available. |