MongoDB – Create Collection

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.  

Create Collection Method 

Tables exist in relational databases, whereas collections exist in MongoDB and can store documents of various or dissimilar types. There are several methods for creating and removing 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. 

ParameterTypeDescription
NameStringCollection name
OptionsDocumentSpecifications of the configuration of the collection

Option 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.

FieldTypeDescription
cappedBoolean If set to true, a capped collection is enabled, which is a fixed-size collection that automatically overwrites its oldest entries when the maximum size is reached. If true is specified, the size parameter must be specified as well.
IndexIDBooleanIf true is set, automatically, an index is created on the ID field. Its default value is false.
sizenumberSpecifies a maximum size for a capped collection in bytes. If true is specified, then this field also needs to be specified.
maxnumberSpecifies 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. 

Similar Posts

Leave a Reply

Your email address will not be published. Required fields are marked *