|
| 1 | +.. _pymongo-specify-query: |
| 2 | + |
| 3 | +=============== |
| 4 | +Specify a Query |
| 5 | +=============== |
| 6 | + |
| 7 | +.. contents:: On this page |
| 8 | + :local: |
| 9 | + :backlinks: none |
| 10 | + :depth: 2 |
| 11 | + :class: singlecol |
| 12 | + |
| 13 | +.. facet:: |
| 14 | + :name: genre |
| 15 | + :values: reference |
| 16 | + |
| 17 | +.. meta:: |
| 18 | + :keywords: expressions, operations, read, write, filter |
| 19 | + |
| 20 | +Overview |
| 21 | +-------- |
| 22 | + |
| 23 | +In this guide, you can learn how to specify a query by using {+driver-short+}. |
| 24 | + |
| 25 | +You can refine the set of documents that a query returns by creating a |
| 26 | +**query filter**. A query filter is an expression that specifies the search |
| 27 | +criteria MongoDB uses to match documents in a read or write operation. |
| 28 | +In a query filter, you can prompt the driver to search for documents with an |
| 29 | +exact match to your query, or you can compose query filters to express more |
| 30 | +complex matching criteria. |
| 31 | + |
| 32 | +Sample Data |
| 33 | +~~~~~~~~~~~ |
| 34 | + |
| 35 | +The examples in this guide run operations on a collection called |
| 36 | +``fruits`` that contains the following documents: |
| 37 | + |
| 38 | +.. code-block:: json |
| 39 | + |
| 40 | + { "_id": 1, "name": "apples", "qty": 5, "rating": 3, "color": "red", "type": ["fuji", "honeycrisp"] }, |
| 41 | + { "_id": 2, "name": "bananas", "qty": 7, "rating": 4, "color": "yellow", "type": ["cavendish"] }, |
| 42 | + { "_id": 3, "name": "oranges", "qty": 6, "rating": 2, "type": ["naval", "mandarin"] }, |
| 43 | + { "_id": 4, "name": "pineapple", "qty": 3, "rating": 5, "color": "yellow" }, |
| 44 | + |
| 45 | +The following code example shows how to create a database and collection, then |
| 46 | +insert the sample documents into your collection: |
| 47 | + |
| 48 | +.. literalinclude:: /includes/specify-query/specify-queries.py |
| 49 | + :start-after: start-sample-data |
| 50 | + :end-before: end-sample-data |
| 51 | + :language: python |
| 52 | + :copyable: |
| 53 | + |
| 54 | +Exact Match |
| 55 | +----------- |
| 56 | + |
| 57 | +Literal value queries return documents with an exact match to your query filter. |
| 58 | + |
| 59 | +The following example specifies a query filter as a parameter to the ``find()`` |
| 60 | +method. The code returns all documents with a ``color`` field value of ``"yellow"``: |
| 61 | + |
| 62 | +.. io-code-block:: |
| 63 | + :copyable: |
| 64 | + |
| 65 | + .. input:: /includes/specify-query/specify-queries.py |
| 66 | + :start-after: start-find-exact |
| 67 | + :end-before: end-find-exact |
| 68 | + :language: python |
| 69 | + |
| 70 | + .. output:: |
| 71 | + |
| 72 | + {'_id': 2, 'name': 'bananas', 'qty': 7, 'rating': 4, 'color': 'yellow', 'type': ['cavendish']} |
| 73 | + {'_id': 4, 'name': 'pineapple', 'qty': 3, 'rating': 5, 'color': 'yellow'} |
| 74 | + |
| 75 | +.. tip:: Find All Documents |
| 76 | + |
| 77 | + To find all documents in a collection, call the ``find()`` method and pass it an |
| 78 | + empty query filter. The following example finds all documents in a |
| 79 | + collection: |
| 80 | + |
| 81 | + .. literalinclude:: /includes/specify-query/specify-queries.py |
| 82 | + :start-after: start-find-all |
| 83 | + :end-before: end-find-all |
| 84 | + :language: python |
| 85 | + :copyable: |
| 86 | + |
| 87 | +Comparison Operators |
| 88 | +-------------------- |
| 89 | + |
| 90 | +Comparison operators evaluate a document field value against a specified value |
| 91 | +in your query filter. The following is a list of common comparison operators: |
| 92 | + |
| 93 | +- ``$gt``: Greater than |
| 94 | +- ``$lte``: Less than or Equal |
| 95 | +- ``$ne``: Not equal |
| 96 | + |
| 97 | +To view a full list of comparison operators, see the :manual:`Comparison Query Operators |
| 98 | +</reference/operator/query-comparison/>` guide in the MongoDB Server manual. |
| 99 | + |
| 100 | +The following example specifies a comparison operator in a query filter as a |
| 101 | +parameter to the ``find()`` method. The code returns all documents with a |
| 102 | +``rating`` field value greater than ``2``: |
| 103 | + |
| 104 | +.. io-code-block:: |
| 105 | + :copyable: |
| 106 | + |
| 107 | + .. input:: /includes/specify-query/specify-queries.py |
| 108 | + :start-after: start-find-comparison |
| 109 | + :end-before: end-find-comparison |
| 110 | + :language: python |
| 111 | + |
| 112 | + .. output:: |
| 113 | + |
| 114 | + {'_id': 1, 'name': 'apples', 'qty': 5, 'rating': 3, 'color': 'red', 'type': ['fuji', 'honeycrisp']} |
| 115 | + {'_id': 2, 'name': 'bananas', 'qty': 7, 'rating': 4, 'color': 'yellow', 'type': ['cavendish']} |
| 116 | + {'_id': 4, 'name': 'pineapple', 'qty': 3, 'rating': 5, 'color': 'yellow'} |
| 117 | + |
| 118 | +Logical Operators |
| 119 | +----------------- |
| 120 | + |
| 121 | +Logical operators match documents by using logic applied to the results of two or |
| 122 | +more sets of expressions. The following is a list of logical operators: |
| 123 | + |
| 124 | +- ``$and``, which returns all documents that match the conditions of *all* clauses |
| 125 | +- ``$or``, which returns all documents that match the conditions of *one* clause |
| 126 | +- ``$nor``, which returns all documents that *do not* match the conditions of any clause |
| 127 | +- ``$not``, which returns all documents that *do not* match the expression |
| 128 | + |
| 129 | +To learn more about logical operators, see the :manual:`Logical Query Operators |
| 130 | +</reference/operator/query-logical/>` guide in the MongoDB Server manual. |
| 131 | + |
| 132 | +The following example specifies a logical operator in a query filter as a |
| 133 | +parameter to the ``find()`` method. The code returns all documents with a |
| 134 | +``qty`` field value greater than ``5`` **or** a ``color`` field value of |
| 135 | +``"yellow"``: |
| 136 | + |
| 137 | +.. io-code-block:: |
| 138 | + :copyable: |
| 139 | + |
| 140 | + .. input:: /includes/specify-query/specify-queries.py |
| 141 | + :start-after: start-find-logical |
| 142 | + :end-before: end-find-logical |
| 143 | + :language: python |
| 144 | + |
| 145 | + .. output:: |
| 146 | + |
| 147 | + {'_id': 2, 'name': 'bananas', 'qty': 7, 'rating': 4, 'color': 'yellow', 'type': ['cavendish']} |
| 148 | + {'_id': 3, 'name': 'oranges', 'qty': 6, 'rating': 2, 'type': ['naval', 'mandarin']} |
| 149 | + {'_id': 4, 'name': 'pineapple', 'qty': 3, 'rating': 5, 'color': 'yellow'} |
| 150 | + |
| 151 | +Array Operators |
| 152 | +--------------- |
| 153 | + |
| 154 | +Array operators match documents based on the value or quantity of elements in an |
| 155 | +array field. The following is a list of available array operators: |
| 156 | + |
| 157 | +- ``$all``, which returns documents with arrays that contain all elements in the query |
| 158 | +- ``$elemMatch``, which returns documents if an element in their array field matches all conditions in the query |
| 159 | +- ``$size``, which returns all documents with arrays of a specified size |
| 160 | + |
| 161 | +To learn more about array operators, see the :manual:`Array Query Operators |
| 162 | +</reference/operator/query-array/>` guide in the MongoDB Server manual. |
| 163 | + |
| 164 | +The following example specifies an array operator in a query filter as a |
| 165 | +parameter to the ``find()`` method. The code returns all documents with a |
| 166 | +``type`` array field containing ``2`` elements: |
| 167 | + |
| 168 | +.. io-code-block:: |
| 169 | + :copyable: |
| 170 | + |
| 171 | + .. input:: /includes/specify-query/specify-queries.py |
| 172 | + :start-after: start-find-array |
| 173 | + :end-before: end-find-array |
| 174 | + :language: python |
| 175 | + |
| 176 | + .. output:: |
| 177 | + |
| 178 | + {'_id': 1, 'name': 'apples', 'qty': 5, 'rating': 3, 'color': 'red', 'type': ['fuji', 'honeycrisp']} |
| 179 | + {'_id': 3, 'name': 'oranges', 'qty': 6, 'rating': 2, 'type': ['naval', 'mandarin']} |
| 180 | + |
| 181 | +Element Operators |
| 182 | +----------------- |
| 183 | + |
| 184 | +Element operators query data based on the presence or type of a field. |
| 185 | + |
| 186 | +To learn more about element operators, see the :manual:`Element Query Operators |
| 187 | +</reference/operator/query-element/>` guide in the MongoDB Server manual. |
| 188 | + |
| 189 | +The following example specifies an element operator in a query filter as a |
| 190 | +parameter to the ``find()`` method. The code returns all documents that have a |
| 191 | +``color`` field: |
| 192 | + |
| 193 | +.. io-code-block:: |
| 194 | + :copyable: |
| 195 | + |
| 196 | + .. input:: /includes/specify-query/specify-queries.py |
| 197 | + :start-after: start-find-element |
| 198 | + :end-before: end-find-element |
| 199 | + :language: python |
| 200 | + |
| 201 | + .. output:: |
| 202 | + |
| 203 | + {'_id': 1, 'name': 'apples', 'qty': 5, 'rating': 3, 'color': 'red', 'type': ['fuji', 'honeycrisp']} |
| 204 | + {'_id': 2, 'name': 'bananas', 'qty': 7, 'rating': 4, 'color': 'yellow', 'type': ['cavendish']} |
| 205 | + {'_id': 4, 'name': 'pineapple', 'qty': 3, 'rating': 5, 'color': 'yellow'} |
| 206 | + |
| 207 | +Evaluation Operators |
| 208 | +-------------------- |
| 209 | + |
| 210 | +Evaluation operators return data based on evaluations of either individual |
| 211 | +fields or the entire collection's documents. |
| 212 | + |
| 213 | +The following is a list of common evaluation operators: |
| 214 | + |
| 215 | +- ``$text``, which performs a text search on the documents |
| 216 | +- ``$regex``, which returns documents that match a specified regular expression |
| 217 | +- ``$mod``, which performs a modulo operation on the value of a field and |
| 218 | + returns documents where the remainder is a specified value |
| 219 | + |
| 220 | +To view a full list of evaluation operators, see the :manual:`Evaluation Query Operators |
| 221 | +</reference/operator/query-evaluation/>` guide in the MongoDB Server manual. |
| 222 | + |
| 223 | +The following example specifies an evaluation operator in a query filter as a |
| 224 | +parameter to the ``find()`` method. The code uses a regular expression to return |
| 225 | +all documents with a ``name`` field value that has at least two consecutive |
| 226 | +``"p"`` characters: |
| 227 | + |
| 228 | +.. io-code-block:: |
| 229 | + :copyable: |
| 230 | + |
| 231 | + .. input:: /includes/specify-query/specify-queries.py |
| 232 | + :start-after: start-find-evaluation |
| 233 | + :end-before: end-find-evaluation |
| 234 | + :language: python |
| 235 | + |
| 236 | + .. output:: |
| 237 | + |
| 238 | + {'_id': 1, 'name': 'apples', 'qty': 5, 'rating': 3, 'color': 'red', 'type': ['fuji', 'honeycrisp']} |
| 239 | + {'_id': 4, 'name': 'pineapple', 'qty': 3, 'rating': 5, 'color': 'yellow'} |
| 240 | + |
| 241 | +Additional Information |
| 242 | +---------------------- |
| 243 | + |
| 244 | +To learn more about querying documents, see the :manual:`Query Documents |
| 245 | +</tutorial/query-documents/>` guide in the MongoDB Server manual. |
| 246 | + |
| 247 | +To learn more about retrieving documents with PyMongo, see |
| 248 | +:ref:`pymongo-retrieve`. |
| 249 | + |
| 250 | +API Documentation |
| 251 | +~~~~~~~~~~~~~~~~~ |
| 252 | + |
| 253 | +To learn more about any of the methods or types discussed in this |
| 254 | +guide, see the following API documentation: |
| 255 | + |
| 256 | +- `find() <{+api-root+}pymongo/collection.html#pymongo.collection.Collection.find>`__ |
0 commit comments