Create Collection in MongoDB
Method db.createCollection (name, options) in MongoDB is used to create Collection.
CreateCollection () method in MongoDB
Method db.createCollection (name, options) in MongoDB is used to create Collection.
Syntax
The basic syntax of createCollection () in MongoDB is as follows:
db . createCollection ( name , options )
In the above command, name is the name of Collection . Options are a Document and are used to determine the configuration for Collection.
ParametersType Description Name String Name of Collection Options Document Define options for memory size and indexingThe options parameter is optional, so you only need to specify the name of the Collection. Here is a list of options you can use:
FieldType Descriptioncapped Boolean If true, activate a Capped Collection. This is a fixed-size Collection that automatically overrides the oldest entries when it approaches the maximum size. If you specify true, then you also need to specify the size parameter autoIndexID Boolean (Optional) If true, automatically create an index on the _id fields. The default value is false size Number (Optional) Specifies the maximum size (byte value) for a Capped Collection. If the capped parameter is true, then you also need to specify this field max Number (Optional) Determine the maximum number of Document allowed in a Capped CollecitonWhile performing data insertion into the Document, MongoDB first checks the size field of Capped Collection, then checks the max field.
For example
The basic syntax of createCollection () method without options is as follows:
> use test switched to db test > db . createCollection ( "mycollection" ) { "ok" : 1 } > You can check the created Collection using the following show collections command :
> show collections mycollection system . indexes
The following examples illustrate the syntax of createCollection () method with some important options.
> db . createCollection ( "mycol" , { capped : true , autoIndexID : true , size : 6142800 , max : 10000 } ) { "ok" : 1 } > In MongoDB, you don't need to create Collection. MongoDB creates collections automatically, when you insert some documents.
> db . tutorialspoint . insert ({ "name" : "tutorialspoint" }) > show collections mycol mycollection system . indexes tutorialspoint > According to Tutorialspoint
Previous post: Delete Database in MongoDB
Next article: Delete Collection in MongoDB
Discover more
Share by
Isabella HumphreyYou should read it
- Delete Document in MongoDB
- Capped Collection in MongoDB
- MongoDB queries and tasks you should know
- Limit records in MongoDB
- Instructions on 2 ways to install MongoDB on Raspberry Pi
- The Quiet Details That Make a Sports Betting Platform Feel Reliable
- Instructions on creating toy set images with ChatGPT AI
- How are AI agents changing the journalism industry?
- Delete Collection in MongoDB
- Data type in MongoDB
- Insert Document in MongoDB