Good morning! I have a collection, product_part_number, and it contains a little more than 2.3 million documents like the following:
{ _id: 2988725, partNumber: '2WFG4' },
{ _id: 3177996, partNumber: '2WFP4' }
(Yes, it only contains _id and partNumber). I also have the following compound index:
{
v: 2,
key: { partNumber: -1, _id: 1 },
name: 'partNumber_-1__id_1'
}
Now I need to see which partNumber
corresponds to the most products using the following aggregation:
db.product_part_number.aggregate( [
{
$group: {
_id: "$partNumber",
count: { $sum: 1 }
}
},
{
$sort: { count: -1 }
},
{
$limit: 1
}
])
However, running explain(“executionStats”) results in an error:
MongoServerError: Exceeded memory limit for $group, but didn't allow external sort. Pass allowDiskUse:true to opt in.
If I add { allowDiskUse: true }
to the expalin() call I can see no any index is used:
executionStats: {
executionSuccess: true,
nReturned: 2388237,
executionTimeMillis: 15192,
totalKeysExamined: 0,
totalDocsExamined: 2388237,
.
.
.
In my situation, how can I use index? Thanks a lot, in advance!