My document is as follows :
{
"_id" : ObjectId("622136811b68d9136e48ba4e"),
"brand_name" : "iPhone",
"brand_rating" : [
{
"cust_name" : "Sham K",
"rating" : 5
},
{
"cust_name" : "Nil",
"rating" : 5
}
],
"models" : [
{
"model_name" : "iPhone 7Plus",
"RAM" : "4GB",
"ROM" : "64GB",
"price" : 98000,
"buyer" : [
{
"cust_name" : "Anu",
"rating" : 3
},
{
"cust_name" : "Kiran",
"rating" : 4
}
]
},
{
"model_name" : "iPhone 2",
"RAM" : "3GB",
"ROM" : "32GB",
"price" : 58000,
"buyer" : [
{
"cust_name" : "Kiran",
"rating" : 4
}
]
}
]
}
Question is List all the customers in descending order who bought the iPhone 7plus.
I try this but sorting is not working
db.brand.aggregate({$unwind : "$models"},{$match : {"models.model_name" :"iPhone 7Plus" }}, {$project : {_id : 0, "models.buyer.cust_name" : 1}}, {$sort : {"models.buyer.cust_name" : -1} })
Output :
{ "models" : { "buyer" : [ { "cust_name" : "Anu" }, { "cust_name" : "Kiran" } ] } }
> [5]
What is the easiest way to solve it?