[SERVER-30417] add expression to get value by keyname from object Created: 28/Jul/17 Updated: 30/Oct/23 Resolved: 22/Apr/21 |
|
| Status: | Closed |
| Project: | Core Server |
| Component/s: | Aggregation Framework |
| Affects Version/s: | None |
| Fix Version/s: | 5.0.0-rc0 |
| Type: | Improvement | Priority: | Major - P3 |
| Reporter: | Asya Kamsky | Assignee: | Alya Berciu |
| Resolution: | Fixed | Votes: | 14 |
| Labels: | asya, expression, needs-syntax, pm1457-nominee, qopt-team | ||
| Remaining Estimate: | Not Specified | ||
| Time Spent: | Not Specified | ||
| Original Estimate: | Not Specified | ||
| Issue Links: |
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| Backwards Compatibility: | Fully Compatible | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| Sprint: | Query 2020-08-10, Query 2020-08-24, Query Optimization 2021-04-05, Query Optimization 2021-04-19, Query Optimization 2021-05-03 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| Participants: | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| Description |
|
While it's possible via combined use of $objectToArray and $filter with $map, it would be nice to have a simple expression to get value of particular field of an object:
This will allow easily plucking out a value when the field name is stored in another part of the same document, or when it has some funny character that precludes it being used as subfield name (like leading "$") as in SERVER-30365 and Second argument could be optional and default to "$$ROOT". |
| Comments |
| Comment by Githook User [ 21/Apr/21 ] | ||||||||||||||
|
Author: {'name': 'Alya Berciu', 'email': 'alyacarina@gmail.com', 'username': 'alyacb'}Message: Co-authored-by: Katherine Wu <katherine.wu@mongodb.com> | ||||||||||||||
| Comment by Githook User [ 21/Apr/21 ] | ||||||||||||||
|
Author: {'name': 'Alya Berciu', 'email': 'alyacarina@gmail.com', 'username': 'alyacb'}Message: | ||||||||||||||
| Comment by Charlie Swanson [ 02/Sep/20 ] | ||||||||||||||
|
Pulling out of sprint since we are awaiting some decisions about whether this is within the scope of a broader project. | ||||||||||||||
| Comment by George Mihailov [ 16/Mar/20 ] | ||||||||||||||
|
I'm stumbling to this over and over. Ideally, it should support a nested path
in a perfect world, even support arrays
| ||||||||||||||
| Comment by Asya Kamsky [ 10/Jun/19 ] | ||||||||||||||
|
I opened | ||||||||||||||
| Comment by Jeffrey Mariconda [ 10/Dec/18 ] | ||||||||||||||
|
I proposed something similar at SERVER-38463, although I think that this syntax should probably be modified to be more consistent with the current $arrayElemAt syntax like the following:
| ||||||||||||||
| Comment by naresh buruzula [ 15/Nov/18 ] | ||||||||||||||
|
I am facing a issue which might be similar to this. "outputs" : {"1234" : "actual value"} I want to get the identifier (1234) of "abc" from "identifiers".
i tried something like below. But that did not work. // here "path" will be assigned with $outputs.1234 , "1234"]}}, Is there anyway to achieve this? Thank you. | ||||||||||||||
| Comment by Cade Embery [ 31/Oct/18 ] | ||||||||||||||
|
+1 for this, Our exact use case would be mainly for $arrayElemAt aswell.
Maybe something like $get, or $pick?
| ||||||||||||||
| Comment by Asya Kamsky [ 23/Oct/18 ] | ||||||||||||||
|
Here is another example where we have to use $let in aggregation expression just to pull out value of a specific known field because there is no way to add "get field v" to an expression so it has to be assigned to variable first:
With new expression it could be
| ||||||||||||||
| Comment by Asya Kamsky [ 28/Jul/17 ] | ||||||||||||||
|
I guess it wouldn't help SERVER-30365 which is looking to exclude the field, just one that's looking to include or access the field value. | ||||||||||||||
| Comment by Asya Kamsky [ 28/Jul/17 ] | ||||||||||||||
|
The aggregation workaround in 3.4.4 or later is:
|