Skip to content

Conversation

@h3n4l
Copy link
Member

@h3n4l h3n4l commented Jan 19, 2026

Summary

  • Add support for db.collection.countDocuments() command
  • Add support for db.collection.estimatedDocumentCount() command

countDocuments()

  • Returns the count of documents matching a filter
  • Supports optional filter argument with query operators
  • Supports options: hint, limit, skip

estimatedDocumentCount()

  • Returns a fast estimate of total document count using collection metadata
  • Faster than countDocuments() for large collections as it doesn't scan documents

Syntax

// countDocuments
db.users.countDocuments()
db.users.countDocuments({ status: "active" })
db.users.countDocuments({ age: { $gte: 30 } })
db.users.countDocuments({}, { hint: "_id_" })
db.users.countDocuments({}, { hint: { status: 1 } })
db.users.countDocuments({}, { limit: 100, skip: 10 })

// estimatedDocumentCount
db.users.estimatedDocumentCount()

Test plan

  • Integration tests for countDocuments with/without filter
  • Integration tests for countDocuments with options (hint, limit, skip)
  • Integration tests for estimatedDocumentCount
  • All existing tests pass
  • Linting clean

🤖 Generated with Claude Code

Copilot AI review requested due to automatic review settings January 19, 2026 07:29
Copy link

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR adds support for the db.collection.countDocuments() MongoDB operation, enabling users to count documents in a collection with optional filtering capabilities.

Changes:

  • Added opCountDocuments operation type constant
  • Implemented argument extraction for countDocuments with optional filter support
  • Added executor function to handle countDocuments operations
  • Comprehensive test coverage for various scenarios (no filter, with filter, empty collection, empty filter)

Reviewed changes

Copilot reviewed 3 out of 3 changed files in this pull request and generated no comments.

File Description
translator.go Added opCountDocuments operation type, implemented extractCountDocumentsArgs function to parse filter arguments, and integrated countDocuments into the method call visitor
executor.go Added executeCountDocuments function to execute the count operation via MongoDB driver and integrated it into the operation dispatcher
executor_test.go Added four comprehensive test cases covering basic usage, filtering with operators, empty collections, and empty filters

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Add support for the db.collection.countDocuments() command which
returns the count of documents matching a filter.

Syntax:
- db.users.countDocuments()
- db.users.countDocuments({ status: "active" })
- db.users.countDocuments({ age: { $gte: 30 } })
- db.users.countDocuments({}, { hint: "_id_" })
- db.users.countDocuments({}, { limit: 100, skip: 10 })

Supported options:
- hint: index name (string) or index specification (document)
- limit: maximum count
- skip: number of documents to skip

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
@h3n4l h3n4l force-pushed the vk/bd54-gomongo-mileston branch from 95ddc3a to 114c1fc Compare January 19, 2026 07:40
Add support for the estimatedDocumentCount() method which returns a fast
estimate of the total document count in a collection using collection metadata.
This method does not accept a filter and is much faster than countDocuments()
for large collections.

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
@h3n4l h3n4l changed the title feat: add db.collection.countDocuments() operation support feat: add db.collection.countDocuments() and estimatedDocumentCount() operation support Jan 19, 2026
@h3n4l h3n4l merged commit 8a58a0c into main Jan 19, 2026
2 checks passed
@h3n4l h3n4l deleted the vk/bd54-gomongo-mileston branch January 19, 2026 07:54
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants