Using aggregation to sorting in complex conditional
I create a topic about using $match which was solved, but now I'm stuck in
a more complex example. For example, consider the struct of my Json:
{
user: 'bruno',
answers: [
{id: 0, value: 3.5},
{id: 1, value: "hello"}
]
}
I would like to add or order, apply some transformations on the values
contained in 'value' only the identifier 'id' equal to 0, for example a
sort method.
Consider the following data:
db.my_collection.insert({ user: 'bruno', answers: [ {id: 0, value: 3.5},
{id: 1, value: "hello"}]})
db.my_collection.insert({ user: 'bruno2', answers: [ {id: 0, value: 0.5},
{id: 1, value: "world"}]})
I tried using:
db.my_collection.aggregate ({$sort: {"answers": {"$elemMatch": {id: 0,
value: -1}}}})
But, it didn't work. The expected result was: {user: 'bruno2' answers:
[{id: 0, value: 0.5}, {id: 1, value: "world"}]}, {user: 'bruno' answers:
[{id: 0, value: 3.5}, {id: 1, value: "hello"}]})
Thank you.
No comments:
Post a Comment