@@ -108,6 +108,173 @@ How Does the Compass Query Compare to MongoDB and SQL Queries?
108
108
SELECT * FROM article
109
109
WHERE author = "Joe Bloggs";
110
110
111
+ Examples
112
+ --------
113
+
114
+ .. class:: example
115
+
116
+ The following examples use the JSON documents below as sample data.
117
+ To import this sample data to your MongoDB deployment with |compass|:
118
+
119
+ 1. Copy the array of documents below by clicking :guilabel:`Copy`.
120
+
121
+ .. code-block:: JSON
122
+
123
+ [
124
+ {
125
+ "_id":{"$oid":"5a9427648b0beebeb69579cc"},
126
+ "name":"Andrea Le",
127
+
128
+ "version":5,
129
+ "scores":[85, 95, 75],
130
+ "dateCreated":{"$date":"2003-03-26"}
131
+ },
132
+ {
133
+ "_id":{"$oid":"5a9427648b0beebeb69579cf"},
134
+ "name":"Greg Powell",
135
+
136
+ "version":1,
137
+ "scores":[65, 75, 80],
138
+ "dateCreated":{"$date":"1999-02-10"}
139
+ }
140
+ ]
141
+
142
+ 2. In |compass-short|, use the left navigation panel to select the
143
+ database and the collection you want to import the data to.
144
+
145
+ #. Click the :guilabel:`Documents` tab.
146
+
147
+ #. Click :guilabel:`Add Data` and select :guilabel:`Insert Document`.
148
+
149
+ #. Ensure that :guilabel:`View` is set to JSON, or ``{}``, and paste
150
+ the copied JSON documents in the field.
151
+
152
+ #. Click :guilabel:`Insert`.
153
+
154
+ .. note::
155
+
156
+ If you do not have a MonogDB deployment or if you would like to
157
+ query a large sample data set, see
158
+ :atlas:`Sample Data for Atlas Clusters</sample-data/>` for
159
+ instructions on creating a free-tier cluster with sample data. Note
160
+ that the examples below are intended to filter the sample JSON
161
+ documents provided on this page and may not properly filter another
162
+ sample data set.
163
+
164
+ .. tabs::
165
+
166
+ .. tab:: Filter for a Match
167
+ :tabid: match
168
+
169
+ The following :ref:`query filter <query-bar-filter>` uses the
170
+ :manual:`$eq operator </reference/operator/query/eq/>` to find
171
+ all documents where ``name`` is "Andrea Le" .
172
+
173
+ .. code-block:: shell
174
+
175
+ { name: { $eq: "Andrea Le" } }
176
+
177
+ The query returns the following document because the ``name``
178
+ field value in the document is an exact match.
179
+
180
+ .. code-block:: JSON
181
+ :copyable: false
182
+
183
+ {
184
+ "_id":ObjectId("5a9427648b0beebeb69579cc"),
185
+ "name":"Andrea Le",
186
+
187
+ "version":5,
188
+ "scores":[85, 95, 75],
189
+ "dateCreated":2003-03-26T00:00:00.000+00:00
190
+ }
191
+
192
+ .. tab:: Filter by Comparison
193
+ :tabid: compare
194
+
195
+ The following :ref:`query filter <query-bar-filter>` uses the
196
+ :manual:`$lte operator </reference/operator/query/lte/>` to
197
+ find all documents where ``version`` is less than or equal
198
+ to ``4``.
199
+
200
+ .. code-block:: shell
201
+
202
+ { version: { $lte: 4 } }
203
+
204
+ The query returns the following document because the value of
205
+ the ``version`` field in the document is less than ``4``.
206
+
207
+ .. code-block:: JSON
208
+ :copyable: false
209
+
210
+ {
211
+ "_id":ObjectId("5a9427648b0beebeb69579cf"),
212
+ "name":"Greg Powell",
213
+
214
+ "version":1,
215
+ "scores":[65, 75, 80],
216
+ "dateCreated":1999-02-10T00:00:00.000+00:00
217
+ }
218
+
219
+ .. tab:: Filter by Date
220
+ :tabid: date
221
+
222
+ The following :ref:`query filter <query-bar-filter>` uses the
223
+ :manual:`$gt operator </reference/operator/query/gt/>` and
224
+ :manual:`Date() method </reference/method/Date/>` to find all
225
+ documents where the ``dateCreated`` field value is later than
226
+ June 22nd, 2000.
227
+
228
+ .. code-block:: shell
229
+
230
+ { dateCreated: { $gt: Date('2000-06-22') } }
231
+
232
+ The query returns the following document because the value of
233
+ the ``dateCreated`` field is after June 22, 2000.
234
+
235
+ .. code-block:: JSON
236
+ :copyable: false
237
+
238
+ {
239
+ "_id":ObjectId("5a9427648b0beebeb69579cc"),
240
+ "name":"Andrea Le",
241
+
242
+ "version":1,
243
+ "scores":[85, 95, 75],
244
+ "dateCreated":2003-03-26T00:00:00.000+00:00
245
+ }
246
+
247
+ .. tab:: Filter by Elements in an Array
248
+ :tabid: array
249
+
250
+ The following :ref:`query filter <query-bar-filter>` uses the
251
+ :manual:`$elemMatch operator </reference/operator/query/elemMatch/>` to find all documents where at least one value in
252
+ the ``scores`` array is greater than 80 and less than ``90``
253
+
254
+ .. code-block:: shell
255
+
256
+ { scores: { $elemMatch: { $gt: 80, $lt: 90 } } }
257
+
258
+ The query returns the following document because one of the
259
+ values in the scores array is ``85``, which matches the
260
+ ``$elemMatch`` criteria.
261
+
262
+ .. code-block:: JSON
263
+ :copyable: false
264
+
265
+ {
266
+ "_id":ObjectId("5a9427648b0beebeb69579cc"),
267
+ "name":"Andrea Le",
268
+
269
+ "version":1,
270
+ "scores":[85, 95, 75],
271
+ "dateCreated":2003-03-26T00:00:00.000+00:00
272
+ }
273
+
274
+ For more query examples, see
275
+ :manual:`Query Documents </tutorial/query-documents/>`
276
+ in the MongoDB manual.
277
+
111
278
.. class:: hidden
112
279
113
280
.. toctree::
0 commit comments