|
| 1 | +.. _vsce-copilot-query: |
| 2 | + |
| 3 | +============== |
| 4 | +/query Command |
| 5 | +============== |
| 6 | + |
| 7 | +.. default-domain:: mongodb |
| 8 | + |
| 9 | +.. contents:: On this page |
| 10 | + :local: |
| 11 | + :backlinks: none |
| 12 | + :depth: 1 |
| 13 | + :class: singlecol |
| 14 | + |
| 15 | +The ``/query`` command assists in generating queries from a natural |
| 16 | +language against a connected MongoDB cluster. The |copilot| provides |
| 17 | +underlying schema information of the relevant collections to GitHub |
| 18 | +Copilot to generate a response. If you do not specify a collection in |
| 19 | +your prompt, the chat prompts you to select a relevant collection. |
| 20 | + |
| 21 | +When the LLM generates a query, you can open the query in a playground |
| 22 | +file or run the query directly in your collection. |
| 23 | + |
| 24 | +Examples |
| 25 | +~~~~~~~~ |
| 26 | + |
| 27 | +Generate a Query |
| 28 | +```````````````` |
| 29 | + |
| 30 | +Consider the ``users`` collection in the `Mflix Sample Database |
| 31 | +<https://www.mongodb.com/docs/atlas/sample-data/sample-mflix/#sample_mflix.users>`__. |
| 32 | +Each document in the collection has the following structure: |
| 33 | + |
| 34 | +.. code-block:: javascript |
| 35 | + :copyable: false |
| 36 | + |
| 37 | + { |
| 38 | + _id: { |
| 39 | + "$oid": "59b99db4cfa9a34dcd7885b6" |
| 40 | + }, |
| 41 | + name: "Kayden Washington", |
| 42 | + |
| 43 | + password: "11222021" |
| 44 | + } |
| 45 | + |
| 46 | +Once you connect to the deployment that contains the ``users`` |
| 47 | +collection, you can ask the GitHub Copilot chat to generate a query that |
| 48 | +finds the document in the ``users`` collection that has the ``name`` |
| 49 | +value of ``Kayden Washington``. |
| 50 | + |
| 51 | +.. code-block:: javascript |
| 52 | + :copyable: false |
| 53 | + |
| 54 | + @MongoDB /query In the sample_mflix database, find a document in the |
| 55 | + users collection with the name of Kayden Washington. |
| 56 | + |
| 57 | +The GitHub Copilot Chat uses the |copilot| to |
| 58 | +generate the following query using knowledge of your database schema: |
| 59 | + |
| 60 | +.. code-block:: javascript |
| 61 | + |
| 62 | + use(`sample_mflix`); |
| 63 | + db.getCollection('users').findOne({ name: 'Kayden Washington' }); |
| 64 | + |
| 65 | +Once the |copilot| generates the query, you can choose to run the query |
| 66 | +directly or open the query in a playground. |
| 67 | + |
| 68 | +.. figure:: /images/copilot-query.png |
| 69 | + :figwidth: 700px |
| 70 | + :alt: Screenshot of copilot generating a query |
| 71 | + |
| 72 | +Build an Aggregation Pipeline |
| 73 | +````````````````````````````` |
| 74 | + |
| 75 | +You can also use the |copilot| to build aggregation pipelines. Consider |
| 76 | +the ``users`` collection in the `Mflix Sample Database |
| 77 | +<https://www.mongodb.com/docs/atlas/sample-data/sample-mflix/#sample_mflix.users>`__. |
| 78 | +Each document in the collection has the following structure: |
| 79 | + |
| 80 | +.. code-block:: javascript |
| 81 | + :copyable: false |
| 82 | + |
| 83 | + { |
| 84 | + _id: { |
| 85 | + "$oid": "59b99db4cfa9a34dcd7885b6" |
| 86 | + }, |
| 87 | + name: "Kayden Washington", |
| 88 | + |
| 89 | + password: "11222021" |
| 90 | + } |
| 91 | + |
| 92 | +Once you connect to the deployment that contains the ``users`` |
| 93 | +collection, you can ask the GitHub Copilot chat to generate an aggregation pipeline. |
| 94 | + |
| 95 | +.. code-block:: javascript |
| 96 | + :copyable: false |
| 97 | + |
| 98 | + @MongoDB /query Generate an aggregation pipeline on the users |
| 99 | + collection that first sorts documents alphabetically by name and then |
| 100 | + removes the password field from each document. |
| 101 | + |
| 102 | +The |copilot| generates the following aggregation pipeline: |
| 103 | + |
| 104 | +.. code-block:: javascript |
| 105 | + |
| 106 | + use('sample_mflix'); |
| 107 | + db.getCollection('users').aggregate([ |
| 108 | + { $sort: { name: 1 } }, |
| 109 | + { $project: { password: 0 } } |
| 110 | + ]); |
| 111 | + |
| 112 | +Once the |copilot| generates the query, you can choose to run the pipeline |
| 113 | +directly or open the pipeline in a playground. |
| 114 | + |
| 115 | +.. figure:: /images/copilot-agg-pipeline.png |
| 116 | + :figwidth: 700px |
| 117 | + :alt: Screenshot of copilot generating an aggregation pipeline |
| 118 | + |
| 119 | +You can also iteratively build on your aggregation pipeline: |
| 120 | + |
| 121 | +.. code-block:: javascript |
| 122 | + :copyable: false |
| 123 | + |
| 124 | + @MongoDB /query Add a stage to my pipeline that adds a username field |
| 125 | + to each document containing the user's email without the |
| 126 | + email domain. |
| 127 | + |
| 128 | +The |copilot| returns the following aggregation pipeline: |
| 129 | + |
| 130 | +.. code-block:: javascript |
| 131 | + |
| 132 | + use('sample_mflix'); |
| 133 | + db.getCollection('users').aggregate([ |
| 134 | + { $sort: { name: 1 } }, |
| 135 | + { $project: { password: 0 } }, |
| 136 | + { $addFields: { username: { $arrayElemAt: [{ $split: ["$email", "@"] }, 0] } } } |
| 137 | + ]); |
| 138 | + |
| 139 | +.. figure:: /images/copilot-agg-pipeline2.png |
| 140 | + :figwidth: 700px |
| 141 | + :alt: Screenshot of copilot iteratively building on an aggregation pipeline |
0 commit comments