-
Notifications
You must be signed in to change notification settings - Fork 10
DOCSP-41137: Count Documents #4
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
61cd203
a64a6d5
d88905e
9ea14c2
aaf531c
8f9feda
e7ec682
238134a
ecc88dd
de04ef9
3969be9
e0e807a
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,190 @@ | ||
.. _kotlin-sync-count: | ||
|
||
=============== | ||
Count Documents | ||
=============== | ||
|
||
.. contents:: On this page | ||
:local: | ||
:backlinks: none | ||
:depth: 2 | ||
:class: singlecol | ||
|
||
.. facet:: | ||
:name: genre | ||
:values: reference | ||
|
||
.. meta:: | ||
:keywords: number, amount, estimation, code example | ||
|
||
Overview | ||
--------- | ||
|
||
In this guide, you can learn how to retrieve accurate and estimated counts of the | ||
number of documents in a collection. | ||
|
||
Sample Data | ||
~~~~~~~~~~~ | ||
|
||
The examples in this guide use the ``movies`` collection in the ``sample_mflix`` | ||
database from the :atlas:`Atlas sample datasets </sample-data>`. To learn how to create a | ||
free MongoDB Atlas cluster and load the sample datasets, see the | ||
:atlas:`Get Started with Atlas </getting-started>` guide. | ||
|
||
.. _kotlin-sync-accurate-count: | ||
|
||
Retrieve an Accurate Count | ||
-------------------------- | ||
|
||
Use the ``countDocuments()`` method to count the number of documents that are in a | ||
collection. To count the number of documents that match specified search | ||
critera, pass a query filter to the ``countDocuments()`` method. | ||
|
||
.. TODO: To learn more about specifying a query, see :ref:`kotlin-sync-specify-query`. | ||
|
||
Count All Documents | ||
~~~~~~~~~~~~~~~~~~~ | ||
|
||
To return a count of all documents in the collection, call the ``countDocuments()`` method | ||
with no arguments, as shown in the following example: | ||
|
||
.. io-code-block:: | ||
|
||
.. input:: | ||
:language: kotlin | ||
|
||
print(collection.countDocuments()) | ||
|
||
.. output:: | ||
:visible: false | ||
|
||
21349 | ||
|
||
Count Specific Documents | ||
~~~~~~~~~~~~~~~~~~~~~~~~ | ||
|
||
To return a count of documents that match specific search criteria, specify your query | ||
in the ``countDocuments()`` method. The following example prints a count of all documents | ||
in the ``movies`` collection that have a ``year`` field value equal to ``1930``: | ||
|
||
.. io-code-block:: | ||
|
||
.. input:: | ||
:language: kotlin | ||
|
||
print(collection.countDocuments(eq("year", "1930"))) | ||
|
||
.. output:: | ||
:visible: false | ||
|
||
10 | ||
|
||
Customize Count Behavior | ||
~~~~~~~~~~~~~~~~~~~~~~~~ | ||
|
||
The ``countDocuments()`` method accepts optional parameters in the form of a | ||
``CountOptions`` object, which represents options you can use to configure the count | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Q: how do you set these options - by specifying fields of a There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Can add some clarifying wording. |
||
operation. You can set these options by instantiating a new ``CountOptions`` object, | ||
setting the object's fields using the corresponding methods, and passing it to the | ||
``countDocuments()`` method. If you don't specify any options, the driver does not | ||
customize the count operation. | ||
|
||
The following table describes the options you can set to customize ``countDocuments()``: | ||
|
||
.. list-table:: | ||
:widths: 30 70 | ||
:header-rows: 1 | ||
|
||
* - Option | ||
- Description | ||
|
||
* - ``comment`` | ||
- | A comment to attach to the operation. | ||
|
||
* - ``skip`` | ||
- | The number of documents to skip before returning results. | ||
|
||
* - ``limit`` | ||
- | The maximum number of documents to count. Must be a positive integer. | ||
|
||
* - ``maxTime`` | ||
- | The maximum amount of time to allow the operation to run, in milliseconds. | ||
|
||
* - ``collation`` | ||
- | The collation to use for the operation. | ||
|
||
* - ``hint`` | ||
- | Gets or sets the index to scan for documents. | ||
|
||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. S: I think it would be helpful to add a code example here that passes an options argument |
||
The following example uses a ``CountOptions`` object to add a comment to the | ||
``countDocuments()`` operation: | ||
|
||
.. code-block:: kotlin | ||
|
||
val options = CountOptions().comment("Retrieving count") | ||
collection.countDocuments(options) | ||
|
||
.. _kotlin-sync-estimated-count: | ||
|
||
Retrieve an Estimated Count | ||
--------------------------- | ||
|
||
Use the ``estimatedDocumentCount()`` method to retrieve an estimate of the number of | ||
documents in a collection. The method estimates the amount of documents based on | ||
collection metadata, which can be faster than performing an accurate count. | ||
|
||
The following example prints the estimated number of documents in a collection: | ||
|
||
.. io-code-block:: | ||
|
||
.. input:: | ||
:language: kotlin | ||
|
||
print(collection.estimatedDocumentCount()) | ||
|
||
.. output:: | ||
:visible: false | ||
|
||
21349 | ||
|
||
Customize Estimated Count Behavior | ||
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ | ||
|
||
The ``estimatedDocumentCount()`` method accepts optional parameters in the form of an | ||
``EstimatedDocumentCountOptions`` object, which represents options you can use to configure | ||
the count operation. You can set these options by instantiating a new | ||
``EstimatedDocumentCountOptions`` object, setting the object's fields using the | ||
corresponding methods, and passing it to the ``estimatedDocumentCount()`` method. | ||
If you don't specify any options, the driver does not customize the count operation. | ||
|
||
The following table describes the options you can set to customize ``estimatedDocumentCount()``: | ||
|
||
.. list-table:: | ||
:widths: 30 70 | ||
:header-rows: 1 | ||
|
||
* - Option | ||
- Description | ||
|
||
* - ``comment`` | ||
- | A comment to attach to the operation. | ||
|
||
* - ``maxTime`` | ||
- | The maximum amount of time to allow the operation to run, in milliseconds. | ||
|
||
The following example uses an ``EstimatedDocumentCountOptions`` object to add a comment to | ||
the ``estimatedDocumentCount()`` operation: | ||
|
||
.. code-block:: kotlin | ||
|
||
val options = EstimatedDocumentCountOptions().comment("Retrieving count") | ||
collection.estimatedDocumentCount(options) | ||
|
||
API Documentation | ||
----------------- | ||
|
||
To learn more about any of the methods or types discussed in this | ||
guide, see the following API documentation: | ||
|
||
- `countDocuments() <{+api+}/mongodb-driver-kotlin-sync/com.mongodb.kotlin.client/-mongo-collection/count-documents.html>`__ | ||
- `estimatedDocumentCount() <{+api+}/mongodb-driver-kotlin-sync/com.mongodb.kotlin.client/-mongo-collection/estimated-document-count.html>`__ |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
S: not necessary, but it might be helpful to use one of the Atlas sample datasets for the code examples on this page and introduce it here (similar to the C++ count documents page). This could help make the current code examples a little more specific and would provide an output