今天我也在思考同样的问题,然后看到了Romain的答案。虽然我想亲自验证一下,但是可以通过在两个聚合上使用explain轻松地完成;
db.verpakking.explain().aggregate([
{ "$match": {type: "VERPAKT"} },
{ "$match": {ras: "CherryStar"} },
]);
这将导致以下输出结果:
{
"waitedMS" : NumberLong(0),
"stages" : [
{
"$cursor" : {
"query" : {
"$and" : [
{
"type" : "VERPAKT"
},
{
"ras" : "CherryStar"
}
]
},
"queryPlanner" : {
"plannerVersion" : NumberInt(1),
"namespace" : "denberk.verpakking",
"indexFilterSet" : false,
"parsedQuery" : {
"$and" : [
{
"ras" : {
"$eq" : "CherryStar"
}
},
{
"type" : {
"$eq" : "VERPAKT"
}
}
]
},
"winningPlan" : {
"stage" : "COLLSCAN",
"filter" : {
"$and" : [
{
"ras" : {
"$eq" : "CherryStar"
}
},
{
"type" : {
"$eq" : "VERPAKT"
}
}
]
},
"direction" : "forward"
},
"rejectedPlans" : [
]
}
}
}
],
"ok" : NumberInt(1)
}
虽然...
db.verpakking.explain().aggregate([
{ "$match": {type: "VERPAKT", ras: "CherryStar"} },
]);
输出结果:
{
"waitedMS" : NumberLong(0),
"stages" : [
{
"$cursor" : {
"query" : {
"type" : "VERPAKT",
"ras" : "CherryStar"
},
"queryPlanner" : {
"plannerVersion" : NumberInt(1),
"namespace" : "denberk.verpakking",
"indexFilterSet" : false,
"parsedQuery" : {
"$and" : [
{
"ras" : {
"$eq" : "CherryStar"
}
},
{
"type" : {
"$eq" : "VERPAKT"
}
}
]
},
"winningPlan" : {
"stage" : "COLLSCAN",
"filter" : {
"$and" : [
{
"ras" : {
"$eq" : "CherryStar"
}
},
{
"type" : {
"$eq" : "VERPAKT"
}
}
]
},
"direction" : "forward"
},
"rejectedPlans" : [
]
}
}
}
],
"ok" : NumberInt(1)
}
如您所见,这段代码完全相同,除了“query”部分(这是正常的,因为我们的查询是不同的)。这证明,无论您使用两个连续的独立的$match管道还是一个组合的$match管道,解析后的查询都将完全相同。