Skip to content

DOCS-800 added dot notation documentation #433

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 2 commits into from
Nov 30, 2012
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
63 changes: 39 additions & 24 deletions source/applications/read.txt
Original file line number Diff line number Diff line change
Expand Up @@ -124,9 +124,11 @@ Consider the following examples that illustrate the use of the
}
)

*Arrays*

- The following operation returns all documents in the ``bios``
collection where the array field ``contribs`` contains the element
``UNIX``:
``'UNIX'``:

.. code-block:: javascript

Expand All @@ -135,11 +137,36 @@ Consider the following examples that illustrate the use of the
contribs: 'UNIX'
}
)

- The following operation returns all documents in the ``bios``
collection where ``awards`` array contains a subdocument element
that contains the ``award`` field equal to ``'Turing Award'`` and the
``year`` field greater than 1980:

.. code-block:: javascript

db.bios.find(
{
awards: {
$elemMatch: {
award: 'Turing Award',
year: { $gt: 1980 }
}
}
}
)

For more examples on accessing arrays, see :ref:`query documents
for arrays <document-query-arrays>` and :ref:`read operations
<read-operations-dot-notation-arrays>`.

*Subdocuments*

- The following operation returns all documents in the ``bios``
collection where the subdocument ``name`` contains a field
``first`` with the value ``Yukihiro`` and a field ``last`` with the
value ``Matsumoto``:
``first`` with the value ``'Yukihiro'`` and a field ``last`` with
the value ``'Matsumoto'``; the query uses :term:`dot-notation` to
access fields in a subdocument:

.. code-block:: javascript

Expand All @@ -151,8 +178,8 @@ Consider the following examples that illustrate the use of the
)

The query matches the document where the ``name`` field contains a
subdocument with the field ``first`` with the value ``Yukihiro``
and a field ``last`` with the value ``Matsumoto``. For instance,
subdocument with the field ``first`` with the value ``'Yukihiro'``
and a field ``last`` with the value ``'Matsumoto'``. For instance,
the query would match documents with ``name`` fields that
held either of the following values:

Expand Down Expand Up @@ -201,6 +228,12 @@ Consider the following examples that illustrate the use of the
first: 'Yukihiro'
}

For more examples on accessing subdocuments, see :ref:`query
documents for subdocuments <document-query-subdocuments>` and
:ref:`read operations <read-operations-dot-notation-subdocuments>`.

*Compound Query Criteria*

- The following operation returns all documents in the ``bios``
collection where either the field ``first`` in the sub-document
``name`` starts with the letter ``G`` **or** where the field
Expand Down Expand Up @@ -230,24 +263,6 @@ Consider the following examples that illustrate the use of the
}
)

- The following operation returns all documents in the ``bios``
collection where ``awards`` array contains a subdocument element
that contains the ``award`` field equal to ``Turing Award`` and the
``year`` field greater than 1980:

.. code-block:: javascript

db.bios.find(
{
awards: {
$elemMatch: {
award: 'Turing Award',
year: { $gt: 1980 }
}
}
}
)

- If there is a ``<projection>`` argument, the :method:`find()
<db.collection.find()>` method returns only those fields as specified
in the ``<projection>`` argument to include or exclude:
Expand Down Expand Up @@ -282,7 +297,7 @@ Consider the following examples that illustrate the use of the

- The following operation finds the documents in the ``bios``
collection where the ``contribs`` field contains the element
``OOP`` and returns all fields *except* the ``_id`` field, the
``'OOP'`` and returns all fields *except* the ``_id`` field, the
``first`` field in the ``name`` subdocument, and the ``birth``
field from the matching documents:

Expand Down
7 changes: 3 additions & 4 deletions source/applications/update.txt
Original file line number Diff line number Diff line change
Expand Up @@ -161,8 +161,8 @@ Consider the following examples that illustrate the use of the
field:

- The :method:`update() <db.collection.update()>` method can perform
the update using the position of the element. Arrays in MongoDB are
zero-based.
the update using the position of the element and
:term:`dot-notation`. Arrays in MongoDB are zero-based.

The following operation queries the ``bios`` collection for
the first document with ``_id`` field equal to ``1`` and updates the
Expand Down Expand Up @@ -196,8 +196,7 @@ Consider the following examples that illustrate the use of the

- The :method:`update() <db.collection.update()>` method can perform
the update of an array that contains subdocuments by using the
:operator:`$` positional operator and the :wiki:`dot notation
<Dot+Notation+(Reaching+into+Objects)>`.
:operator:`$` positional operator and the :term:`dot-notation`.

The following operation queries the ``bios`` collection for the first
document where the ``_id`` field equals ``6`` and the ``awards``
Expand Down
93 changes: 87 additions & 6 deletions source/core/document.txt
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ The document contains the following fields:

- ``_id`` that holds an *ObjectId*.

- ``name`` that holds a *sub-document* that contains the fields
- ``name`` that holds a *subdocument* that contains the fields
``first`` and ``last``.

- ``birth`` and ``death``, which both have *Date* types.
Expand Down Expand Up @@ -246,6 +246,9 @@ Consider the following examples of query documents:

{ _id: 1, name: { first: 'John', last: 'Backus' } }

The ``name`` field must match the document exactly, including the
order of the fields.

- The following document specifies the compound query criteria where
``_id`` is equal to ``1`` **or** the ``name`` field equals the
document ``{ first: 'John', last: 'Backus' }``:
Expand All @@ -254,6 +257,79 @@ Consider the following examples of query documents:

{ $or: [ { _id: 1 }, { name: { first: 'John', last: 'Backus' } } ] }

The ``name`` field must match the document exactly, including the
order of the fields.

.. _document-query-arrays:

*Query Documents for Arrays*
Copy link
Contributor

Choose a reason for hiding this comment

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

these italicized bits need to be subheadings. change throughout.


You can specify query criteria for arrays at the array level and, with
:term:`dot-notation`, at the element level:

- The following document specifies the query criteria where
Copy link
Contributor

Choose a reason for hiding this comment

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

every other sentence begins with the words "the following" which is consistent but also makes things easier to not read and therefore miss/skip over

``contribs`` matches exactly the array ``[ 'Fortran', 'ALGOL',
'Backus-Naur Form', 'FP' ]``, including the order of the elements:

.. code-block:: javascript

{ contribs: [ 'Fortran', 'ALGOL', 'Backus-Naur Form', 'FP' ] }

- The following document specifies the query criteria where the value
of ``contribs`` is an array that contains as one of its element
``'ALGOL'``:
Copy link
Contributor Author

Choose a reason for hiding this comment

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

To select an array given the value of one of it's components, use a query document that resembles:

 fooo

If ALGOL is a member of the array in the contribs field, then this query document will select those documents.


.. code-block:: javascript

{ contribs: 'ALGOL' }

- The following document specifies the query criteria where
``contribs`` contains an element ``'ALGOL'`` in the second position.
Array indexes are zero-based:

.. code-block:: javascript

{ 'contribs.1': 'ALGOL' }

See :ref:`read operations <read-operations-dot-notation-arrays>` for more
Copy link
Contributor

Choose a reason for hiding this comment

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

"See read operations for more information"

doesn't quite read right?

examples with arrays.

.. _document-query-subdocuments:

*Query Documents for Subdocuments*

You can specify query criteria for subdocuments at the subdocument
level and, with :term:`dot-notation`, at the field level:
Copy link
Contributor

Choose a reason for hiding this comment

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

I'd drop the hyphen in dot-notation it'll work fine.


- The following document specifies the query criteria where the value
of the field ``name`` is a subdocument that contains *only* the field
``first`` equal to ``'John'`` and the field ``last`` equal to
``'Backus'``, in that order.

.. code-block:: javascript

{ name: { first: 'John', last: 'Backus' } }

- The following document specifies the query criteria where the value
of the field ``name`` is a subdocument that contains the field
``first`` equal to ``'John'`` and may contain other fields:

.. code-block:: javascript

{ 'name.first': 'John' }

- The following document specifies the query criteria where the value
of the field ``awards`` is an array that contains, as one of its
elements, a subdocument that contains the field ``award`` equal to
``'National Medal of Science'`` and may contain other fields:

.. code-block:: javascript

{ 'awards.award': 'National Medal of Science' }

See :ref:`read operations <read-operations-dot-notation-subdocuments>` for
more examples with subdocuments.

When passed as an argument to methods such as the :method:`find()
<db.collection.find()>` method, the :method:`remove()
<db.collection.remove()>` method, or the :method:`update()
Expand Down Expand Up @@ -295,7 +371,8 @@ When passed as an argument to the :method:`update()

- Modifies the field ``name`` whose value is another document.
Specifically, the :operator:`$set` operator updates the ``middle``
field in the ``name`` subdocument.
field in the ``name`` subdocument. The document uses
:term:`dot-notation` to access a field in a subdocument.

- Adds an element to the field ``awards`` whose value is an array.
Specifically, the :operator:`$push` operator adds another document as
Expand All @@ -315,6 +392,10 @@ When passed as an argument to the :method:`update()
}
)

For additional examples of updates that involve array elements,
Copy link
Contributor

Choose a reason for hiding this comment

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

I think burrying the cross reference at the end of pargraphs like this is probably less than ideal, and is probably a little wordy.

including where the elements are documents, see the :operator:`$`
positional operator.
Copy link
Contributor

Choose a reason for hiding this comment

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

see the positional operatior (i.e. $)


.. _documents-index:
.. _document-index-specification:

Expand All @@ -335,9 +416,10 @@ Index documents contain field and value pairs, in the following form:

- ``value`` is either 1 for ascending or -1 for descending.

The following document specifies the :ref:`multi-key index <index-type-multi-key>` on the ``_id``
field and the ``last`` field contained in the subdocument ``name``
field:
The following document specifies the :ref:`multi-key index
<index-type-multi-key>` on the ``_id`` field and the ``last`` field
contained in the subdocument ``name`` field; the document uses
:term:`dot-notation` to access a field in a subdocument:

.. code-block:: javascript

Expand All @@ -355,7 +437,6 @@ the index to create:
</core/read-operations>` and :doc:`write </core/write-operations>`
operations.


.. _documents-sort-order:

Sort Order Specification Documents
Expand Down
Loading