Create a collection: CreateCollection () method
MongoDB db.createcollection (name, options)
is used to create a collection.
Grammar:
The basic createcollection () command syntax is as follows:
Db.createcollection (name, options)
In the command, name is the name of the collection that you want to create. Options is a file that specifies the set of configurations
Parameters |
Type |
Describe |
Name |
String |
The name of the collection to create |
Options |
Document |
(optional) Specify the memory size and indexing options |
Option parameters are optional, so you only need to go to the specified collection name. The following are a list of options that you can use:
Field |
Type |
Describe |
Capped |
Boolean |
(optional) If true, enable the CAP collection. A CAP collection is a fixed size collection that automatically overwrites the earliest entries when it reaches its maximum size. If you specify true, you need to also specify dimension parameters. |
Autoindexid |
Boolean |
(optional) If true, the default value for automatically creating an indexed _id field is false. |
Size |
Number |
Optionally, specify the maximum size byte cap collection. If the cap is true then you also need to specify this field. |
Max |
Number |
Optionally, specify the maximum number of files allowed in the capping collection. |
When you insert a document, MongoDB the first check Size field Cap collection, and then it checks the largest field.
Example:
The basic syntax for CreateCollection () methods that do not use options is as follows:
>use test switched to DB Test >db.createcollection ("mycollection") {"OK": 1} >
You can check show collections by using the collection command you created
>show Collections mycollection System.indexes
The following example shows the syntax for several important options createcollection () methods:
>db.createCollection("mycol", { capped : true, autoIndexID : true, size : 6142800, max : 10000 } ) { "ok" : 1 } >
In MongoDB, you do not need to create a collection. When you insert some files MongoDB a collection that is automatically created.
>db.yiibai.insert({"name" : "yiibai"}) >show collections mycol mycollection system.indexes yiibai >
Delete collection: Drop () method
MongoDB's
is used to delete a collection from the database.
Grammar:
The basic syntax for the drop () command is as follows
Db. Collection_name.drop ()
First, check the available collections in the database MyDB
>use MyDB switched to DB MyDB >show collections MyCol mycollection system.indexes yiibai >
Now delete the collection name mycollection
>db.mycollection.drop () True >
Check the list of collections in the database again
>show collections MyCol System.indexes Yiibai >
The drop () method returns True and returns False if the selected collection is discarded successfully