-
Type: Bug
-
Resolution: Works as Designed
-
Priority: Major - P3
-
None
-
Affects Version/s: 3.4.15
-
Component/s: Querying
-
None
-
ALL
-
(copied to CRM)
When perform "count" with "$elemmatch" in the query it never uses COUNT_SCAN and has to FETCH document before counting. Here is an example:
//create a simple document that has an array of embedded doc, like this: var doc = { "annotationList": [ { "meta": { "league": "league_value1", "class": "class_value1" } }, { "meta": { "league": "league_value1", "class": "class_value1" } } ] } //save doc to the collection db.testCollection.save(doc); //build compound index using 2 multikey field db.testCollection.ensureIndex({"annotationList.meta.league": 1, "annotationList.meta.league": 1});
Now if we perform the following:
db.testCollection.explain("executionStats").count({ "annotationList": { "$elemMatch": { "meta.league": "league_value1" } } });
We can see that it does the following: XSCAN -> FETCH -> COUNT
However if we do the following:
cursor = db.testCollection.explain("executionStats").count({ "annotationList.meta.league": "league_value1" });
We can see that now there is not need to FETCH documents and it simply performs COUNT_SCAN
- is related to
-
SERVER-37290 Avoid FETCH stage with $elemmatch if using covered index and multiKeyPaths is correct
- Closed
- related to
-
SERVER-27494 Avoid unwind on multi-key index field for covered index aggregation
- Backlog
-
SERVER-41594 Covered query don't use index for range $gt+$lt. Only for one side and FETCH filter for another
- Closed