Mastering MongoDB: A Beginner's Guide to Building and Querying NoSQL Databases!
Introduction
MongoDB is a robust NoSQL database known for its flexibility and scalability, ideal for modern applications. In this guide, we’ll go through the process of creating a MongoDB database, setting up a collection, inserting documents, and querying the data with filters.
Prerequisites
To follow along, you will need:
A basic understanding of MongoDB and NoSQL databases.
MongoDB installed on your machine or access to a MongoDB cloud instance like MongoDB Atlas.
A MongoDB client such as MongoDB Compass or access to the MongoDB shell.
Step 1: Create a New MongoDB Database and Collection
Connecting to MongoDB
Using the MongoDB Shell:
Open a terminal and run the following command to connect to your MongoDB server:
mongo
If connecting to MongoDB Atlas, replace this with the Atlas connection string provided.
Using MongoDB Compass:
Open MongoDB Compass and connect using the MongoDB URI.
For a local instance, you can typically just click Connect.
Create a New Database
In the MongoDB Shell:
Run the following command to create and switch to a new database called
my_database
:use my_database
In MongoDB Compass:
Go to Databases > Create Database.
Name the database
my_database
and add a collection namedusers
.
Create a New Collection
In the MongoDB Shell:
db.createCollection("users")
In MongoDB Compass:
- After creating the database, specify
users
as your collection name.
- After creating the database, specify
Step 2: Insert Several Documents
Next, we’ll insert user data into the users
collection using Indian data for names, cities, and emails.
Inserting Documents
In the MongoDB Shell:
Use the
insertMany
method to add multiple documents:db.users.insertMany([ { name: "Aarav", age: 28, city: "Mumbai", email: "aarav@example.in" }, { name: "Bhoomi", age: 34, city: "Delhi", email: "bhoomi@example.in" }, { name: "Chirag", age: 22, city: "Bangalore", email: "chirag@example.in" }, { name: "Deepa", age: 29, city: "Mumbai", email: "deepa@example.in" }, { name: "Eshan", age: 42, city: "Chennai", email: "eshan@example.in" } ]);
In MongoDB Compass:
Go to the
users
collection.Use the Insert Document option and insert each document individually, or use the Insert Multiple Documents feature.
Step 3: Querying Documents Using Filters
Now that you’ve inserted data, let’s retrieve documents with various filters.
Query by Age
To find users with a specific age, use the find
method with a filter. For instance, to find users who are 28 years old:
db.users.find({ age: 28 }).pretty()
Query by City
To find users from a specific city, such as Delhi
:
db.users.find({ city: "Delhi" }).pretty()
Step 4: Using Complex Queries
MongoDB supports complex queries with advanced conditions.
To find users older than 30:
db.users.find({ age: { $gt: 30 } }).pretty()
To find users who live in
Mumbai
and are younger than 30:db.users.find({ city: "Mumbai", age: { $lt: 30 } }).pretty()
Step 5: Retrieve Specific Fields
You can choose to retrieve only certain fields, such as name
and email
, in your query. Use the following syntax:
db.users.find({}, { name: 1, email: 1, _id: 0 }).pretty()
Here, 1
means the field will be included in the output, and 0
means it will be excluded. _id
is set to 0
to exclude it from the results.
Conclusion
In this guide, we walked through creating a MongoDB database, adding a collection and documents, and querying data using filters. MongoDB’s dynamic schema and flexible querying options make it an excellent choice for applications that require agile data handling.