MongoDB Quickstart

Rajat Saxena

July 9, 2023

MongoDB is a Document database used by companies like Forbes, Toyota etc. CourseLit (CodeLit’s own LMS) also uses MongoDB as a database.

Installation

Data

All the database operations are going to use the following data. It is a list of hotels.

[
	{
		"name": "Blue Royal",
		"price": 200,
		"amenities": ["tv", "geyser", "wifi"],
		"address": {
			"city": "Mumbai"
		}
	}, 
	{
		"name": "Panama Lagoon",
		"price": 300,
		"amenities": ["tv", "wifi"],
		"address": {
			"city": "Bangalore"
		}
	}, 
	{
		"name": "Ibis Luxuries",
		"price": 650,
		"amenities": ["tv", "geyser", "wifi", "meals"],
		"address": {
			"city": "Mumbai"
		}
	}, 
	{
		"name": "Paris Knight",
		"price": 450,
		"amenities": ["tv", "meals"],
		"address": {
			"city": "New Delhi"
		}
	}, 
	{
		"name": "Citrus Hotel",
		"price": 150,
		"amenities": [],
		"address": {
			"city": "New Delhi"
		}
	}
]

Connecting To A MongoDB Server

  1. Install Mongosh.

  2. Connect using the following command.

mongosh <connection-string>

To obtain a connection string of your MongoDB Atlas instance, go here.

Operations

Show databases

show databases

Create database

use <database-name>

Show collections

show collections

Insert

Insert a single record

db.hotels.insertOne({ 
	"name": "Blue Royal",
	"price": 200,
	"amenities": ["tv", "geyser", "wifi"],
	"address": {
		"city": "Mumbai"
	}
});

Insert multiple records

db.hotels.insertMany([
	{
		"name": "Panama Lagoon",
		"price": 300,
		"amenities": ["tv", "wifi"],
		"address": {
			"city": "Bangalore"
		}
	}, 
	{
		"name": "Ibis Luxuries",
		"price": 650,
		"amenities": ["tv", "geyser", "wifi", "meals"],
		"address": {
			"city": "Mumbai"
		}
	}, 
	{
		"name": "Paris Knight",
		"price": 450,
		"amenities": ["tv", "meals"],
		"address": {
			"city": "New Delhi"
		}
	}, 
	{
		"name": "Citrus Hotel",
		"price": 150,
		"amenities": [],
		"address": {
			"city": "New Delhi"
		}
	}
])

Drop a collection

db.hotels.drop()

Basic queries

List all records of a collection

db.hotels.find()

Find a record by name

db.hotels.find({ name: "Paris Knight" })

Projection

List all records but only the “name” property

db.hotels.find({}, {name: 1})

// skip _id field as well
db.hotels.find({}, {name: 1, _id: 0})

Limiting records

// Get only one record
db.hotels.find().limit(1)

Sorting records

// Sort by price 'low to high'
db.hotels.find().sort({price: 1})

Counting records

db.hotels.countDocuments()

Advanced queries

List hotels which cost under $400/night

db.hotels.find({price: {$lt: 300}})

List all hotels in certain city

db.hotels.find({"address.city": "Mumbai"})

List all hotels in multiple cities

db.hotels.find({"address.city": {$in: ["New Delhi", "Mumbai"]}})

List all hotels with a tv and a geyser

db.hotels.find({amenities: {$all: ["tv", "geyser"]}})

List all hotels with a tv and a geyser with price more than $200/night

db.hotels.find({amenities: {$all: ["tv", "wifi"]}, price: {$gt: 200}})

See the list of all operators here.

Update

Change the price of hotel “Blue Royal”

db.hotels.updateOne(
	{name: "Blue Royal"}, 
	{$set: {price: 250}}
);

Add a country to a hotel

db.hotels.update(
	{name: "Ibis Luxuries"},
	{$set: {"address.country": "india"}}
)

Add an amenity to a hotel

db.hotels.updateOne(
	{name: "Ibis Luxuries"},
	{$addToSet: {amenities: "spa"}}
);

Remove an amenity from a hotel

db.hotels.updateOne(
	{name: "Ibis Luxuries"},
	{$pull: {amenities: "spa"}}
);

Unset a property

db.hotels.update(
	{name: "Ibis Luxuries"},
	{$unset: {"address.country": ""}}
)

Update many records

db.hotels.updateMany({}, {$inc: {price: 20}})

Upsert

If a matching record is not found, it will be inserted instead.

db.hotels.update(
	{name: "Gloria Paradise"},
	{$set: {price: 350, amenities: ["tv"]}},
	{upsert: true}
)

Delete

Delete by name

db.hotels.deleteOne({name: 'Gloria Paradise'})

Delete by a nested property

db.hotels.deleteMany({"address.city": "New Delhi"})

Delete all records

db.hotels.deleteMany({})

Become 1% better, every week

We send you the latest news, articles, and tutorials about full stack development every week. Drop your email to start skilling up and stay ahead in the game!