|
| 1 | +.. _vsce-playground-databases: |
| 2 | + |
| 3 | +================================ |
| 4 | +Create Databases and Collections |
| 5 | +================================ |
| 6 | +.. default-domain:: mongodb |
| 7 | + |
| 8 | +.. contents:: On this page |
| 9 | + :local: |
| 10 | + :backlinks: none |
| 11 | + :depth: 1 |
| 12 | + :class: singlecol |
| 13 | + |
| 14 | +.. include:: /includes/fact-vsce-preview.rst |
| 15 | + |
| 16 | +You can create |
| 17 | +:manual:`databases and collections </core/databases-and-collections/>` |
| 18 | +using a MongoDB Playground. |
| 19 | + |
| 20 | +Prerequisites |
| 21 | +------------- |
| 22 | + |
| 23 | +If you have not done so already, you must complete the following |
| 24 | +prerequisites before you can create a database or collection with a |
| 25 | +MongoDB Playground: |
| 26 | + |
| 27 | +- :ref:`Create a connection to a MongoDB deployment |
| 28 | + <vsce-connect-task>`. |
| 29 | +- :ref:`Activate the connection to the MongoDB deployment |
| 30 | + <vsce-activate-connection>`. |
| 31 | + |
| 32 | +Create a Database and Collection |
| 33 | +-------------------------------- |
| 34 | + |
| 35 | +Once you connect to your deployment using MongoDB for VS code, use the left |
| 36 | +navigation to: |
| 37 | + |
| 38 | +1. Select an active connection and click the :icon-fa5:`plus` icon that |
| 39 | + appears. |
| 40 | + |
| 41 | +#. A MongoDB playground automatically opens with a template form to create |
| 42 | + a database and a regular collection or a |
| 43 | + :ref:`time series collection <vsce-time-series>`. |
| 44 | + |
| 45 | +#. Fill in ``const database`` and ``const collection`` with names of your |
| 46 | + database and collection. |
| 47 | + |
| 48 | +#. Uncomment the form of the collection you wish to create. |
| 49 | + |
| 50 | +#. Fill in the collection fields according to your collection's specifications. |
| 51 | + |
| 52 | +#. To run the playground, click the :guilabel:`Play Button` at the top right |
| 53 | + of the VS code navigation bar. |
| 54 | + |
| 55 | +After running the playground, the left navigation updates with new database |
| 56 | +listed under the active connection it was created. You can find the newly |
| 57 | +created collection by expanding the new database. |
| 58 | + |
| 59 | +.. seealso:: |
| 60 | + |
| 61 | + - To add documents to the collection, see |
| 62 | + :ref:`vsce-create-doc-playground`. |
| 63 | + |
| 64 | + - To learn more about creating time series collections using MongoDB |
| 65 | + playgrounds, see :ref:`vsce-time-series` |
| 66 | + |
| 67 | +Example |
| 68 | +------- |
| 69 | + |
| 70 | +This example creates a database named ``grades`` and a regular collection |
| 71 | +named ``testscores``. |
| 72 | + |
| 73 | +To use this example, start with a template from your MongoDB |
| 74 | +Playgrounds: |
| 75 | + |
| 76 | +.. code-block:: javascript |
| 77 | + |
| 78 | + const database = 'grades'; |
| 79 | + const collection = 'testscores'; |
| 80 | + |
| 81 | + use(database); |
| 82 | + |
| 83 | + db.createCollection(collection); |
| 84 | + |
| 85 | +In the example: |
| 86 | + |
| 87 | +- ``const database`` declares the name ``grades`` for the database. |
| 88 | + |
| 89 | +- ``const collection`` declares the name ``testscores`` for the collection. |
| 90 | + |
| 91 | +- ``use(database)`` creates the new ``grades`` database. |
| 92 | + |
| 93 | +- ``db.createCollection(collection)`` creates the collection ``testscores`` |
| 94 | + inside of the ``grades`` database. |
| 95 | + |
| 96 | +When you press the :guilabel:`Play Button`, MongoDB for VS Code shows the |
| 97 | +following results in the ``Playground Results.json`` pane to confirm the |
| 98 | +creation of the database and collection. |
| 99 | + |
| 100 | +.. code-block:: javascript |
| 101 | + |
| 102 | + { |
| 103 | + "ok": 1 |
| 104 | + } |
| 105 | + |
| 106 | +The ``grades`` database and ``testscores`` collection also appear in your |
| 107 | +left navigation: |
| 108 | + |
| 109 | +.. figure:: /images/vsce-database.png |
| 110 | + :figwidth: 200px |
| 111 | + :alt: Image showing database and collection |
| 112 | + |
| 113 | +.. toctree:: |
| 114 | + :titlesonly: |
| 115 | + |
| 116 | + /time-series-collections |
0 commit comments