I have collection with documents, for example:
[
{
'id':'1'
'some_field':'test',
'rates':[
{'user_id':'12','rate':'very_good'},
{'user_id':'13','rate':'very_good'}
{'user_id':'14','rate':'bad'},
{'user_id':'15','rate':'normal'}
]
}
]
And i have collection with values of rates in string:
[
{
"rate_name" : "bad",
"rate_value" : 1
},
{
"rate_name" : "normal",
"rate_value" : 2
},
{
"rate_name" : "very_good",
"rate_value" : 3
},
]
I need map data from first collection from array rates with value from second collection and group this values to new field.
For example:
[
{
'id':'1'
'some_field':'test',
'rates':[3,3,1,2]
]
}
]
How i can do this?
2
Answers
Here’s one way you could do it if
"$getField"
is available (MongoDB server version 5.0 and above). See below for another option.Try it on mongoplayground.net.
If
"$getField"
is unavailable, here’s another way you could do it.Try it on mongoplayground.net.
You can do a plain
$unwind
then$lookup
approach.Mongo Playground