Let’s say I have the following collection with _id
and traits
.
[
{
_id: 1,
traits: {
Rarity: {
infoin: 15,
},
Type: {
iron: 3,
sliver: 5,
wood: 7,
},
},
},
{
_id: 2,
traits: {
Cloth: {
barron1: 11,
barron2: 12,
},
Hair: {
black: 6,
yellow: 9,
red: 8
}
},
},
...
]
As you can see keys of traits
are dynamic and the keys of sub-objects as well.
Here is the result I wanna get:
[
{
_id: 1,
traits: 15,
},
{
_id: 2,
traits: 23
}
]
Tip:
infocoin = iron + sliver + wood
barron1 + barron2 = black + yellow + red
2
Answers
$set
– SettraitObjs
array field by converting the object to array via$objectToArray
.$set
– SetfirstTraitValues
field by getting the value of first document fromtraitObjs
array, then converting from object to array via$objectToArray
.$project
– Decorate the output document. Settraits
field by convertingfirstTraitValues
array to number type with$reduce
and$sum
all thev
values.Sample Mongo Playground
Since all the values in the first key document and the second key document of
traits
are the same,Hence the above approach just sums up all the values in the first key document of
traits
.This answer is really the same as @yong-shun’s answer but it composes everthing into one
"$project"
. I don’t know if it would be more efficient or not.Try it on mongoplayground.net.