|
| 1 | +.. _prompt-natural-language-agg: |
| 2 | + |
| 3 | +===================================== |
| 4 | +Prompt a Natural Language Aggregation |
| 5 | +===================================== |
| 6 | + |
| 7 | +.. default-domain:: mongodb |
| 8 | + |
| 9 | +.. contents:: On this page |
| 10 | + :local: |
| 11 | + :backlinks: none |
| 12 | + :depth: 1 |
| 13 | + |
| 14 | +You can use MongoDB Compass to generate aggregation queries using natural |
| 15 | +language. Compass uses AI to generate aggregations based on prompts |
| 16 | +you provide. Querying with natural language can be a helpful starting |
| 17 | +point and assist you in learning to write MongoDB queries. |
| 18 | + |
| 19 | +About this Task |
| 20 | +--------------- |
| 21 | + |
| 22 | +.. note:: |
| 23 | + |
| 24 | + .. include:: /includes/fact-natural-language-query.rst |
| 25 | + |
| 26 | +- You can also provide natural language prompts on the |
| 27 | + :ref:`documents tab <prompt-natural-language-query>`. |
| 28 | + |
| 29 | +- This page uses the :ref:`sample_mflix.movies <sample-mflix>` |
| 30 | + collection from the Atlas sample dataset. |
| 31 | + |
| 32 | +Before you Begin |
| 33 | +---------------- |
| 34 | + |
| 35 | +You must :ref:`enable natural language querying in Compass<enable-natural-language-querying>`. |
| 36 | + |
| 37 | +Steps |
| 38 | +----- |
| 39 | + |
| 40 | +.. procedure:: |
| 41 | + :style: connected |
| 42 | + |
| 43 | + .. step:: Navigate to the :guilabel:`Natural Language Query Bar` |
| 44 | + |
| 45 | + a. Select the :guilabel:`Aggregations` tab. |
| 46 | + #. Click the :guilabel:`Generate aggregation` button. |
| 47 | + |
| 48 | + The :guilabel:`Natural Language Query Bar` displays. |
| 49 | + |
| 50 | + .. step:: Type a question about your collection |
| 51 | + |
| 52 | + Type a natural language prompt for your collection into the query |
| 53 | + bar. Aggregation pipeline prompts usually have an aggregation verb |
| 54 | + such as count, average, or sum with logical conditions. For |
| 55 | + example: ``How many movies have more than 3 writers |
| 56 | + in the writers array?`` |
| 57 | + |
| 58 | + a. Press enter or click the :guilabel:`Generate aggregation` button. |
| 59 | + #. An aggregation pipeline populates in the :guilabel:`Pipeline` |
| 60 | + bar. You can scroll down to see the syntax of each stage. |
| 61 | + |
| 62 | + .. step:: Run the aggregation |
| 63 | + |
| 64 | + a. Before running the query, make sure to thoroughly review the |
| 65 | + syntax of each stage. Ensure the returned pipeline has the |
| 66 | + fields and stages that match your desired use case. |
| 67 | + |
| 68 | + .. warning:: |
| 69 | + |
| 70 | + Aggregation pipelines can contain operators such as |
| 71 | + :pipeline:`$merge` and :pipeline:`$out` that can |
| 72 | + modify your collection's data. |
| 73 | + |
| 74 | + b. Press enter or click the :guilabel:`Run` button to execute |
| 75 | + the pipeline. |
| 76 | + |
| 77 | + The results populate in the aggregations view. |
| 78 | + |
| 79 | + .. tip:: |
| 80 | + |
| 81 | + .. include:: /includes/fact-natural-language-feedback.rst |
| 82 | + |
| 83 | +Example |
| 84 | +------- |
| 85 | + |
| 86 | +Below are examples of prompts to help you understand expected results |
| 87 | +when using natural language prompts for aggregation. |
| 88 | + |
| 89 | +.. list-table:: |
| 90 | + :header-rows: 1 |
| 91 | + :widths: 40 60 |
| 92 | + |
| 93 | + * - Prompt |
| 94 | + - Response |
| 95 | + |
| 96 | + * - ``Count the movies that have a release year of 1999`` |
| 97 | + - |
| 98 | + .. code-block:: json |
| 99 | + :copyable: false |
| 100 | + |
| 101 | + [ |
| 102 | + { |
| 103 | + $match: { |
| 104 | + year: 1999 |
| 105 | + }, |
| 106 | + }, |
| 107 | + { |
| 108 | + $count: "total_movies", |
| 109 | + } |
| 110 | + ] |
| 111 | + |
| 112 | + * - ``Which comedy genre movie has the most awards?`` |
| 113 | + - |
| 114 | + .. code-block:: json |
| 115 | + :copyable: false |
| 116 | + |
| 117 | + [ |
| 118 | + { |
| 119 | + $match: { |
| 120 | + genres: "Comedy" |
| 121 | + } |
| 122 | + }, |
| 123 | + { |
| 124 | + $sort: { |
| 125 | + "awards.wins": -1, |
| 126 | + "awards.nominations": -1 |
| 127 | + } |
| 128 | + }, |
| 129 | + { |
| 130 | + $limit: 1 |
| 131 | + }, |
| 132 | + { |
| 133 | + $project: { |
| 134 | + _id: 0, |
| 135 | + title: 1, |
| 136 | + "awards.wins": 1, |
| 137 | + "awards.nominations": 1 |
| 138 | + } |
| 139 | + } |
| 140 | + ] |
| 141 | + |
| 142 | + * - ``How many movies have a imdb.rating > 4?`` |
| 143 | + - |
| 144 | + .. code-block:: json |
| 145 | + :copyable: false |
| 146 | + |
| 147 | + [ |
| 148 | + { |
| 149 | + $match: { |
| 150 | + "imdb.rating": { $gt: 4 } |
| 151 | + } |
| 152 | + }, |
| 153 | + { |
| 154 | + $group: { |
| 155 | + _id: null, |
| 156 | + count: { $sum: 1 } |
| 157 | + } |
| 158 | + } |
| 159 | + ] |
| 160 | + |
| 161 | +Next Steps |
| 162 | +---------- |
| 163 | + |
| 164 | +:ref:`prompt-natural-language-query` |
| 165 | + |
| 166 | +Learn More |
| 167 | +---------- |
| 168 | + |
| 169 | +:ref:`query-natural-language` |
0 commit comments