There is a document with nested arrays, just want to sort collection by customerId then productList.productId and then productList.itemList.id in ascending order. MongoDb version is 3.0.14.
I've tried so far this and the query doesn't sort the collection as expected:
db.file_old.find({}, {
customerId: 1,
"productList.productId": 1,
"productList.itemList.id": 1
})
.sort({
customerId: 1,
productList: 1,
"productList.itemList": 1
})
and try aggregate framework also like this:
db.file_old.aggregate([
{"$unwind": "$productList"} ,
{"$sort": {"customerId": 1, "productList.productId": 1}}
])
It work fine for two field but if try to adding "productList.itemList.id" doesn't work, like this:
db.file_old.aggregate([
{"$unwind": "$productList"} ,
{"$sort": {"customerId": 1, "productList.productId": 1, "productList.itemList.id": 1}}
])
Collection structure:
{
"_id" : ObjectId("5f33cc2a1e84082968132324"),
"customerId" : 2196,
"productList" : [
{
"productId" : 7531,
"itemList" : [
{
"id" : 144
},
{
"id" : 145
}
]
},
{
"productId" : 7534,
"itemList" : [
{
"id" : 1244
},
{
"id" : 1243
},
{
"id" : 1245
},
{
"id" : 1242
}
]
}
]
},{
"_id" : ObjectId("5f33cc2a1e84082968132326"),
"customerId" : 2201,
"productList" : [
{
"productId" : 101201,
"itemList" : [
{
"id" : 863
},
{
"id" : 865
},
{
"id" : 862
}
]
},
{
"productId" : 7537,
"itemList" : [
{
"id" : 982
},
{
"id" : 1002
},
{
"id" : 896
}
]
}
]
}