diff --git a/source/faq/fundamentals.txt b/source/faq/fundamentals.txt index bbede4711b1..b8004da1932 100644 --- a/source/faq/fundamentals.txt +++ b/source/faq/fundamentals.txt @@ -44,35 +44,51 @@ 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:`mongo <~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 ``) 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. + +You can also create a collection explicitly using +:method:`db.createCollection` method if you want to specify specific +:ref:`options`, 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. + db.createCollection("myNewCollection1"); .. _faq-schema-free: diff --git a/source/reference/method/db.createCollection.txt b/source/reference/method/db.createCollection.txt index 2887f02ab01..b0e7879424e 100644 --- a/source/reference/method/db.createCollection.txt +++ b/source/reference/method/db.createCollection.txt @@ -57,6 +57,9 @@ Definition } ) + + .. _create_collection_parameters: + The :method:`db.createCollection()` method has the following parameters: