[GODRIVER-1579] Support collection.Count() Created: 15/Apr/20  Updated: 21/Apr/20  Resolved: 21/Apr/20

Status: Closed
Project: Go Driver
Component/s: API
Affects Version/s: 1.3.0, 1.3.2
Fix Version/s: None

Type: Improvement Priority: Major - P3
Reporter: LT Tsai Assignee: Divjot Arora (Inactive)
Resolution: Won't Do Votes: 1
Labels: None
Remaining Estimate: Not Specified
Time Spent: Not Specified
Original Estimate: Not Specified
Environment:

mongodb 3.6



 Description   

Could we support count() and countDocuments()?

I know count() is deprecated, but the performance is way better than countDocuments() in our mongodb 3.6 sharded cluster.

I would like to have count() method a option to use. Thank you



 Comments   
Comment by Divjot Arora (Inactive) [ 21/Apr/20 ]

Hi liting@17.media,

Unfortunately, we cannot bring back a previously deprecated and removed API. The Count() helper was intentionally deprecated because the underlying count server command behaved differently depending on the options provided to it and this created an inconsistent user experience. For more details on this, see https://github.com/mongodb/specifications/blob/master/source/crud/crud.rst#count-api-details.

The server team has done work to improve the performance of the aggregate-based count operation to ensure that it's close to that of the count command. If you have specific operations and performance numbers that you can share, consider filing a ticket in the SERVER Jira project for the perf issues you're seeing. The best workarounds in the driver are to either use one of the existing count helpers or to use Database.RunCommand to run the count command with a filter:

cmd := bson.D{
    {"count", "collectionName"},
    {"query", <filterDocument>},
}
res, err := db.RunCommand(ctx, cmd).DecodeBytes()
// handle error
 
count := res.Lookup("n").Int32()

I'm going to close out this ticket, but feel free to leave another comment if you have any other questions.

– Divjot

Comment by LT Tsai [ 17/Apr/20 ]

Hi Eldar,

Collection.CountDocuments is not as fast as Collection.Count() in our sharded mongo cluster.

Comment by Eldar Jahijagi? [ 16/Apr/20 ]

Hi there,
Would `Collection.CountDocuments` be of any help?

Comment by LT Tsai [ 16/Apr/20 ]

Hi divjot.arora

Thank you for your reply.  

I found that Collection.EstimatedDocumentCount only returns the total count of a collection. 

However, I would like to have a `querry` to count only the documents I cared about. 

For now, it seems only the deprecated count method suits for my use case. 

Maybe one possible way is to let EstimatedDocumentCount support query. Is it possible to implement it? 

  

Comment by Divjot Arora (Inactive) [ 15/Apr/20 ]

Hi liting@17.media,

As you mentioned, count is deprecated and is therefore not supported in the driver. Does Collection.EstimatedDocumentCount satisfy your use case? It runs the count command under the hood.

– Divjot

Generated at Thu Feb 08 08:36:41 UTC 2024 using Jira 9.7.1#970001-sha1:2222b88b221c4928ef0de3161136cc90c8356a66.