I have the following query:
db.pmusers.aggregate([
{
$unwind: '$preferableUsersIds'
},
{
$group:{_id: '$preferableUsersIds', number:{$sum: 1}}
},
{
$sort:{number:-1}
},
{
$limit:1
}
])
I understand that it is not the optimal solution because I sort all rntries instead of find only one.
Does mongoDb support to rewrite it in more efficient way?
P.S.
I know aboout $max but don't see how it can help me.
this one works at least not faster:
db.pmusers.aggregate([
{
$unwind: '$preferableUsersIds'
},
{
$sortByCount: "$preferableUsersIds"
},
{
$limit:1
}
])