@@ -287,55 +287,70 @@ chart.
287
287
288
288
Query Bar
289
289
---------
290
+
291
+ The :guilabel:`Query` bar supports more complex queries than
292
+ the filter panel. Additionally, you can use the query bar to create
293
+ :manual:`aggregation pipelines </core/aggregation-pipeline/>` to
294
+ process your data before it is rendered.
295
+
296
+ Filter Data using the Query Bar
297
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
290
298
291
299
To filter data using the :guilabel:`Query` bar:
292
300
293
301
1. In the :guilabel:`Query` bar, input a filter document. Use the
294
302
same syntax used in the query portion of the
295
303
:manual:`db.collection.find()
296
- </reference/method/db.collection.find/>` method.
304
+ </reference/method/db.collection.find/>` method. Your filter document
305
+ must be in curly brackets.
297
306
298
307
#. Click :guilabel:`Apply`.
299
308
300
- Considerations
301
- ~~~~~~~~~~~~~~
309
+ .. admonition:: Considerations
310
+ :class: note
302
311
303
- - Filters on large collections may have performance issues if there
304
- are not appropriate :manual:`indexes <indexes>` on the collection .
312
+ - Filters on large collections may encounter performance issues if
313
+ the collection is not appropriately :manual:`indexed <indexes>`.
305
314
306
- - The date functions utilized in the |charts| query bar are consistent
307
- and compatible with the date functions used in the
308
- :manual:`mongo shell </mongo>`. As a result, you can use:
315
+ - The date functions utilized in the |charts| query bar are
316
+ consistent and compatible with the date functions used in the
317
+ :manual:`mongo shell </mongo>`. As a result, you can use:
309
318
310
- - ``new Date()``,
311
- - ``ISODate()``, or
312
- - ``new ISODate()``.
319
+ - ``new Date()``,
320
+ - ``ISODate()``, or
321
+ - ``new ISODate()``.
313
322
314
- |
323
+ |
315
324
316
- The ``Date()`` function (as opposed to the ``new Date()``
317
- constructor) returns the current date as a string, so it cannot be
318
- used for querying dates in |charts-short|.
325
+ The ``Date()`` function (as opposed to the ``new Date()``
326
+ constructor) returns the current date as a string, so it cannot be
327
+ used for querying dates in |charts-short|.
319
328
320
- Examples
321
- ~~~~~~~~
329
+ Element Query Example
330
+ `````````````````````
322
331
323
- Comparison Query Filter
324
- ```````````````````````
332
+ The following chart shows the average Metacritic ratings of movies
333
+ over time :ref:`binned <charts-bin-data>` by 5 year periods.
325
334
326
- The following chart shows the average runtime of movies by
327
- genre. The filter of ``{runtime: {$gte: 120 }}`` means that we only
328
- include movies which have a runtime greater than or equal to 120
329
- minutes.
330
-
331
- .. figure:: /images/charts/charts-dashboard-filter-2.png
335
+ .. figure:: /images/charts/query-bar-comparison.png
332
336
:figwidth: 720px
333
- :alt: Charts dashboard filter
337
+ :alt: Example query comparison
338
+
339
+ The chart utilizes the following query:
340
+
341
+ .. code-block:: javascript
342
+
343
+ { 'writers.1': { $exists: true }}
344
+
345
+ ``writers`` is an array where each element is a writer who
346
+ contributed to the movie. This filter ensures that only documents with
347
+ at least two writers are factored into the mean Metacritic rating by
348
+ checking that the second array element exists.
334
349
335
350
.. _regex-filter:
336
351
337
- Regular Expression (RegEx) Query
338
- ````````````````````````````````
352
+ Regular Expression (RegEx) Query Example
353
+ ````````````````````````````````````````
339
354
340
355
.. _`regular expression`: :manual:`
341
356
@@ -357,12 +372,103 @@ the letter ``A``, you would write the following in the
357
372
358
373
.. code-block:: javascript
359
374
360
- { "job " : { $regex : "^A" } }
375
+ { "jobs " : { $regex : "^A" } }
361
376
362
377
To find all documents where the ``jobs`` field begins with the
363
378
letter ``A`` or ``a``, you would write the following in the
364
379
:guilabel:`Query` bar:
365
380
366
381
.. code-block:: javascript
367
382
368
- { "job" : { $regex : "^A", $options : "i" } }
383
+ { "jobs" : { $regex : "^A", $options : "i" } }
384
+
385
+ Date Query Example
386
+ ``````````````````
387
+
388
+ The following chart shows total sale amounts from an office supply
389
+ company, categorized by purchase method:
390
+
391
+ .. figure:: /images/charts/query-bar-date.png
392
+ :figwidth: 720px
393
+ :alt: Example date query
394
+
395
+ The chart utilizes the following query:
396
+
397
+ .. code-block:: javascript
398
+
399
+ {
400
+ $and: [
401
+ {
402
+ saleDate: { $gte: new Date("2017-01-01") }
403
+ },
404
+ {
405
+ 'items.4': { $exists: true }
406
+ } ]
407
+ }
408
+
409
+ Each document in the collection represents a single sale. ``items`` is
410
+ an array where each element is an item purchased during a sale.
411
+
412
+ This query restricts the documents shown to only those with
413
+ a ``saleDate`` equal to or more recent than ``January 1, 2017`` with at
414
+ least 5 elements in the ``items`` array.
415
+
416
+ Create Aggregation Pipelines
417
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~
418
+
419
+ :manual:`Aggregation pipelines </core/aggregation-pipeline/>` transform
420
+ your documents into an aggregated set of results. In |charts|,
421
+ aggregation pipelines are commonly used to visualize new fields created
422
+ from calculated results of pre-existing fields, but also have many other
423
+ applications.
424
+
425
+ To create an aggregation pipeline in the :guilabel:`Query` bar:
426
+
427
+ 1. In the :guilabel:`Query` bar, input an aggregation pipeline. Your
428
+ pipeline must be in square brackets.
429
+
430
+ #. Click :guilabel:`Apply` to execute your pipeline.
431
+
432
+ Aggregation Pipeline Example
433
+ ````````````````````````````
434
+
435
+ The following chart shows total sale amounts from an office supply
436
+ company, categorized by store location. The chart utilizes the
437
+ following aggregation pipeline in the :guilabel:`Query` bar:
438
+
439
+ .. code-block:: javascript
440
+
441
+ [
442
+ {
443
+ $unwind: "$items"
444
+ },
445
+ {
446
+ $addFields: {
447
+ saleAmount: {
448
+ $multiply: [ "$items.price", "$items.quantity" ]
449
+ }
450
+ }
451
+ }
452
+ ]
453
+
454
+ This aggregation pipeline processes the collection data using
455
+ the following stages:
456
+
457
+ 1. The :pipeline:`$unwind` stage unwinds the ``items`` array and
458
+ outputs a new document for each item in the array. Each element
459
+ in the ``items`` array contains a single item sold during a
460
+ transaction.
461
+
462
+ #. The :pipeline:`$addFields` stage adds a new field to the
463
+ documents called ``saleAmount``. The :expression:`$multiply`
464
+ expression sets the value of ``saleAmount`` to the product of
465
+ ``items.price`` and ``items.quantity``. You can see this
466
+ new field highlighted in the following screenshot:
467
+
468
+ .. figure:: /images/charts/query-bar-agg-example.png
469
+ :figwidth: 720px
470
+ :alt: Example Aggregation Pipeline
471
+
472
+ Once the data has been processed using the pipeline, the
473
+ chart displays the the :guilabel:`Sum` of all
474
+ ``saleAmounts`` categorized by store location.
0 commit comments