I would like to do aggregation by using $project to query array of objects to get single field data and store as array in descending order or reverse array. I had searched for some solutions at stackoverflow but did not see questions with project query then reverse array.
For example below is my mock data:
"models" : [
{
"model" : "abc002",
"total_modules" : 2
},
{
"model" : "abc003",
"total_modules" : 2
},
{
"model" : "abc004",
"total_modules" : 2
},
]
I have tried with the below solution but it is not exactly what I want as the output is slightly different as shown below:
db.collection.aggregate([
{$project: {"models.model":1}}
])
Output:
"models" : [
{
"model" : "abc002"
},
{
"model" : "abc003"
},
{
"model" : "abc004"
}
]
**In fact, I would like to get this output:**
{
models: [ abc004, abc003, abc002 ]
}
OR
{
models: [ {model:abc004}, {model:abc003}, {model:abc002} ]
}