Uploaded image for project: 'Core Server'
  1. Core Server
  2. SERVER-44836

count_documents with filter does not use the partialFilterExpression index right when the keys in the filter are indexed

    • Type: Icon: Bug Bug
    • Resolution: Duplicate
    • Priority: Icon: Major - P3 Major - P3
    • None
    • Affects Version/s: None
    • Component/s: None
    • Labels:
      None
    • ALL

      Hello,

      I have a large sharded collection with similar documents. A part of them have a field called "tag", which is always a string. I've indexed the field using partialFilterExpression:

      {"v" : 2,
       "key" : { "tag" : 1 },
       "name" : "tag",
       "ns" : "database.collection",
       "partialFilterExpression" : {
       "tag" : { "$exists" : true } }
       }

       

      However, when i run database.collection.count_documents({"tag": "value"}), the result takes a long time to complete. Running current_op shows me the query running:

      "secs_running" : NumberLong(616),
       "microsecs_running" : NumberLong(616856589),
       "op" : "getmore",
       "ns" : "database.collection",
       "command" : {
        "getMore" : NumberLong("4801338963376724146"),
        "collection" : "collection",
       "planSummary" : "IXSCAN { tag: 1 }",
       "cursor" : {
       "cursorId" : NumberLong("4801338963376724146"),
       "createdDate" : ISODate("2019-11-26T13:23:57.281Z"),
       "lastAccessDate" : ISODate("2019-11-26T13:23:57.282Z"),
       "nDocsReturned" : NumberLong(0),
       "nBatchesReturned" : NumberLong(0),
       "noCursorTimeout" : false,
       "tailable" : false,
       "awaitData" : false,
       "originatingCommand" : {
       "$truncated" : "{ $truncated: \"{ aggregate: \"collection\", pipeline: [ { $match: { tag: \"value\" } }, { $group: { _id: { $const: 1 }, n: { $sum: { $const: 1 } } } } ], fro...\" }"
       },

       So i'm running an aggregate, which i know can be slow. I remember running these kind of queries very fast, when the field was indexed. Is this because the index is a partialFilterExpression? If so, it looks like a bug, because i think it should be able to use the information from that index in order to complete a simple count operation.

       

      I'm running mongod 4.2.1, and the count was issued with pymongo 3.9.0.

            Assignee:
            eric.sedor@mongodb.com Eric Sedor
            Reporter:
            thestick613 Tudor Aursulesei
            Votes:
            0 Vote for this issue
            Watchers:
            4 Start watching this issue

              Created:
              Updated:
              Resolved: