
- MongoDB Tutorial
- MongoDB - Home
- MongoDB - Overview
- MongoDB - Advantages
- MongoDB - Environment
- MongoDB - Data Modeling
- MongoDB - Create Database
- MongoDB - Drop Database
- MongoDB - Create Collection
- MongoDB - Drop Collection
- MongoDB - Data Types
- MongoDB - Insert Document
- MongoDB - Query Document
- MongoDB - Update Document
- MongoDB - Delete Document
- MongoDB - Projection
- MongoDB - Limiting Records
- MongoDB - Sorting Records
- MongoDB - Indexing
- MongoDB - Aggregation
- MongoDB - Replication
- MongoDB - Sharding
- MongoDB - Create Backup
- MongoDB - Deployment
- MongoDB - Java
- MongoDB - PHP
- Advanced MongoDB
- MongoDB - Relationships
- MongoDB - Database References
- MongoDB - Covered Queries
- MongoDB - Analyzing Queries
- MongoDB - Atomic Operations
- MongoDB - Advanced Indexing
- MongoDB - Indexing Limitations
- MongoDB - ObjectId
- MongoDB - Map Reduce
- MongoDB - Text Search
- MongoDB - Regular Expression
- Working with Rockmongo
- MongoDB - GridFS
- MongoDB - Capped Collections
- Auto-Increment Sequence
- MongoDB Useful Resources
- MongoDB - Questions and Answers
- MongoDB - Quick Guide
- MongoDB - Useful Resources
- MongoDB - Discussion
How to create a new collection in MongoDB?
To create a new collection in MongoDB, you need to use createCollection() method.
Case 1: The easiest syntax to create a new collection in MongoDB is as follows:
db.createCollection(“yourNewCollectionName”);
Case 2: The alternative syntax to create a new collection in MongoDB is as follows:
db.createCollections(“yourNewCollectionName”,options);
The options parameter above can have the following values:
capped field which returns boolean type which will either true or false.
autoIndexId field which returns boolean type which will either true or false.
size field which returns number.
max field which returns a number also.
From the above values, the autoIndexed is deprecated from MongoDB version 3.4 and 3.2. We are using MongoDB version 4.0.5. This is how you can also check the current MongoDB version installed on your system:
> db.version(); 4.0.5
Case 1: Let us see the query to create a new collection in MongoDB -
> db.createCollection("userInformation");
The following is the output:
{ "ok" : 1 }
To display the created collection, you need to use show command. The query is as follows:
> show collections;
The following is the output:
userInformation
Case 2: Let us see an alternative way to create a new collection in MongoDB. The query is as follows:
> db.createCollection("bookInformation",{capped:true,size:7000000,max:12000});
The following is the output:
{ "ok" : 1 }
Yes we have created a new collection now using the alternative syntax. To display the created collection name, you need to use show command. The query is as follows:
> show collections;
The following is the output displaying the collections we created above:
bookInformation userInformation
- Related Articles
- How to create a new database in MongoDB?
- How to create a MongoDB collection using Java?
- How to insert new documents into a MongoDB collection in your database?
- Create index in a MongoDB collection?
- Add new field to every document in a MongoDB collection?
- How to add a new field to all the documents in a MongoDB collection
- Adding new property to each document in a large MongoDB collection?
- Create a new ArrayList from another collection in Java
- How to create a collection correctly in MongoDB to avoid “ReferenceError: Not defined” error?
- How to drop a collection in MongoDB?
- How to create a new object and get its saved object in MongoDB?
- MongoDB Convert One record with an array to multiple records in a new collection?
- How to add a column in MongoDB collection?
- How to add a new value to a collection in Laravel?
- Create a new user and set role in MongoDB
