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

Empty result when counting from empty collection in aggregation

    • Type: Icon: Bug Bug
    • Resolution: Unresolved
    • Priority: Icon: Major - P3 Major - P3
    • None
    • Affects Version/s: 4.4.4
    • Component/s: Aggregation Framework
    • None
    • Query Optimization
    • ALL
    • Hide

      Example with "live" collection not empty:

      > db.live.find()
      { "_id" : "9674988e-ac03-4aa7-b948-13f7a2df48ea", "severity" : 200, "timestamp" : NumberLong("1614873465861257000"), "summary" : "test alarm", "serial" : "serial1", "tags" : "[]" }
      { "_id" : "ee520f41-62a9-4016-8db1-b538489e8d00", "severity" : 0, "timestamp" : NumberLong("1614873465861759000"), "summary" : "test alarm", "serial" : "serial1", "tags" : "[]" }
      { "_id" : "848ccdcc-10de-4a3b-9cc7-ab863c9be003", "severity" : 300, "timestamp" : NumberLong("1614873465861771000"), "summary" : "test alarm", "serial" : "serial1", "tags" : "[]" }
      { "_id" : "bcf9d77b-3b15-4f14-b4ff-3adc869fdb56", "severity" : 400, "timestamp" : NumberLong("1614873465861797000"), "summary" : "test alarm", "serial" : "serial2", "tags" : "[]" }
      { "_id" : "96a0b9c4-2948-4cf3-b350-d0df55b9c2ed", "severity" : 0, "timestamp" : NumberLong("1614873465861818000"), "summary" : "test alarm", "serial" : "serial2", "tags" : "[]" }
      { "_id" : "4cce4bdd-a393-43b2-8835-ddb58394af80", "severity" : 600, "timestamp" : NumberLong("1614873465861839000"), "summary" : "test alarm", "serial" : "serial2", "tags" : "[]" }
      > db.live.aggregate([{$match:{}},{$count: 'count'},{$out: 'test'}])
      > db.test.find()
      { "_id" : ObjectId("6041050aaf18759a3f23a440"), "count" : 6 }
      

      Example with empty "live" collection:

      > db.live.find()
      > db.live.aggregate([{$match:{}},{$count: 'count'},{$out: 'test'}])
      > db.test.find()
      >
      

       

      Show
      Example with "live" collection not empty: > db.live.find() { "_id" : "9674988e-ac03-4aa7-b948-13f7a2df48ea" , "severity" : 200, "timestamp" : NumberLong( "1614873465861257000" ), "summary" : "test alarm" , "serial" : "serial1" , "tags" : "[]" } { "_id" : "ee520f41-62a9-4016-8db1-b538489e8d00" , "severity" : 0, "timestamp" : NumberLong( "1614873465861759000" ), "summary" : "test alarm" , "serial" : "serial1" , "tags" : "[]" } { "_id" : "848ccdcc-10de-4a3b-9cc7-ab863c9be003" , "severity" : 300, "timestamp" : NumberLong( "1614873465861771000" ), "summary" : "test alarm" , "serial" : "serial1" , "tags" : "[]" } { "_id" : "bcf9d77b-3b15-4f14-b4ff-3adc869fdb56" , "severity" : 400, "timestamp" : NumberLong( "1614873465861797000" ), "summary" : "test alarm" , "serial" : "serial2" , "tags" : "[]" } { "_id" : "96a0b9c4-2948-4cf3-b350-d0df55b9c2ed" , "severity" : 0, "timestamp" : NumberLong( "1614873465861818000" ), "summary" : "test alarm" , "serial" : "serial2" , "tags" : "[]" } { "_id" : "4cce4bdd-a393-43b2-8835-ddb58394af80" , "severity" : 600, "timestamp" : NumberLong( "1614873465861839000" ), "summary" : "test alarm" , "serial" : "serial2" , "tags" : "[]" } > db.live.aggregate([{$match:{}},{$count: 'count' },{$out: 'test' }]) > db.test.find() { "_id" : ObjectId( "6041050aaf18759a3f23a440" ), "count" : 6 } Example with empty "live" collection: > db.live.find() > db.live.aggregate([{$match:{}},{$count: 'count' },{$out: 'test' }]) > db.test.find() >  

      Hi,

      During an aggregation, we need to count the number of elements matching a specific rule. It works properly most of the time, however when there is no matching document (or the collection itself is empty), we get an empty result instead of a document with count equal to 0.

      We understand this is probably a optimization to avoid processing stages on empty result, however operation such as count should probably be computed anyway in order to produce a consistent result.

      The reason why we want to apply count inside aggregation instead of the aggregation result is because we would like to apply MongoDB expression evaluation on it (i.e. {count: { $eq: 20 }} for example).

            Assignee:
            backlog-query-optimization [DO NOT USE] Backlog - Query Optimization
            Reporter:
            mael.jaquier@swissdotnet.ch Maël Jaquier
            Votes:
            0 Vote for this issue
            Watchers:
            8 Start watching this issue

              Created:
              Updated: