@@ -41,35 +41,51 @@ the columns in a relational database table.
41
41
How do I create a database and a collection?
42
42
--------------------------------------------
43
43
44
+ .. note::
45
+
46
+ You can enter the commands referenced in this FAQ by using
47
+ the :binary:`~bin.mongo` shell. The mongo shell is an
48
+ interactive JavaScript interface to MongoDB. You can use the mongo
49
+ shell to query and update data as well as perform administrative
50
+ operations.
51
+
44
52
If a database does not exist, MongoDB creates the database when you
45
53
first store data for that database.
46
54
47
55
If a collection does not exist, MongoDB creates the collection when you
48
- first store data for that collection. [#explicit-creation]_
56
+ first store data for that collection.
49
57
50
58
As such, you can switch to a non-existent database (``use <dbname>``)
51
59
and perform the following operation:
52
60
53
61
.. code-block:: javascript
54
62
55
- use myNewDB
63
+ use myNewDB;
64
+
65
+ db.myNewCollection1.insertOne( { x: 1 } );
66
+ db.myNewCollection2.createIndex( { a: 1 } );
56
67
57
- db.myNewCollection1 .insertOne( { x: 1 } )
58
- db.myNewCollection2.createIndex( { a: 1 } )
68
+ - The :method:` db.collection .insertOne()` method creates
69
+ the collection ``myNewCollection1`` if it does not already exist.
59
70
60
- The ``insert`` operation creates both the database ``myNewDB`` and the
61
- collection ``myNewCollection1 `` if they do not already exist.
71
+ - The :method:`db.collection.createIndex()` method creates the index and
72
+ the collection ``myNewCollection2 `` if it does not already exist.
62
73
63
- The ``createIndex`` operation, which occurs after the ``myNewDB`` has
64
- been created, creates the index and the collection ``myNewCollection2``
65
- if the collection does not exist. If ``myNewDb`` did not exist, the
66
- ``createIndex`` operation would have also created the ``myNewDB``.
74
+ - If the ``myNewDb`` database did not exist, either the
75
+ :method:`db.collection.createIndex()` method or
76
+ :method:`db.collection.insertOne()` method would have created
77
+ the ``myNewDb`` database automatically.
78
+
79
+ You can also create a collection explicitly using
80
+ :method:`db.createCollection` method if you want to specify specific
81
+ :ref:`options<create_collection_parameters>`, such as maximum size
82
+ or document validation rules:
83
+
84
+ .. code-block:: javascript
67
85
68
- .. [#explicit-creation]
86
+ use myNewDB;
69
87
70
- You can also create a collection explicitly using
71
- :method:`db.createCollection` if you want to specify specific
72
- options, such as maximum size or document validation rules.
88
+ db.createCollection("myNewCollection1");
73
89
74
90
.. _faq-schema-free:
75
91
0 commit comments