Getting started with MongoDB: The Practical Guide

Introduction
MongoDB is a (NoSQL) document database that provides high performance and rapid scalability. Terminology relationship between RDBMS & MongoDB: Table <=> Collection , Row <=> Document, Column <=> Field. To improve the performance it is recommcommed to use embedded document.
To getting started with MongoDB you don’t need to install or set up an environment of any kind. I’ll tell you the easiest way to begin your journey.
1. Goto MongoDB official site & do the free registration.
2. Follow on-screen instructions to set up MongoDB Atlas cluster, create DB, IP white listing, create user.
3. Download Compass Offical MongoDB Client to explore, modify & visualize data using GUI.
4. Connect Compass to Mongo Atlas cluster using the connection string (full guide available on official site).
Cheatsheet
If you followed the above steps then you are good to go. Now I’m going to list out the most used queries & try to cover them with a short explanation.
db specific operations
#Show All Databases
show dbs#Show Current Database
db#Create Or Switch Database
use studentdb#Drop Database
db.dropDatabase()#Create Collection
db.createCollection('profile')#Show Collections
show collections
CRUD queries
#Insert One Row
db.profile.insertOne({
name: 'John Doe',
rollNo: 1,
age: 10,
gender: 'M',
address: {
city: 'Pune',
state: 'MH',
country: 'IN'
}
})#Insert Multiple Rows
db.profile.insertMany([
{
name: 'John Doe',
rollNo: 1,
age: 10,
gender: 'M'
},
{
name: 'Jane Doe',
rollNo: 2,
age: 11,
gender: 'F'
}
])#Get All Rows
db.profile.find()#Get All Rows Formatted
db.profile.find().pretty()#Get Rows by condition
db.profile.find({ age: 10})#Chaining - limit & Sort(asc:1,desc:-1)
db.profile.find().limit(2).sort({ name: 1 })#Get Rows Count
db.profile.find({ age: 10}).count()#Get specific fields by condition
db.profile.find(
{ age: 10 },
{ _id: 0, name: 1, age: 1 }
)#Update Row - upsert & multi
db.profile.update(
{ age: 10 },
{ name: 'Mike Doe', age: 11},
{ upsert: true, multi: false }
)#Update specific field
db.profile.update({ age: 10 },
{
$set: {
name: 'Mike Doe',
age: 11
}
})#Delete Row
db.profile.remove({ age: 10 })
Other useful queries
#Run Javascript file
load("myScript.js")#Foreach
db.profile.find().forEach(function(x) {
print(“City name: “ + x.address.city)
})#Rename field
db.profile.update({ age: 10 },
{
$rename: {
rollNo: 'no'
}
})#Add Index
db.profile.createIndex({ age: 10 })#Text Search
db.profile.find({
$text: {
$search: "\"Mike Doe\""
}
})#Greater & Less Than
db.profile.find({ age: { $gt: 10} })
db.profile.find({ age: { $gte: 10} })
db.profile.find({ age: { $lt: 10} })
db.profile.find({ age: { $lte: 10} })#Explain
db.profile.explain("executionStats").find({ age: 10 })#Aggregate
db.profile.aggregate([
{
$match: {age : {$gte: 10 } }
},
{
$group: { _id: "$city", total: {$avg: "$age" } }
},
{
$sort: { total: -1 }
}
])
for more info check out the MongoDB cheatsheet here
Python CRUD operation with MongoDB
NOTE: Mongo connection and all operations are performed just for demo purposes only. Please do not use the following snippet as it is in production. for more info & implementation (like proper repo layer, handle expt) checkout the MongoDB blog post
I hope you find something useful. If you like this article then please clap for me…👏👏👏