Create a collection:
db.createCollection(name, options)
- options can be the following parameters:
-
capped (Boolean): (optional) If true, a fixed collection is created. A fixed collection refers to a collection with a fixed size. When the maximum value is reached, it automatically overwrites the oldest document. ** When the value is true, the size parameter must be specified.
-
autoIndexId (Boolean): (optional) If true, automatically create an index in the _id field. The default is false.
-
size (numeric): optional) Specify a maximum value for the fixed set, in kilobytes (KB). ** If capped is true, this field also needs to be specified.
-
max (numeric value): (optional) specifies the maximum number of documents in a fixed collection.
-
When inserting a document, MongoDB first checks the size field of the fixed collection, and then checks the max field.
> use demo
switched to db demo
> db.createCollection("demoList")
{ "ok" : 1 }
View collection:
show collections 或 show tables
> show collections
demoList
> show tables
demoList
>
Create a collection directly when inserting a document
> db.demoList2.insert({"name":"测试集合2"})
WriteResult({ "nInserted" : 1 })
> show tables
demoList
demoList2
>
Delete collection:
db.collection.drop()
> show tables
demoList
demoList2
> db.demoList.drop()
true
> show collections
demoList2
>