This is my data:
{
"name": "some name",
"blockNumber": 10,
"uniqueId: 2
},
{
"name": "some name",
"blockNumber": 9,
"uniqueId: 3
},
{
"name": "some name",
"blockNumber": 10,
"uniqueId: 1
},
This is my query:
myModel.aggregate([
{
$match: someFilter,
},
{
$sort: { blockNumber: -1 },
},
])
And there is the expected result:
{
"name": "some name",
"blockNumber": 10,
"uniqueId: 1
},
{
"name": "some name",
"blockNumber": 10,
"uniqueId: 2
},
{
"name": "some name",
"blockNumber": 9,
"uniqueId: 3
}
I am looking for a way to additionally sort the data depending on a second value (in this case uniqueId) only if blockNum is the same for multiple documents, so that the final result is also sorted by descending uniqueId. Example:
{
"name": "some name",
"blockNumber": 10,
"uniqueId: 2
},
{
"name": "some name",
"blockNumber": 10,
"uniqueId: 1
},
{
"name": "some name",
"blockNumber": 9,
"uniqueId: 3
}
blockNumberdescending anduniqueIdascending, the document withblockNumber: 10anduniqueId: 1will come first then followed byblockNumber: 10anduniqueId: 2.$sortstage check this$sortdocuments are first sorted by <field1>. Then documents with the same <field1> values are further sorted by <field2>.