In this section, we show you how to use MongoDB to create collections.
Used in MongoDB createCollection() Method to create a collection.
Syntax format:
db.createCollection(name, options)
Parameter description:
Name: the name of the collection to be created
Options: optional parameter that specifies options for memory size and indexing
Options can be the following parameters:
Field | Types | Description |
|---|---|---|
| Boolean | (optional) if true, a fixed collection is created. A fixed set is a collection of fixed size that automatically overwrites the earliest documents when the maximum is reached. |
When the value is true, the size parameter must be specified. | ||
| Boolean | This parameter is no longer supported after 3.2. (optional) if true, automatically create an index in the _ id field. The default is false. |
| Numerical value | (optional) specify a maximum value, the number of bytes, for the fixed collection. |
If capped is true, you also need to specify this field. | ||
| Numerical value | (optional) specifies the maximum number of documents contained in a fixed collection. |
When inserting a document, MongoDB first checks the fixed collection’s Create a runoob collection in the test database: If you want to view an existing collection, you can use the show collections or show tables command: Here is the use of createCollection () with several key parameters: Create a fixed collection mycol with a total space size of 6142800 B and a maximum of 10000 documents. In MongoDB, you don’t need to create a collection. When you insert some documents, MongoDB automatically creates collections.
size
Field, and then check
max
Field. 3.11.1. Example ¶
> use test
switched to db test
> db.createCollection("runoob")
{ "ok" : 1 }
>
> show collections
runoob
system.indexes
> db.createCollection("mycol", { capped : true, autoIndexId : true, size :
6142800, max : 10000 } )
{ "ok" : 1 }
>
> db.mycol2.insert({"name" : "菜鸟教程"})
> show collections
mycol2
...