Peter Jaeger
Peter Jaeger

Reputation: 63

MongoDB $sortbycount workaround on Azure Cosmos DB

Unfortunately $sortbycount within an aggregate pipeline is currently not support on Microsoft Azure implementation of MongoDB (CosmosDB). How can I model an easy $sortbycount without actually using it but getting the same results ?

db.gcfblikes.aggregate(
    ...here are some statements then
    {$sortByCount: "$likes.name"}
    ...more statements
)

Upvotes: 1

Views: 181

Answers (1)

Saravana
Saravana

Reputation: 12817

try this aggregation pipeline, $sortByCount is simplified form of $group and $sort by count in descending order

db.gcfblikes.aggregate(
    [
        {$group : {_id : "$likes.name", count : {$sum : 1}}},
        {$sort : {count : -1}}
    ]
)

Upvotes: 3

Related Questions