Create a collection

Use the db.createcollection () method to create the collection

Syntax format:

db.createCollection(name, options)
Copy the code

Parameter description:

  • Name: Name of the collection to be created
  • Options: An optional parameter that specifies options regarding memory size and indexes

Options can be the following parameters:

field type describe
capped Boolean (Optional) If true, a fixed collection is created. A fixed set is a set with a fixed size that automatically overwrites the earliest documents when it reaches its maximum value.When this value is true, the size argument must be specified.
autoIndexId Boolean This parameter is not supported after 3.2. (Optional) If the value is true, an index is automatically created in the _ID field. The default is false.
size The numerical (Optional) Specify a maximum value, the number of bytes, for a fixed set.If capped is true, you also need to specify this field.
max The numerical (Optional) Specifies the maximum number of documents to contain in a fixed collection.

When inserting a document, MongoDB first checks the size field of the fixed set and then the Max field.

From novice tutorial

Create a collection of users under the test database

> db.createCollection("user")
Copy the code

View existing collections

> show collections
user
Copy the code

We don’t actually need to create the collection, when we insert the document, if the collection doesn’t exist, it will be created automatically

> db.fruit.insert({"name":"apple"})
WriteResult({ "nInserted" : 1 })
Copy the code

Delete collection

Drop the collection using the drop() method of the collection object

Viewing the current collection

>Show Collections // View the current collections
article
fruit
user
Copy the code

Delete collection fruit

>Db.fruit. Drop () // Drop the fruit collection
true
Copy the code

Looking at the current collection again with show Collections, it is found that it has been deleted

>Show collections // Check again
article
user
Copy the code