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