Created
March 7, 2021 19:04
-
-
Save RahulJyala7/7f08b12c2002774054a34883a432f18e to your computer and use it in GitHub Desktop.
Mongo query with sorted subDocument
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
db.getCollection('test').aggregate([{$match: {name: "item6"}},{$unwind: '$slots'}, {$sort: {"slots.date": -1}}, | |
{$group: {_id: '$_id',root: { $mergeObjects: '$$ROOT' }, slots: {$push: '$slots'}}}, { "$limit": 1 }, { | |
$replaceRoot: { | |
newRoot: { | |
$mergeObjects: ['$root', '$$ROOT'] | |
} | |
} | |
}, { | |
$project: { | |
root: 0 | |
} | |
}]) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment