Skip to content

DOCSP-18892 improved examples for getField #5953

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 1 commit into from
Oct 8, 2021
Merged
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
80 changes: 76 additions & 4 deletions source/reference/operator/aggregation/getField.txt
Original file line number Diff line number Diff line change
Expand Up @@ -133,14 +133,14 @@ greater than ``200``:

.. code-block:: javascript

db.inventory.aggregate([
db.inventory.aggregate( [
{
$match:
{ $expr:
{ $gt: [ { $getField: "price.usd" }, 200 ] }
}
}
])
] )

The operation returns the following results:

Expand Down Expand Up @@ -171,14 +171,14 @@ products have a ``$price`` greater than ``200``:

.. code-block:: javascript

db.inventory.aggregate([
db.inventory.aggregate( [
{
$match:
{ $expr:
{ $gt: [ { $getField: {$literal: "$price" } }, 200 ] }
}
}
])
] )

The operation returns the following results:

Expand All @@ -190,3 +190,75 @@ The operation returns the following results:
{ _id: 4, item: 'leather boots', qty: 300, '$price': 249.99 }
]

Query a Field in a Sub-document
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

Create an ``inventory`` collection with the following documents:

.. code-block:: javascript

db.inventory.insertMany( [
{ "_id" : 1, "item" : "sweatshirt", "price.usd": 45.99,
"quantity": { "$large": 50, "$medium": 50, "$small": 25 }
},
{ "_id" : 2, "item" : "winter coat", "price.usd": 499.99,
"quantity": { "$large": 35, "$medium": 35, "$small": 35 }
},
{ "_id" : 3, "item" : "sun dress", "price.usd": 199.99,
"quantity": { "$large": 45, "$medium": 40, "$small": 5 }
},
{ "_id" : 4, "item" : "leather boots", "price.usd": 249.99,
"quantity": { "$large": 20, "$medium": 30, "$small": 40 }
},
{ "_id" : 5, "item" : "bow tie", "price.usd": 9.99,
"quantity": { "$large": 0, "$medium": 10, "$small": 75 }
}
] )

The following operation returns documents where the number of
``$small`` items is less than or equal to ``20``.

.. code-block:: javascript
:emphasize-lines: 6-8

db.inventory.aggregate( [
{ $match:
{ $expr:
{ $lte:
[
{ $getField:
{ field: { $literal: "$small" },
input: "$quantity"
}
},
20
]
}
}
}
] )

Use these operators to query the collection:

- The :expression:`$lte` operator finds values less than or equal to
20.
- :expression:`$getField` requires explicit ``field`` and ``input``
parameters because the ``$small`` field is part of a
sub-document.
- :expression:`$getField` uses :expression:`$literal` to evaluate
"``$small``", because the field name has a dollar sign (``$``) in it.

Example output:

.. code-block:: javascript
:copyable: false

[
{
_id: 3,
item: 'sun dress',
'price.usd': 199.99,
quantity: { '$large': 45, '$medium': 40, '$small': 5 }
}
]