I have document like this
{
"id": "1",
"myarray": [
{
"_id": "4",
"name": "d",
"order": 4
},
{
"_id": "1",
"name": "a",
"order": 1
},
{
"_id": "2",
"name": "b",
"order": 2
}
]
}
i want to see sort data in array by order when get from db i am trying some query like below but it seems not OK and nothing changed
db.collection.aggregate([{$unwind: "$myarray"},{$project: {orders:"$myarray.order"}},{$group: {_id:"$_id",min:{$min: "$orders"}}},{$sort: {min:1}}])
db.myarray.find({},{$sort:{"myarray.order":-1}})
db.collection.find({"_id":"1"}).sort({"myarray.order":1})
what is correct query?
I need something like this
db.collection.find({"_id":"1"}).sort({"myarray.order":1})
db.collection.aggregate({ $unwind: '$myarray' }, { $sort: {'myarray.order': -1 }})?