Create Collection in MongoDB
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
You should read it
- Query Document in MongoDB
- Index (Mong) in MongoDB
- 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
- Data modeling in MongoDB
- ObjectId in MongoDB
- Shard in MongoDB
- Advantages of MongoDB
- Text Search in MongoDB
Maybe you are interested
Collection of easy-to-make, delicious, quality coffee cocktail recipes for everyone
5 Best Tools to Manage Your MP3 Music Collection
Can SOCKS5 Proxies Improve Data Collection Strategies
The Benefits of Liverpool Rubbish Collection
Collection of iPhone Widget customization applications
Collection of Dell Laptops worth using in 2023