[SERVER-37341] get value from document using expression Created: 27/Sep/18  Updated: 04/Oct/18  Resolved: 04/Oct/18

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

Type: Improvement Priority: Major - P3
Reporter: yassine Karim [X] Assignee: Nick Brewer
Resolution: Duplicate Votes: 0
Labels: None
Remaining Estimate: Not Specified
Time Spent: Not Specified
Original Estimate: Not Specified

Issue Links:
Duplicate
duplicates SERVER-30417 add expression to get value by keynam... Closed
Participants:

 Description   

can we hav an aggregation operator like "$arrayElemAt" 
but instead of working on array the input is a document

{ $elemAt: [ <document>, <key> ] }

The <document> expression can be any valid expression as long as it resolves to a document.

The <key> expression can be any valid expression as long as it resolves to a String.

 

Behavio

Example     ->     Results

{ $elemAt: [ \{k1:"v1",k2:"v2"}

, "k1" ] }   -> "v1"

{ $elemAt: [ \{k1:"v1",k2:"v2"}

, "k3" ] }   -> null 



 Comments   
Comment by Nick Brewer [ 04/Oct/18 ]

yassineKarim I believe what you're describing is similar to the functionality described here: SERVER-30417

You can vote for that ticket, and follow along with it for updates.

-Nick

Comment by yassine Karim [X] [ 04/Oct/18 ]

nick.brewer the second parameter of arrayElemAt is an expression which resolve to an integer

currently there is no way in mongo to access a sub Document using an expression as key 
as an example 

inpput :

{ filed1:\{k1:v1,k2:v2}

,

field2:[k1,k2,k3]

}

in our aggregation pipeline we are intersted in unwinding on field 2 and getting the associated value on field1
output :

{ filed1:v1, field2:k1 }

,

{ filed1:v2, field2:k2 }

,

{ field2:k3 }

 

our workaround is storing in an array field1 and using the unwind index to get the elements

 

Yassine

Comment by Nick Brewer [ 01/Oct/18 ]

yassineKarim What you're describing does not appear to replicate the functionality of arrayElemAt, which uses an integer to return a value at a specific position. By comparison, what you're suggesting appears to use two equality statements.

It would be useful to see what a sample document would look like in the example you've provided. Can you provide some more details on the specific task you're trying to accomplish?

-Nick

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