Skip to content

DOCSP-14047 add note about mongoshell to page #5928

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
57 changes: 43 additions & 14 deletions source/faq/fundamentals.txt
Original file line number Diff line number Diff line change
Expand Up @@ -41,35 +41,64 @@ the columns in a relational database table.
How do I create a database and a collection?
--------------------------------------------

.. note::

You can enter the commands referenced in this FAQ by using
the :binary:`~bin.mongo` shell. The mongo shell is an
interactive JavaScript interface to MongoDB. You can use the mongo
shell to query and update data as well as perform administrative
operations.

If a database does not exist, MongoDB creates the database when you
first store data for that database.

If a collection does not exist, MongoDB creates the collection when you
first store data for that collection. [#explicit-creation]_
first store data for that collection.

As such, you can switch to a non-existent database (``use <dbname>``)
and perform the following operation:

.. code-block:: javascript

use myNewDB
use myNewDB;

db.myNewCollection1.insertOne( { x: 1 } );
db.myNewCollection2.createIndex( { a: 1 } );

db.myNewCollection1.insertOne( { x: 1 } )
db.myNewCollection2.createIndex( { a: 1 } )
- The :method:`db.collection.insertOne()` method creates
the collection ``myNewCollection1`` if it does not already exist.

The ``insert`` operation creates both the database ``myNewDB`` and the
collection ``myNewCollection1`` if they do not already exist.
- The :method:`db.collection.createIndex()` method creates the index and
the collection ``myNewCollection2`` if it does not already exist.

The ``createIndex`` operation, which occurs after the ``myNewDB`` has
been created, creates the index and the collection ``myNewCollection2``
if the collection does not exist. If ``myNewDb`` did not exist, the
``createIndex`` operation would have also created the ``myNewDB``.
- If the ``myNewDb`` database did not exist, either the
:method:`db.collection.createIndex()` method or
:method:`db.collection.insertOne()` method would have created
the ``myNewDb`` database automatically.
<<<<<<< HEAD

You can also create a collection explicitly using
:method:`db.createCollection` method if you want to specify specific
:ref:`options<create_collection_parameters>`, such as maximum size
or document validation rules:

.. code-block:: javascript

use myNewDB;

=======

You can also create a collection explicitly using
:method:`db.createCollection` method if you want to specify specific
:ref:`options<create_collection_parameters>`, such as maximum size
or document validation rules:

.. code-block:: javascript

.. [#explicit-creation]
use myNewDB;

You can also create a collection explicitly using
:method:`db.createCollection` if you want to specify specific
options, such as maximum size or document validation rules.
>>>>>>> 275b25d49 (DOCSP-14047 add note about mongoshell to page)
db.createCollection("myNewCollection1");

.. _faq-schema-free:

Expand Down
6 changes: 6 additions & 0 deletions source/reference/method/db.createCollection.txt
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,12 @@ Definition
collation: <document>,
writeConcern: <document>} )


.. _create_collection_parameters:


.. _create_collection_parameters:

The :method:`db.createCollection()` method has the following parameters:


Expand Down