|
| 1 | +:template: guide |
| 2 | + |
| 3 | +.. _guide-search-compound: |
| 4 | + |
| 5 | +=================================== |
| 6 | +Querying with the Compound Operator |
| 7 | +=================================== |
| 8 | + |
| 9 | +.. default-domain:: mongodb |
| 10 | + |
| 11 | +.. contents:: On this page |
| 12 | + :local: |
| 13 | + :backlinks: none |
| 14 | + :depth: 1 |
| 15 | + :class: singlecol |
| 16 | + |
| 17 | +Overview |
| 18 | +-------- |
| 19 | + |
| 20 | +.. short-description:: |
| 21 | + |
| 22 | + Retrieve documents in MongoDB with a query on multiple fields. |
| 23 | + |
| 24 | +In this guide, you will learn how to use multiple search operators in |
| 25 | +a compound query using the aggregation pipeline. |
| 26 | + |
| 27 | +.. time:: 15 |
| 28 | + |
| 29 | +What You'll Need |
| 30 | +---------------- |
| 31 | + |
| 32 | +- A MongoDB account. See :ref:`guides-create-atlas-account`. |
| 33 | +- An Atlas cluster. See :ref:`guides-create-a-cluster`. |
| 34 | +- Sample datasets :ref:`loaded into your cluster <guides-load-sample-data>`. |
| 35 | +- A search index with default settings and dynamic indexing. See |
| 36 | + :ref:`guides-search-dynamic`. |
| 37 | + |
| 38 | +Procedure |
| 39 | +--------- |
| 40 | + |
| 41 | +.. procedure:: |
| 42 | + :style: normal |
| 43 | + |
| 44 | + .. include:: /includes/navigate_to_collections.rst |
| 45 | + |
| 46 | + .. step:: In the right-hand pane, click ``sample_supplies``, |
| 47 | + then click the ``sales`` collection. |
| 48 | + |
| 49 | + .. step:: Run a query on your index using the Aggregation Pipeline. |
| 50 | + |
| 51 | + .. procedure:: |
| 52 | + :style: connected |
| 53 | + |
| 54 | + .. step:: Click |
| 55 | + the :guilabel:`Aggregation` tab. |
| 56 | + |
| 57 | + .. step:: If you do not see a pipeline with an initial |
| 58 | + stage window in the right pane, click |
| 59 | + :guilabel:`Create New` and select :guilabel:`Pipeline`. |
| 60 | + |
| 61 | + .. step:: In the stage window, click on the dropdown menu |
| 62 | + labeled :guilabel:`Select...` and select the |
| 63 | + :guilabel:`$search` stage. |
| 64 | + |
| 65 | + .. step:: Replace the placeholder code with the following |
| 66 | + ``compound`` operator: |
| 67 | + |
| 68 | + .. code-block:: json |
| 69 | + |
| 70 | + { |
| 71 | + "compound": { |
| 72 | + "filter": [{ |
| 73 | + "text": { |
| 74 | + "query": "Online", |
| 75 | + "path": "purchaseMethod" |
| 76 | + } |
| 77 | + }], |
| 78 | + "should": [{ |
| 79 | + "text": { |
| 80 | + "query": "notepad", |
| 81 | + "path": "items" |
| 82 | + } |
| 83 | + }] |
| 84 | + } |
| 85 | + } |
| 86 | + |
| 87 | + .. step:: Review the results of the compound search in the |
| 88 | + right-hand pane. Your results should include the fields listed |
| 89 | + in the following sample document. |
| 90 | + |
| 91 | + .. note:: |
| 92 | + |
| 93 | + The fields will not always appear in the same order |
| 94 | + for every returned document. |
| 95 | + |
| 96 | + .. code-block:: json |
| 97 | + :copyable: false |
| 98 | + |
| 99 | + items: Array |
| 100 | + storeLocation: "Denver" |
| 101 | + customer: Object |
| 102 | + couponUsed: false |
| 103 | + purchaseMethod: "Online" |
| 104 | + _id: ObjectID('5bd761dcae323e45a93cd06e') |
| 105 | + saleDate: 2013-11-22T18:49:45.212+00:00 |
| 106 | + |
| 107 | + You can expand ``Array`` and ``Object`` fields to view their |
| 108 | + contents by clicking on them. |
| 109 | + |
| 110 | + Due to the ``filter`` operator, only records with a |
| 111 | + ``purchaseMethod`` value of ``Online`` appear. |
| 112 | + Additionally, due to the ``should`` operator, |
| 113 | + records with an ``items`` value of ``notepad`` score |
| 114 | + higher than those without. |
| 115 | + |
| 116 | + You now have a ``$search`` aggregation stage with a compound |
| 117 | + search definition. |
| 118 | + |
| 119 | +Summary |
| 120 | +------- |
| 121 | + |
| 122 | +If you successfully completed the procedure in this guide, you have |
| 123 | +created an Atlas Search aggregation stage with a compound operator, |
| 124 | +and have seen how different clauses in the operator interact to |
| 125 | +focus your search results across multiple axes. In the next guide, you |
| 126 | +will learn how to group search results using facets. |
| 127 | + |
| 128 | +.. guide-next:: |
0 commit comments