I encountered weird thing when using C# MongoDB `CountDocumentAsync` function. I enabled query logging on MongoDB and this is what I got:
{
"op" : "command",
"ns" : "somenamespace",
"command" : {
"aggregate" : "reservations",
"pipeline" : [
{
"some_query_key": "query_value"
},
{
"$group" : {
"_id" : null,
"n" : {
"$sum" : 1
}
}
}
],
"cursor" : {}
},
"keyUpdates" : 0,
"writeConflicts" : 0,
"numYield" : 9,
"locks" : {
"Global" : {
"acquireCount" : {
"r" : NumberLong(24)
}
},
"Database" : {
"acquireCount" : {
"r" : NumberLong(12)
}
},
"Collection" : {
"acquireCount" : {
"r" : NumberLong(12)
}
}
},
"responseLength" : 138,
"protocol" : "op_query",
"millis" : 2,
"execStats" : {},
"ts" : ISODate("2018-09-27T14:08:48.099Z"),
"client" : "172.17.0.1",
"allUsers" : [ ],
"user" : ""
}
simple count is converted into an aggregate.
More interestingly when I use `CountAsync` function (which btw is marked obsolete with remark I should be using `CountDocumentsAsync`) it produces:
{
"op" : "command",
"ns" : "somenamespace",
"command" : {
"count" : "reservations",
"query" : {
"query_key": "query_value"
}
},
"keyUpdates" : 0,
"writeConflicts" : 0,
"numYield" : 9,
"locks" : {
"Global" : {
"acquireCount" : {
"r" : NumberLong(20)
}
},
"Database" : {
"acquireCount" : {
"r" : NumberLong(10)
}
},
"Collection" : {
"acquireCount" : {
"r" : NumberLong(10)
}
}
},
"responseLength" : 62,
"protocol" : "op_query",
"millis" : 2,
"execStats" : {
},
"ts" : ISODate("2018-09-27T13:58:27.758Z"),
"client" : "172.17.0.1",
"allUsers" : [ ],
"user" : ""
}
which is what I would expect. Does anyone know what might be a reason for this behavior? I browsed documentation but didn't find anything interesting regarding it.
There is one stackoverflow's question regarding performance of aggregation as above vs count operation and according to what is written there aggregation is a slower operation
simple count is converted into an aggregate.
Hi Damian,
This is part of the changes mentioned in DRIVERS-501 to implement new count API. There are two new helpers added estimatedDocumentCount and countDocuments. For example in C#: EstimatedDocumentCountAsync() and CountAsync().
This is because the behaviour of the deprecated count() command differs depending on the options passed to it and the topology in use; and may or may not provide an accurate count. The new helpers were added to ensure developers make an informed decision about how to count() documents.
The names of the new helpers were chosen to make it clear how they behave and exactly what they do. The estimatedDocumentCount helper returns an estimate of the count of documents in the collection using collection metadata, rather than counting the documents or consulting an index. The countDocuments helper counts the documents that match the provided query filter using an aggregation pipeline.
See also: MongoDB Driver Specs: Count API Details
There is one stackoverflow’s question regarding performance of aggregation as above vs count operation and according to what is written there aggregation is a slower operation
The StackOverflow post is from 2015; there have been many changes and improvements to MongoDB since.
Regards,
Wan.