Skip to content

DOCS-1492 count, create, shardCollection #1061

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

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
49 changes: 49 additions & 0 deletions source/reference/command/count-field.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
object:
name: count
type: dbcommand
field:
optional: false
type: field
name: count
type: string
position: 1
description: |
The name of the collection to count.
---
object:
name: count
type: dbcommand
field:
optional: true
type: field
name: query
type: document
position: 2
description: |
The selection query to determine which documents in the collection to
count.
---
object:
name: count
type: dbcommand
field:
optional: true
type: field
name: limit
type: integer
position: 3
description: |
The maximum number of matching documents to return.
---
object:
name: count
type: dbcommand
field:
optional: true
type: field
name: skip
type: integer
position: 4
description: |
The number of matching documents to skip before returning results.
...
160 changes: 79 additions & 81 deletions source/reference/command/count.txt
Original file line number Diff line number Diff line change
Expand Up @@ -4,88 +4,86 @@ count

.. default-domain:: mongodb

Definition
----------

.. dbcommand:: count

The :dbcommand:`count` command counts the number of documents in a
collection. The command returns a document that contains the count
as well as the command status. The :dbcommand:`count` command takes
the following prototype form:

Counts the number of documents in a collection and returns a document
that contains the number as well as the command status.
:dbcommand:`count` has the following form:

.. code-block:: javascript

{ count: <collection>, query: <query>, limit: <limit>, skip: <skip> }

The command fields are as follows:

:field String count:

The name of the collection to count.

:field document query:

Optional. Specifies the selection query to determine which
documents in the collection to count.

:field integer limit:

Optional. Specifies the limit for the documents matching the
selection ``query``.

:field integer skip:
Optional. Specifies the number of matching documents to skip.

Consider the following examples of the :dbcommand:`count` command:

- Count the number of all documents in the ``orders`` collection:

.. code-block:: javascript

db.runCommand( { count: 'orders' } )

In the result, the ``n``, which represents the count, is ``26``
and the command status ``ok`` is ``1``:

.. code-block:: javascript

{ "n" : 26, "ok" : 1 }

- Count the number of the documents in the ``orders`` collection
with the field ``ord_dt`` greater than ``new Date('01/01/2012')``:

.. code-block:: javascript

db.runCommand( { count:'orders',
query: { ord_dt: { $gt: new Date('01/01/2012') } }
} )

In the result, the ``n``, which represents the count, is ``13``
and the command status ``ok`` is ``1``:

.. code-block:: javascript

{ "n" : 13, "ok" : 1 }

- Count the number of the documents in the ``orders`` collection
with the field ``ord_dt`` greater than ``new Date('01/01/2012')``
skipping the first ``10`` matching records:

.. code-block:: javascript

db.runCommand( { count:'orders',
query: { ord_dt: { $gt: new Date('01/01/2012') } },
skip: 10 } )

In the result, the ``n``, which represents the count, is ``3`` and
the command status ``ok`` is ``1``:

.. code-block:: javascript

{ "n" : 3, "ok" : 1 }

.. note::

MongoDB also provides the :method:`cursor.count()`
method and the shell wrapper :method:`db.collection.count()`
method.

.. read-lock

:dbcommand:`count` has the following fields:

.. include:: /reference/command/count-field.rst

.. note::

MongoDB also provides the :method:`~cursor.count()` method and the
shell wrapper :method:`db.collection.count()` method.

Examples
--------

The following sections provide examples of the :dbcommand:`count` command.

Count all Documents
~~~~~~~~~~~~~~~~~~~

The following operation counts the number of all documents in the
``orders`` collection:

.. code-block:: javascript

db.runCommand( { count: 'orders' } )

In the result, the ``n``, which represents the count, is ``26``,
and the command status ``ok`` is ``1``:

.. code-block:: javascript

{ "n" : 26, "ok" : 1 }

Count Documents with Specified Field Values
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

The following operation counts the number of the documents in the
``orders`` collection with the field ``ord_dt`` greater than ``new
Date('01/01/2012')``:

.. code-block:: javascript

db.runCommand( { count:'orders',
query: { ord_dt: { $gt: new Date('01/01/2012') } }
} )

In the result, the ``n``, which represents the count, is ``13``
and the command status ``ok`` is ``1``:

.. code-block:: javascript

{ "n" : 13, "ok" : 1 }

Skip Matching Documents
~~~~~~~~~~~~~~~~~~~~~~~

The following operation counts the number of the documents in the
``orders`` collection with the field ``ord_dt`` greater than ``new
Date('01/01/2012')`` and skip the first ``10`` matching documents:

.. code-block:: javascript

db.runCommand( { count:'orders',
query: { ord_dt: { $gt: new Date('01/01/2012') } },
skip: 10 } )

In the result, the ``n``, which represents the count, is ``3`` and
the command status ``ok`` is ``1``:

.. code-block:: javascript

{ "n" : 3, "ok" : 1 }
72 changes: 72 additions & 0 deletions source/reference/command/create-field.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
object:
name: create
type: dbcommand
field:
optional: false
type: field
name: create
type: string
position: 1
description: |
The name of the new collection.
---
object:
name: create
type: dbcommand
field:
optional: true
type: field
name: capped
type: Boolean
position: 2
description: |
Creates a :term:`capped collection`. To create a capped collection,
specify ``true``. If you create a capped collection, you must also set
a maximum size in the ``size`` field.
---
object:
name: create
type: dbcommand
field:
optional: true
type: field
name: autoIndexID
type: Boolean
position: 3
description: |
Disables the automatic creation of an index on the ``_id`` field. To
disable the automatic creation, specify ``false``. Before 2.2, the
default value for ``autoIndexId`` was ``false``.
---
object:
name: create
type: dbcommand
field:
optional: true
type: field
name: size
type: integer
position: 4
description: |
The maximum size for the capped collection. Once a capped collection
reaches its maximum size, MongoDB drops old documents from the
collection to make way for new documents. The ``size`` field is
required for capped collections.
---
object:
name: create
type: dbcommand
field:
optional: true
type: field
name: max
type: integer
position: 5
description: |
The maximum number of documents to keep in the capped collection. The
``size`` limit takes precedence over this limit. If a capped
collection reaches its maximum size before it reaches the maximum
number of documents, MongoDB removes old documents. If you use this
limit, ensure that the ``size`` limit is sufficient to contain the
documents limit.
...
63 changes: 28 additions & 35 deletions source/reference/command/create.txt
Original file line number Diff line number Diff line change
Expand Up @@ -4,53 +4,46 @@ create

.. default-domain:: mongodb

Definition
----------

.. dbcommand:: create

The ``create`` command explicitly creates a collection. The command
uses the following syntax:
Explicitly creates a collection. :dbcommand:`create` has the
following form:

.. code-block:: javascript

{ create: <collection_name> }

To create a :term:`capped collection` limited to 40 KB, issue command in
the following form:
{ create: <collection_name>,
capped: <true|false>,
autoIndexId: <true|false>,
size: <max_size>,
max: <max_documents>
}
Copy link
Contributor

Choose a reason for hiding this comment

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

brackets/whitespace are inconsistent here.

Copy link
Author

Choose a reason for hiding this comment

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

I think it's the way it renders on the screen. In the text source, brackets are in column 8 and fields in column 10.

Copy link
Contributor

Choose a reason for hiding this comment

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

the brace is on line 18 with a key/value pair, but appears at the start of 23 rather than 22.


.. code-block:: javascript
:dbcommand:`create` has the following fields:

{ create: "collection", capped: true, size: 40 * 1024 }
.. include:: /reference/command/create-field.rst

The options for creating capped collections are:
For more information on the ``autoIndexId`` field in versions before
2.2, see :ref:`2.2-id-indexes-capped-collections`.

:option capped: Specify ``true`` to create a :term:`capped collection`.
The :method:`db.createCollection()` method wraps the
:dbcommand:`create` command.

:option autoIndexId: Specify ``false`` to disable the automatic
index created on the ``_id`` field. Before
2.2, the default value for ``autoIndexId`` was
``false``. See :ref:`2.2-id-indexes-capped-collections`
for more information.
.. note::

:option size: The maximum size for the capped collection. Once a
capped collection reaches its max size, MongoDB will
drop old documents from the database to make way for
the new documents. You must specify a ``size``
argument for all capped collections.
The :dbcommand:`create` command obtains a write lock on the affected database and
will block other operations until it has completed. The write
lock for this operation is typically short lived. However,
allocations for large capped collections may take longer.

:option max: The maximum number of documents to preserve in the
capped collection. This limit is subject to the
overall size of the capped collection. If a capped
collection reaches its maximum size before it contains
the maximum number of documents, the database will
remove old documents. Thus, if you use this option,
ensure that the total size for the capped collection
is sufficient to contain the max.
Example
-------

The :method:`db.createCollection()` provides a wrapper function that
provides access to this functionality.
To create a :term:`capped collection` limited to 40 KB, issue the
command in the following form:

.. note::
.. code-block:: javascript

This command obtains a write lock on the affected database and
will block other operations until it has completed. The write
lock for this operation is typically short lived; however,
allocations for large capped collections may take longer.
db.runCommand( { create: "collection", capped: true, size: 40 * 1024 } )
Loading