MongoDB Create Collection
1. Manually Create:
Syntax format:
DB. CreateCollection(name, options)
Parameter description:
- Name: The collection name to create
- Options: Optional parameters, specifying option for memory size and index
Options can be the following parameters:
Field |
type |
Description |
Capped |
Boolean |
(optional) If true, creates a fixed collection. A fixed collection is a set that has a fixed size that automatically overwrites the oldest document when it reaches its maximum value. When this value is true, you must specify the size parameter. |
Autoindexid |
Boolean |
(optional) If true, the index is automatically created in the _id field. The default is False. |
Size |
Numerical |
Optionally, specify a maximum value (in bytes) for the fixed collection. If capped is true, you also need to specify the field. |
Max |
Numerical |
Optionally, specify the maximum number of documents contained in the fixed collection |
For example: Create a Colle1 collection in the exam table:
> use examswitched to DB exam> Db.createcollection ("colle1"" OK"1 }>#查看当前数据库的集合colle1system.indexes
Create a fixed collection mycol, the entire collection space size 6142800 KB, the maximum number of documents is 10,000.
> db.createcollection ("mycol"truetrue, size: 614280010000"OK"1 }>
2. In MongoDB, you do not need to create a collection. When you insert some documents, MongoDB automatically creates the collection.
> show Collectionscolle1system.indexes> Db.exam.insert ({"name111"" rookie tutorial""ninserted"1 })> Show Collectionscolle1examsystem.indexes
MongoDB Delete Collection
Syntax format:
Db.collection.drop ()
return value
If the selected collection is successfully deleted, the drop () method returns True, otherwise false is returned.
For example: View all collections and delete one of the collections
> show collectionscolle1examsystem.indexes> show tablescolle1examsystem.indexes> Db.colle1.drop ()true> show tablesexamsystem.indexes>
View a collection of current data can be used with show collections or show Tables
Collection Operations for MongoDB