Skip to content

DOCS-4829 #2389

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
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
96 changes: 96 additions & 0 deletions source/tutorial/project-fields-from-query-results.txt
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,102 @@ The operation returns the following document:
.. code-block:: javascript

{ "classification" : { "category" : "chocolate" } }

Suppress Specific Fields in Embedded Documents
----------------------------------------------

Use :ref:`dot notation <document-dot-notation>` to supress specific
fields inside an embedded document using a ``0`` instead of ``1``.
For example, the ``inventory`` collection contains the following document:

.. code-block:: javascript

{
"_id" : 3,
"type" : "food",
"item" : "Super Dark Chocolate",
"classification" : { "dept" : "grocery", "category" : "chocolate"},
"vendor" : {
"primary" : {
"name" : "Marsupial Vending Co",
"address" : "Wallaby Rd",
"delivery" : ["M","W","F"]
},
"secondary":{
"name" : "Intl. Chocolatiers",
"address" : "Cocoa Plaza",
"delivery" : ["Sa"]
}
}
}

The following operation returns all documents where the value of the
``type`` field is ``food`` and the ``_id`` field is ``3``.
The projection supresses only the ``category`` field in the
``classification`` document. The ``dept`` field remains
inside the ``classification`` document.

.. code-block:: javascript

db.inventory.find(
{ type: 'food', _id: 3 },
{ "classification.category": 0}
)

The operation returns the following document:

.. code-block:: javascript

{
"_id" : 3,
"type" : "food",
"item" : "Super Dark Chocolate",
"classification" : { "dept" : "grocery"},
"vendor" : {
"primary" : {
"name" : "Bobs Vending",
"address" : "Wallaby Rd",
"delivery" : ["M","W","F"]
},
"secondary":{
"name" : "Intl. Chocolatiers",
"address" : "Cocoa Plaza",
"delivery" : ["Sa"]
}
}
}
You can supress nested subdocuments at any depth using :ref:`dot notation <document-dot-notation>`.
The following specifies a projection to suppress the ``delivery`` array only for the ``secondary`` document.

.. code-block:: javascript

db.inventory.find(
{ "type" : "food" },
{ "vendor.secondary.delivery" : 0 }
)

This returns all documents except the ``delivery`` array in the ``secondary`` document

.. code-block:: javascript

{
"_id" : 3,
"type" : "food",
"item" : "Super Dark Chocolate",
"classification" : { "dept" : "grocery", "category" : "chocolate"},
"vendor" : {
"primary" : {
"name" : "Bobs Vending",
"address" : "Wallaby Rd",
"delivery" : ["M","W","F"]
},
"secondary":{
"name" : "Intl. Chocolatiers",
"address" : "Cocoa Plaza"
}
}
}


Projection for Array Fields
---------------------------
Expand Down