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
May be interested
- Text Search in MongoDBstarting with version 2.4, mongodb started supporting text indexes to search within the string content.
- Delete Document in MongoDBthe remove () method in mongodb is used to delete the document from collection. the remove () method takes two parameters. the first parameter of the deletion criteria determines the document to delete, and the second parameter is justone.
- Create Backup in MongoDBto create a database backup in mongodb, you should use the mongodump command. this command will dump all server data into dump directory. there are many options available from which you can limit the amount of data or backup created by remote server.
- Advantages of MongoDBany relation database has a unique schema design to index the data tables and relationships between those tables. meanwhile in mongodb there is no concept of relationship.
- Data type in MongoDBmongodb supports many different data types. the following are some typical data types.
- Create Database in MongoDBthe use database_name command in mongodb is used to create the database. this command will create a new database, if it does not exist yet, otherwise, this command will return the existing database.
- MongoDB malicious code attacks more than 26,000 victims in a weekmalware that attacks the mongodb database has rekindled last week and after the weekend with the arrival of three new groups hijack more than 26,000 servers, of which one group attacked 22,000 machines.
- Learn about security features and authentication in MongoDBin the following article, we will continue to introduce security and authentication features in the mongodb database management system series. in essence, the basic account validation feature is available in mongodb, but has been disabled in default mode, and in this mode the system requires at least 1 active database ...
- Insert Document in MongoDBto insert data into collection in mongodb, you need to use the insert () or save () method.
- Auto-Increment Sequence in MongoDBmongodb does not have the auto-increment out-of-the-box feature like sql database. by default, it uses a 12-byte objectid for the _id field as the primary key to uniquely identify the documents. however, there are situations when we want the _id field to have some values that automatically increase beyond objectid.