MongoDB is an open-source NoSQL document database management program written in C++. NoSQL databases can be effectively used when working with large sets of distributed data; therefore, they are considered valuable alternatives to traditional relational databases. MongoDB can manage, store, or retrieve document-oriented information. Organizations can use it for many other features, including non-parameterized queries, cataloging, load-distributing, and server-side JavaScript execution. The article discusses the create collection method, its syntax, and name and options that will help retrieve, store, manipulate, and communicate aggregate data. 

Become a Data Scientist With Real-World Experience

Data Scientist Master’s ProgramExplore Program
Become a Data Scientist With Real-World Experience

Create Collection Method 

In relational databases, there are tables, while in MongoDB, there are collections, which are capable of storing documents of diverse or dissimilar types. Several ways can be employed to create and remove collections in MongoDB. Of which one way is by using db.createCollection(name, options). MongoDB creates a collection for an inserted command automatically if no similar collection already exists in the MongoDB database.

For example, To create a capped collection, insert the following command,

db.createCollection(“teachers”,{capped : true, size : 9232768}){“ok” : 1}

After inserting this command, a collection named “teachers” having a max size of 9232768 bytes will be created in the MongoDB database. Once the collection reaches the size limit, overwriting of old entries will start.

Syntax of the Create Collection Method

Given is a syntax of the Create Collection Method that can be used to create a collection in MongoDB: db.createCollection(collection_name, options)

Name and Options

In the Create Collection method, “name” is the name of the collection created. “Options” is a document used to denote the configuration of a collection, including memory size and indexing. 

Parameter

Type

Description

Name

String

Collection name

Options

Document

Specifications of the configuration of the collection

Options is an optional parameter, so only the name of the collection needs to be specified. Given below is a list of options that can be used. Please note that all of these parameters are optional.

Field

Type

Description

capped

Boolean

If it is set to true, a capped collection is enabled, which is a fixed size collection that overwrites its oldest entries automatically when maximum size is reached. If true is specified, the size parameter also needs to be specified.

IndexID

Boolean

If true is set, automatically, an index is created on the ID field. Its default value is false.

size

number

Specifies a maximum size for a capped collection in bytes. If true is specified, then this field also needs to be specified.

max

number

Specifies the highest number of documents allowed in the collection.

A regular collection does not have size limitations, unlike capped collections, which have limitations of size. MongoDB first evaluates the size field of the capped collection and then checks the max field while inserting the document.

Conclusion 

While living and working in the current data-driven era, organizations need to have all their documents and files stored in a reliable database. After going through the discussion, you will now be able to create collections in MongoDB confidently. All you need to do is execute some code lines, and the rest of everything happens automatically. 

Want to become a better data professional and kickstart your Data Science journey? Then check the Professional Certificate Program in Data Science right away! At the end of the program, the participants will be issued a Data Science certificate from Purdue, one of the most innovative universities in the U.S. They will also get an opportunity to have hands-on experience in handling projects of top companies like Amazon, Walmart, Uber, and Mercedes Benz and receive career mentorship and valuable insights from an industry expert to build resumes and ace the interviews.

Our Big Data Courses Duration And Fees

Big Data Courses typically range from a few weeks to several months, with fees varying based on program and institution.

Program NameDurationFees
Post Graduate Program in Data Engineering

Cohort Starts: 25 Apr, 2024

8 Months$ 3,850