MongoDB is a popular open-source document database and NoSQL database. MongoDB was created using the C++ programming language. MongoDB is a document-oriented database that provides ease of scaling, higher availability, and higher performance. It operates on a number of systems and platforms. 

MongoDB is built on concepts of documents and the principles of collection. On a single MongoDB server, many databases are frequently discovered. (A physical location where data is stored is called a Database. Each database has its set of files on the file system.) In this article, we will be discussing, in short, almost every topic related to MongoDB.

Learn the Ins & Outs of Software Development

Caltech Coding BootcampExplore Program
Learn the Ins & Outs of Software Development

Data Modeling

The Embedded data model and Normalized data model are the two types of data models offered by MongoDB.

Embedded Modeling

This paradigm, also known as the de-normalized data model, allows us to have (embed) all associated data in a single document.

Example

{

_id: ,

Stud_ID: "1003452A902"

Personal_details:{

First_Name: "Priya",

Last_Name: "Shabina",

Date_Of_Birth: "1997-11-12"

},

Contact: {

e-mail: "priya-shabina@yahoo.com"

},

Address: {

city: "Pune",

State: "Maharashtra"

}

}

Normalized Data Modeling

We can use references in this model to refer to subdocuments in the primary document.

Example

Student

{

_id: <ObjectId123>,

Stud_ID: "1003452A902"

}

Personal Details

{

_id: <ObjectId123>,

studDocID: " ObjectId121",

First_Name: "Priya",

Last_Name: "Shbina",

Date_Of_Birth: "1997-11-12"

}

Contact

{

_id: <ObjectId123>,

studDocID: " ObjectId123",

e-mail: "priya-shabina@yahoo.com",

}

Limit Records

This has two methods - limit() method and skip() method.

limit() method

The limit() method in MongoDB is used to limit the number of records. The method takes only one number type argument, which is the number of documents to display.

Syntax

db.COLLECTION_NAME.find().limit(NUMBER)

skip() method

Aside from the limit() function, there's also skip(), which takes a number type input and skips the specified number of documents.

Syntax

db.COLLECTION_NAME.find().limit(NUMBER).skip(NUMBER)

Get In-Demand Skills to Launch Your Data Career

Big Data Hadoop Certification Training CourseExplore Course
Get In-Demand Skills to Launch Your Data Career

Sorting Records

sort() method

In MongoDB, the sort() method is used to sort documents. The method takes a document with a list of fields and the order in which they should be sorted. The numbers 1 and -1 are used to determine the sorting order. In ascending order, 1 is utilized, while in descending order, -1 is used.

Syntax of sort() method

db.COLLECTION_NAME.find().sort({KEY:1})

Create Database

We use the use command to create a database in MongoDB. 

The use Command

DATABASE NAME is the name of the database created by MongoDB. If a database does not exist, the command will create one; otherwise, it will return the current database.

Syntax

use DATABASE_NAME

If you wish to learn about creating a database in MongoDB, read this comprehensive article.

Drop Database

dropDatabase() Method

To delete an existing database in MongoDB, use the db.dropDatabase() command.

Syntax

db.dropDatabase()

Create Collection

createCollection() Method

To construct a collection in MongoDB, use the db.createCollection(name, parameters) method.

Syntax

db.createCollection(name, options)

Drop Collection

drop() Method

db.collection is a MongoDB collection. db.collection.drop() is a function that is used to remove a collection from the database.

Syntax

db.COLLECTION_NAME.drop()

Capped Collection in MongoDB

Updates to documents that result in increased document size are restricted in capped collections. Capped collections hold documents in the same order as disc storage, ensuring that the document size does not exceed the disc space allocated.

Creating Capped Collection

To make a capped collection, use the createCollection command with the capped option set to true and the collection's maximum size in bytes specified.

Syntax

db.createCollection("cappedLogCollection",{capped:true,size:10000})

Querying Capped Collection

A find query on a capped collection will return results in insertion order by default. Use the sort command instead if you want the documents to be returned in reverse order.

Syntax

db.cappedLogCollection.find().sort({$natural:-1})

The Ultimate Ticket to Top Data Science Job Roles

Post Graduate Program In Data ScienceExplore Now
The Ultimate Ticket to Top Data Science Job Roles

Data Types in MongoDB

  • Double - Floating-point values are stored using the double type.
  • Timestamp - This is useful for keeping track of when a document has been edited or added to.
  • Max/Min keys - This type of key is used to compare a value to the BSON elements with the lowest and highest values.
  • String - The most often used datatype for storing data is a string. In MongoDB, strings must be valid in UTF-8.
  • Symbol - This datatype is used in the same way as a string, although it's usually reserved for languages that utilize a specific symbol type.
  • Binary - The datatype binary data is used to store binary data.
  • Integer - Integer is a data type that stores a numerical value. Depending on your server, integers can be 32-bit or 64-bit.
  • Regular Expression - The datatype regular expression is used to store regular expressions.
  • Arrays - This type is used to hold numerous values in one key, such as an array, a list, or a list of values.
  • Date - The current date or time in UNIX time format is stored in this data type. You can create your own date time by constructing a Date object and passing in the day, month, and year.
  • Null - This type is used to store a value that is null.
  • Boolean - The boolean (true/false) value is stored using this type.
  • Object - Object This data type is used for documents that are embedded.
  • Code - JavaScript code is stored in the document using this data type.
  • Object ID - It is a data type for storing the document's ID.

Projection in MongoDB

In MongoDB, projection refers to not picking the complete document's information but information that is actually required. If there are five fields in a document and we need to see only 3 of them, then we get to choose only three of them.

find() Method

The find() method in MongoDB allows a second optional parameter, which is a list of fields to obtain, as defined in the MongoDB Query Document. All of a document's fields are displayed whenever MongoDB's find() method is used. Providing a list of fields with the values 0 or 1 will prevent this from happening. The value 0 indicates that the field should be hidden, whereas 1 indicates that the field should be visible.

Syntax

db.COLLECTION_NAME.find({},{KEY:1})

MongoDB Insert Document

insert() method

We need to use MongoDB's insert() or save() methods to insert data into a MongoDB collection.

Syntax

db.COLLECTION_NAME.insert(document)

insertOne() method

Syntax

db.COLLECTION_NAME.insertOne(document)

insertMany() method

Syntax

db.COLLECTION_NAME.insertMany(document1, document2,...documentn)

MongoDB Delete Document

remove() Method

To delete a document from a collection, use MongoDB's remove() method. The delete() function takes two arguments. The first is the deletion criteria, while the second is the justOne flag.

deletion criteria - It is based on documents (optional) Delete criteria based on documents will be removed.

justOne(optional) - If set to true or 1, justOne removes only one document.

Syntax

db.COLLECTION_NAME.remove(DELLETION_CRITTERIA)

MongoDB Update Document

To update a document into a collection, use MongoDB's update() and save() functions. The save() function replaces the existing document with the document supplied in the save() method, while the update() method updates the values in the existing document.

update() Method

The update() method modifies the existing document's values.

Syntax

db.COLLECTION_NAME.update(SELECTION_CRITERIA, UPDATED_DATA)

save() Method

The save() method overwrites the existing document with the new one that is handed in.

Syntax

db.COLLECTION_NAME.save({_id:ObjectId(),NEW_DATA})

Become a Data Scientist With Real-World Experience

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

Indexing

Indexes are unique data structures that store a tiny fraction of the data set in a format that is easy to navigate. The value of a specific field or collection of fields is stored in the index, which is ordered by the field's value as stated in the index.

createIndex() Method

MongoDB's createIndex() method is used to create an index.

Syntax

db.COLLECTION_NAME.createIndex({KEY:1})

dropIndex() method

MongoDB's dropIndex() method allows you to remove a specific index.

Syntax

db.COLLECTION_NAME.dropIndex({KEY:1})

dropIndexes() method

This function deletes a collection's numerous (specified) indexes.

Syntax

db.COLLECTION_NAME.dropIndexes()

getIndexes() method

The description of all the indexes in the collection is returned by this method.

Syntax

db.COLLECTION_NAME.getIndexes() 

Sharding in MongoDB

MongoDB's method of handling the needs of data growth is sharding, which is the technique of storing data records across numerous machines. As data grows in size, a single system may not be able to store it all or provide a satisfactory read and write throughput. The difficulty of horizontal scaling is solved by sharding.

These Have Three Components in MongoDB:

1. Query Routers: Mongo instances that communicate with client apps and route operations to the appropriate shard are called Query Routers. The query router runs the operations, targets them to shards, and then returns the results to the clients. To share the client request load, a shared cluster can include many query routers.

2. Config servers: These keep track of the metadata for the cluster. This data contains a mapping of the cluster's data set to the shards. The query router uses this metadata to target operations to certain shards. In production, shared clusters contain three config servers.

3. Shards: They are a type of data storage. They give strong data consistency and availability. Each shard is a single duplicate set in the production environment.

MongoDB Regex

Regular Expressions are commonly used to search for a pattern or word in a string in all languages. The $regex operator in MongoDB also has regular expression capability for string pattern matching. MongoDB's regular expression language is PCRE (Perl Compatible Regular Expression).

Level Up Your Data Science Career With Simplilearn

In this article, we discussed most of the important topics related to MongoDB in brief and got a complete overview of each of those. To dive deep into this and get a grasp of each of these topics and more in a detailed manner, check out Simplilearn’s Data Science Certification to level up your data science career.

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: 5 Apr, 2024

8 Months$ 3,850