We are using a fairly trivial query to count data in a collection where a specific nested field is set or not set.
db[‘collname’].find({“consumer.consumed”:{$exists: true}}).count()
As simple as it comes. In order to be quick, we have created an index. The MongoDB profiler and the explain plan show that the index should be used. The query as such should be as simple as skimming over the existing index and return a docment number. However the explain shows a large number of yields that I completely fail to understand and execution quite often takes several seconds.
{
"appName": "MongoDB Shell",
"command": {
"count": "collname",
"query": {
"consumer.consumed": {
"$exists": true
}
},
"fields": {},
"$clusterTime": {
"clusterTime": {
"$timestamp": {
"t": 1591257092,
"i": 1
}
},
"signature": {
"hash": {
"$binary": "PHh4eHh4eD4=",
"$type": "00"
},
"keyId": {
"$numberLong": "6790010151542718465"
}
}
},
"$db": "dpa-id-hub"
},
"planSummary": [
{
"IXSCAN": {
"consumer.consumed": 1
}
}
],
"keysExamined": 316394,
"docsExamined": 316394,
"numYields": 2473,
"reslen": 170,
"locks": {
"Global": {
"acquireCount": {
"r": 2474
}
},
"Database": {
"acquireCount": {
"r": 2474
}
},
"Collection": {
"acquireCount": {
"r": 2474
}
}
},
"storage": {
"data": {
"bytesRead": 10726149,
"timeReadingMicros": 42554
}
},
"protocol": "op_msg",
"millis": 709
}