Skip to content

Agg frame1 DOCS-504 Port aggregation wiki page #413

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

Merged
merged 3 commits into from
Nov 19, 2012
Merged
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
34 changes: 27 additions & 7 deletions source/aggregation.txt
Original file line number Diff line number Diff line change
Expand Up @@ -4,19 +4,39 @@
Aggregation
===========

Aggregation provides
a natural method for aggregating data inside of MongoDB.
.. default-domain:: mongodb

Aggregation provides a natural method for aggregating data inside of
MongoDB.

For a description of MongoDB aggregation, see
:doc:`/applications/aggregation`. For examples of aggregation, see
:doc:`/tutorial/aggregation-examples`. For descriptions of aggregation
operators, see :doc:`/reference/aggregation`.
:doc:`/applications/aggregation`:

.. toctree::
:maxdepth: 2

applications/aggregation

The following is the outline of the aggregation documentation:
For examples of aggregation, see
:doc:`/tutorial/aggregation-examples`:

.. toctree::
:maxdepth: 3

applications/aggregation
tutorial/aggregation-examples

For the descriptions of aggregation operators, see
:doc:`/reference/aggregation`.

.. toctree::
:maxdepth: 3

reference/aggregation

In addition to the aggregation framework, MongoDB provides simple
:doc:`aggregation methods and commands </reference/simple-aggregation>`:

.. toctree::
:maxdepth: 2

reference/simple-aggregation
2 changes: 1 addition & 1 deletion source/applications/aggregation.txt
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ In a shell environment the pipe redirects a stream of characters from
the output of one process to the input of the next. The MongoDB
aggregation pipeline streams MongoDB documents from one :ref:`pipeline
operator <aggregation-pipeline-operator-reference>` to the next to process the
documents.
documents. Pipeline operators can be repeated in the pipe.

All pipeline operators process a stream of documents and the
pipeline behaves as if the operation scans a :term:`collection` and
Expand Down
88 changes: 74 additions & 14 deletions source/reference/command/count.txt
Original file line number Diff line number Diff line change
Expand Up @@ -7,25 +7,85 @@ count
.. dbcommand:: count

The :dbcommand:`count` command counts the number of documents in a
collection. For example:

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:

.. 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.

> db.runCommand( { count: "collection" } );
{ "n" : 10 , "ok" : 1 }
Consider the following examples of the :dbcommand:`count` command:

In the :program:`mongo` shell, this returns the number of documents
in the collection (e.g. ``collection``.) You may also use the
:method:`count() <cursor.count()>` method on any cursor object to
return a count of the number of documents in that cursor. The
following operation produces the same result in the
:program:`mongo` shell:
- Count the number of all documents in the ``orders`` collection:

.. code-block:: javascript
.. 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

> db.collection.count():
{ "n" : 10 , "ok" : 1 }
{ "n" : 3, "ok" : 1 }

The collection in this example has 10 documents.
.. note::

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

.. read-lock
70 changes: 55 additions & 15 deletions source/reference/command/distinct.txt
Original file line number Diff line number Diff line change
Expand Up @@ -6,28 +6,68 @@ distinct

.. dbcommand:: distinct

The :dbcommand:`distinct` command returns an array of distinct values for a
given field across a single collection. The command takes the
following form:
The :dbcommand:`distinct` command finds the distinct values for a
specified field across a single collection. The command returns a
document that contains an array of the distinct values as well as
the query plan and status. The command takes the following prototype
form:

.. code-block:: javascript

{ distinct: collection, key: age, query: { query: { field: { $exists: true } } } }
{ distinct: collection, key: <field>, query: <query> }

This operation returns all distinct values of the field (or
``key``) ``age`` in documents that match the query ``{ field: {
$exists: true }``.
The command fields are as follows:

:field String collection:

The name of the collection to query for distinct values.

.. note::
:field string field:

Specifies the field for which to return the distinct values.

:field document query:

Optional. Specifies the selection ``query`` to determine the
subset of documents from which to retrieve the distinct
values.

The query portion of the :dbcommand:`distinct` is optional.
Consider the following examples of the :dbcommand:`distinct` command:

The shell and many :term:`drivers <driver>` provide a helper method that provides
this functionality. You may prefer the following equivalent syntax:
- Return an array of the distinct values of the field ``ord_dt``
from all documents in the ``orders`` collection:

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

db.runCommand ( { distinct: 'orders', key: 'ord_dt' } )

- Return an array of the distinct values of the field ``sku`` in the
subdocument ``item`` from all documents in the ``orders``
collection:

.. code-block:: javascript

db.collection.distinct("age", { field: { $exists: true } } );
db.runCommand ( { distinct: 'orders', key: 'item.sku' } )

The :dbcommand:`distinct` command will use an index to locate and
return data.
- Return an array of the distinct values of the field ``ord_dt``
from the documents in the ``orders`` collection where the
``price`` is greater than ``10``:

.. code-block:: javascript

db.runCommand ( { distinct: 'orders',
key: 'ord_dt',
query: { price: { $gt: 10 } }
} )

.. note::

- MongoDB also provides the shell wrapper method
:method:`db.collection.distinct()` for the
:dbcommand:`distinct` command. Additionally, many MongoDB
:term:`drivers <driver>` also provide a wrapper method. Refer
to the specific driver documentation.

- When possible, the :dbcommand:`distinct` command will use an
index to find the documents in the query as well as to return
the data.
Loading