I'm having issue with sorting child element having multiple array of object.
e.g.
Collection: conversations
[
{
"key" : "value",
"receivers" : [
{ "userId" : 1, "updatedAt" : new Date() },
{ "userId" : 2, "updatedAt" : new Date() }
]
},
{
"key" : "value",
"receivers" : [
{ "userId" : 1, "updatedAt" : new Date() },
{ "userId" : 2, "updatedAt" : new Date() }
]
}
]
Now i'm finding result based on receivers.userId
conversation.find( { 'receivers.userId' : 1 } ).sort({ 'receivers.updatedAt' : -1 })
Here issue i'm facing is with sorting.. i need to sort record only on userId = 1 from child array. currently if userId: 2 has latest updated date then it is coming first.. as i need to consider updatedDate of userId:1