Search code examples
mongodbaggregation-frameworkexplainnosql

MongoDB: $match and $explain


I have the following query:

uno = db.prueba.aggregate(
    {$project: {a_gt_b: {$cmp: ['$num-a','$num-b']}}},
    {$match: {a_gt_b:{$gt:0}}},
    {$group:{"_id":"$a_gt_b",total:{"$sum":1}}},
    {$project: {"_id":0,"total":1}}
);

I want execute $explain command, according to this answer I have to take the $match portion but I don't know how.


Solution

  • That answer is super outdated (I will update it). Starting with Mongo 2.6 you can explain your aggregation results:

    db.orders.aggregate([
       # put your whole aggregation query
    ], {
       explain: true
    })