|
10 | 10 | :depth: 1 |
11 | 11 | :class: singlecol |
12 | 12 |
|
| 13 | +Definition |
| 14 | +---------- |
| 15 | + |
13 | 16 | .. query:: $gt |
14 | 17 |
|
15 | | - *Syntax*: ``{field: {$gt: value} }`` |
| 18 | + *Syntax*: ``{ field: { $gt: value } }`` |
16 | 19 |
|
17 | 20 | :query:`$gt` selects those documents where the value of the |
18 | 21 | ``field`` is greater than (i.e. ``>``) the specified ``value``. |
19 | 22 |
|
20 | 23 | .. include:: /includes/fact-type-bracketing.rst |
21 | 24 |
|
22 | | - Consider the following example: |
| 25 | +Examples |
| 26 | +-------- |
| 27 | + |
| 28 | +The following examples use the ``inventory`` collection. Create the |
| 29 | +collection: |
| 30 | + |
| 31 | +.. include:: /includes/examples-create-inventory.rst |
| 32 | + |
| 33 | +Match Document Fields |
| 34 | +~~~~~~~~~~~~~~~~~~~~~ |
| 35 | + |
| 36 | +Select all documents in the ``inventory`` collection where ``quantity`` |
| 37 | +is greater than ``20``: |
| 38 | + |
| 39 | +.. code-block:: javascript |
| 40 | + |
| 41 | + db.inventory.find( { quantity: { $gt: 20 } } ) |
| 42 | + |
| 43 | +Example output: |
| 44 | + |
| 45 | +.. code-block:: javascript |
| 46 | + |
| 47 | + { |
| 48 | + _id: ObjectId("61ba25cbfe687fce2f042414"), |
| 49 | + item: 'nuts', |
| 50 | + quantity: 30, |
| 51 | + carrier: { name: 'Shipit', fee: 3 } |
| 52 | + }, |
| 53 | + { |
| 54 | + _id: ObjectId("61ba25cbfe687fce2f042415"), |
| 55 | + item: 'bolts', |
| 56 | + quantity: 50, |
| 57 | + carrier: { name: 'Shipit', fee: 4 } |
| 58 | + } |
23 | 59 |
|
24 | | - .. code-block:: javascript |
| 60 | +Perform an Update Based on Embedded Document Fields |
| 61 | +~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ |
25 | 62 |
|
26 | | - db.inventory.find( { qty: { $gt: 20 } } ) |
| 63 | +The following example sets the ``price`` field based on a :query:`$gt` |
| 64 | +comparison against a field in an embedded document. |
27 | 65 |
|
28 | | - This query will select all documents in the ``inventory`` collection |
29 | | - where the ``qty`` field value is greater than ``20``. |
| 66 | +.. code-block:: javascript |
30 | 67 |
|
31 | | - Consider the following example that uses the :query:`$gt` |
32 | | - operator with a field from an embedded document: |
| 68 | + db.inventory.updateOne( |
| 69 | + { "carrier.fee": { $gt: 2 } }, { $set: { "price": 9.99 } } |
| 70 | + ) |
33 | 71 |
|
34 | | - .. code-block:: javascript |
| 72 | +Example output: |
35 | 73 |
|
36 | | - db.inventory.update( { "carrier.fee": { $gt: 2 } }, { $set: { price: 9.99 } } ) |
| 74 | +.. code-block:: javascript |
37 | 75 |
|
38 | | - This :method:`~db.collection.update()` operation will set |
39 | | - the value of the ``price`` field in the first document found containing the |
40 | | - embedded document ``carrier`` whose ``fee`` field value is |
41 | | - greater than ``2``. |
| 76 | + { |
| 77 | + _id: ObjectId("61ba3ec9fe687fce2f042417"), |
| 78 | + item: 'nuts', |
| 79 | + quantity: 30, |
| 80 | + carrier: { name: 'Shipit', fee: 3 }, |
| 81 | + price: 9.99 |
| 82 | + }, |
| 83 | + { |
| 84 | + _id: ObjectId("61ba3ec9fe687fce2f042418"), |
| 85 | + item: 'bolts', |
| 86 | + quantity: 50, |
| 87 | + carrier: { name: 'Shipit', fee: 4 } |
| 88 | + }, |
| 89 | + { |
| 90 | + _id: ObjectId("61ba3ec9fe687fce2f042419"), |
| 91 | + item: 'washers', |
| 92 | + quantity: 10, |
| 93 | + carrier: { name: 'Shipit', fee: 1 } |
| 94 | + } |
42 | 95 |
|
43 | | - To set the value of the ``price`` field in *all* documents containing the |
44 | | - embedded document ``carrier`` whose ``fee`` field value is greater than ``2``, |
45 | | - specify the ``multi:true`` option in the :method:`~db.collection.update()` method: |
| 96 | +This :method:`~db.collection.updateOne()` operation searches for an |
| 97 | +embedded document, ``carrier``, with a subfield named ``fee``. It sets |
| 98 | +``{ price: 9.99 }`` in the first document it finds where ``fee`` has a |
| 99 | +value greater than 2. |
46 | 100 |
|
47 | | - .. code-block:: javascript |
| 101 | +To set the value of the ``price`` field in *all* documents where |
| 102 | +``carrier.fee`` is greater than 2, use |
| 103 | +:method:`~db.collection.updateMany()`. |
48 | 104 |
|
49 | | - db.inventory.update( |
50 | | - { "carrier.fee": { $gt: 2 } }, |
51 | | - { $set: { price: 9.99 } }, |
52 | | - { multi: true } |
53 | | - ) |
| 105 | +.. seealso:: |
54 | 106 |
|
55 | | - .. seealso:: |
| 107 | + - :method:`~db.collection.find()` |
| 108 | + - :update:`$set` |
56 | 109 |
|
57 | | - - :method:`~db.collection.find()` |
58 | | - - :method:`~db.collection.update()` |
59 | | - - :update:`$set` |
|
0 commit comments