|
| 1 | +=============== |
| 2 | +Specify a Query |
| 3 | +=============== |
| 4 | + |
| 5 | +.. default-domain:: mongodb |
| 6 | + |
| 7 | +.. contents:: On this page |
| 8 | + :local: |
| 9 | + :backlinks: none |
| 10 | + :depth: 1 |
| 11 | + :class: singlecol |
| 12 | + |
| 13 | +Overview |
| 14 | +-------- |
| 15 | + |
| 16 | +Most CRUD operations allow you to narrow the set of matched documents by |
| 17 | +specifying matching criteria in a **query document**. Query documents |
| 18 | +contain one or more query operators that apply to specific fields which |
| 19 | +determine which documents to include in the result set. |
| 20 | + |
| 21 | +In this page, we cover the following query operators with |
| 22 | +examples on how to use them: |
| 23 | + |
| 24 | +- :ref:`Comparison Operators <query-comparison>` |
| 25 | +- :ref:`Logical Operators <query-logical>` |
| 26 | +- :ref:`Array Operators <query-arrays>` |
| 27 | +- :ref:`Element Operators <query-elements>` |
| 28 | +- :ref:`Evaluation Operators <query-evaluation>` |
| 29 | + |
| 30 | +The examples in this guide use the following documents in the |
| 31 | +``paint_purchases`` collection: |
| 32 | + |
| 33 | +.. code-block:: json |
| 34 | + |
| 35 | + { "_id": 1, "color": "red", "qty": 9, "vendor": ["A", "E"] } |
| 36 | + { "_id": 2, "color": "purple", "qty": 8, "vendor": ["B", "D", "F"], "rating": 5 } |
| 37 | + { "_id": 3, "color": "blue", "qty": 5, "vendor": ["A", "E"] } |
| 38 | + { "_id": 4, "color": "white", "qty": 6, "vendor": ["D"], "rating": 9 } |
| 39 | + { "_id": 5, "color": "yellow", "qty": 4, "vendor": ["A", "B"] } |
| 40 | + { "_id": 6, "color": "pink", "qty": 3, "vendor": ["C"] } |
| 41 | + { "_id": 7, "color": "green", "qty": 8, "vendor": ["C", "E"], "rating": 7 } |
| 42 | + { "_id": 8, "color": "black", "qty": 7, "vendor": ["A", "C", "D"] } |
| 43 | + |
| 44 | +.. _query-comparison: |
| 45 | + |
| 46 | +Comparison Operators |
| 47 | +-------------------- |
| 48 | + |
| 49 | +Comparison operators query data based on comparisons with values in a |
| 50 | +collection. Common comparison operators include ``gt()`` for "greater |
| 51 | +than" comparisons, ``lte()`` for "less than or equal to" comparisons, |
| 52 | +and ``ne()`` for "not equal to " comparisons. |
| 53 | + |
| 54 | +The following example uses the ``Filters.gt()`` method to match all |
| 55 | +documents where the value of ``qty`` is greater than ``7`` in the |
| 56 | +``paint_purchases`` collection: |
| 57 | + |
| 58 | +.. literalinclude:: /includes/fundamentals/code-snippets/Query.java |
| 59 | + :language: java |
| 60 | + :dedent: |
| 61 | + :start-after: begin comparisonFilter |
| 62 | + :end-before: end comparisonFilter |
| 63 | + |
| 64 | +The following shows the output of the query using the operator specified |
| 65 | +above: |
| 66 | + |
| 67 | +.. code-block:: json |
| 68 | + :copyable: false |
| 69 | + |
| 70 | + { "_id": 1, "color": "red", "qty": 9, "vendor": ["A", "E"] } |
| 71 | + { "_id": 2, "color": "purple", "qty": 8, "vendor": ["B", "D", "F"], "rating": 5 } |
| 72 | + { "_id": 7, "color": "green", "qty": 8, "vendor": ["C", "E"], "rating": 7 } |
| 73 | + |
| 74 | +.. _query-logical: |
| 75 | + |
| 76 | +Logical Operators |
| 77 | +----------------- |
| 78 | + |
| 79 | +Logical operators query data using logic applied to the results of |
| 80 | +field-level operators. Common logical operators include ``and()`` where |
| 81 | +all operators must be true, and ``or()`` where at least one of the |
| 82 | +operators must be true. |
| 83 | + |
| 84 | +The following example uses the ``Filters.and()`` method to match |
| 85 | +documents where the value of ``qty`` is less than or equal to ``5`` and |
| 86 | +the value of ``color`` is not ``"pink"`` in the ``paint_purchases`` |
| 87 | +collection: |
| 88 | + |
| 89 | +.. literalinclude:: /includes/fundamentals/code-snippets/Query.java |
| 90 | + :language: java |
| 91 | + :dedent: |
| 92 | + :start-after: begin logicalFilter |
| 93 | + :end-before: end logicalFilter |
| 94 | + |
| 95 | +The following shows the output of the query using the operator specified |
| 96 | +above: |
| 97 | + |
| 98 | +.. code-block:: json |
| 99 | + :copyable: false |
| 100 | + |
| 101 | + { "_id": 3, "color": "blue", "qty": 5, "vendor": ["A", "E"] } |
| 102 | + { "_id": 5, "color": "yellow", "qty": 4, "vendor": ["A", "B"] } |
| 103 | + |
| 104 | +.. _query-arrays: |
| 105 | + |
| 106 | +Array Operators |
| 107 | +--------------- |
| 108 | + |
| 109 | +Array operators query data based on the value or quantity of elements in |
| 110 | +an array field. |
| 111 | + |
| 112 | +The following example uses the ``Filters.size()`` method to match |
| 113 | +documents where the size of ``vendor`` list is ``3`` in the |
| 114 | +``paint_purchases`` collection: |
| 115 | + |
| 116 | +.. literalinclude:: /includes/fundamentals/code-snippets/Query.java |
| 117 | + :language: java |
| 118 | + :dedent: |
| 119 | + :start-after: begin arrayFilter |
| 120 | + :end-before: end arrayFilter |
| 121 | + |
| 122 | +The following shows the output of the query using the operator specified |
| 123 | +above: |
| 124 | + |
| 125 | +.. code-block:: json |
| 126 | + :copyable: false |
| 127 | + |
| 128 | + { "_id": 2, "color": "purple", "qty": 8, "vendor": ["B", "D", "F"], "rating": 5 } |
| 129 | + { "_id": 8, "color": "black", "qty": 7, "vendor": ["A", "C", "D"] } |
| 130 | + |
| 131 | +.. _query-elements: |
| 132 | + |
| 133 | +Element Operators |
| 134 | +----------------- |
| 135 | + |
| 136 | +Element operators query data based on the presence or type of a field. |
| 137 | + |
| 138 | +The following example uses the ``Filters.exists()`` method to match |
| 139 | +documents that have a ``rating`` in the ``paint_purchases`` collection: |
| 140 | + |
| 141 | +.. literalinclude:: /includes/fundamentals/code-snippets/Query.java |
| 142 | + :language: java |
| 143 | + :dedent: |
| 144 | + :start-after: begin elementFilter |
| 145 | + :end-before: end elementFilter |
| 146 | + |
| 147 | +The following shows the output of the query using the operator specified |
| 148 | +above: |
| 149 | + |
| 150 | +.. code-block:: json |
| 151 | + :copyable: false |
| 152 | + |
| 153 | + { "_id": 2, "color": "purple", "qty": 8, "vendor": ["B", "D", "F"], "rating": 5 } |
| 154 | + { "_id": 4, "color": "white", "qty": 6, "vendor": ["D"], "rating": 9 } |
| 155 | + { "_id": 7, "color": "green", "qty": 8, "vendor": ["C", "E"], "rating": 7 } |
| 156 | + |
| 157 | +.. _query-evaluation: |
| 158 | + |
| 159 | +Evaluation Operators |
| 160 | +-------------------- |
| 161 | + |
| 162 | +Evaluation operators query data on higher level logic, like regex |
| 163 | +and text searches. |
| 164 | + |
| 165 | +The following example uses the ``Filters.regex()`` method to match |
| 166 | +documents that have a ``color`` ending with the letter ``"k"`` in the |
| 167 | +``paint_purchases`` collection: |
| 168 | + |
| 169 | +.. literalinclude:: /includes/fundamentals/code-snippets/Query.java |
| 170 | + :language: java |
| 171 | + :dedent: |
| 172 | + :start-after: begin evaluationFilter |
| 173 | + :end-before: end evaluationFilter |
| 174 | + |
| 175 | +The following shows the output of the query using the operator specified |
| 176 | +above: |
| 177 | + |
| 178 | +.. code-block:: json |
| 179 | + :copyable: false |
| 180 | + |
| 181 | + { "_id": 6, "color": "pink", "qty": 3, "vendor": ["C"] } |
| 182 | + { "_id": 8, "color": "black", "qty": 7, "vendor": ["A", "C", "D"] } |
| 183 | + |
| 184 | +See the following documentation for more information about the operators |
| 185 | +in this guide: |
| 186 | + |
| 187 | +- :manual:`Query Operators </reference/operator/query/>` |
| 188 | +- :manual:`Comparison Operators </reference/operator/query-comparison/>` |
| 189 | +- :manual:`Logical Operators </reference/operator/query-logical/>` |
| 190 | +- :manual:`Array Operators </reference/operator/query-array/>` |
| 191 | +- :manual:`Element Operators </reference/operator/query-element/>` |
| 192 | +- :manual:`Evaluation Operators </reference/operator/query-evaluation/>` |
0 commit comments