📝 Notes on basic CRUD operations in mongoDB
mongod
in the terminalmongo
on a separate terminal.NOTE : Sometimes the mongod server is already running in the background in ubuntu. So first just try running the command mentioned in the second step i.e.
mongo
. If it doesn’t work then start the server and the interactive shell, one after another respectively.
After successfully entering the interactive shell try out the following stuff :
use db_name
where db_name is the name of the database you want to create.
use cars
show dbs
admin 0.000GB
config 0.000GB
local 0.000GB
temp 0.000GB
NOTE : The cars db created in the earlier command isn’t visible as it has no data in it. Once a collection is created it will be visible on executing the above command.
db
mongoDB
what tables are in SQL
.mongoDB
unlike SQL
.db.createCollection('collection_name')
db.createCollection('car_details')
mongoDB
when you make an entry in a collection that entry is called a document i.e. a row in terms of SQL
db.collection_name.insert(data)
db.car_details.insert(
{
'brand': 'chevrolet',
'model_name': 'beat',
'type': 'hatchback',
'engine': {
'fuel': 'petrol',
'cc': 1000,
'bhp': 100
},
'color': ['red', 'blue', 'green'],
'safety': {
'sb_warn': false
},
'price' : 400000
}
)
db.collection_name.insert([{},{},....])
find()
function.
db.car_details.find()
pretty()
function
db.car_details.find().pretty()
find()
output.find()
function.
db.car_details.find({ 'type' : 'sedan' }).pretty()
SQL
.
db.car_details.find({
'price' : { '$lte' : 500000 }
}).pretty()
Logical operators can be used to filter documents in mongoDB using the following specifications:-
Eg:- (AND)
db.car_details.find({'$and' : [
{ 'type' : 'sedan' },
{ 'price' : { '$gte' : 500000 } }
]}).pretty()
Notice that the following command will also output the same result as the previous, this is because the default operation in case of multiple filters is an AND operation.
db.car_details.find({
'type' : 'sedan' ,
'price' : { '$gte' : 500000 }
}).pretty()
db.car_details.find({'$or': [
{ 'type' : 'sedan' },
{ 'price' : { '$gte' : 500000 } }
]}).pretty()
sort()
db.car_details.find().sort(
{'price' : -1}
).pretty()
limit()
db.car_details.find().limit(3).pretty()
count()
count()
function can be used
db.car_details.find().count()
$exists
db.car_details.find({
'safety.airbags': {'$exists' : true}
}).pretty()
$regex
Eg:-
db.car_details.find({
'model_name' : { $regex: /^e/ }
}).pretty()
There are 3 functions associated to updation of documents in mongoDB :-
updateOne()
db.car_details.updateOne(
{ 'model_name' : 'cruze' },
{ '$set' : { 'price' : 1500000 } }
)
updateMany()
db.car_details.updateMany(
{ },
{ '$rename' : { 'body_type' : 'type' } }
)
db.car_details.replaceOne(
{ 'model_name' : 'jazz' },
{
'brand': 'honda',
'model_name': 'jazz',
'type': 'hatchback',
'engine': {
'fuel': 'diesel',
'cc': 1150,
'bhp': 115
},
'color': ['silver','orange','white'],
'safety': {
'airbags' : 'front',
'sb_warn': true
},
'price' : 750000
}
)
One or many documents can be deleted in mongoDb using the following commands:-
deleteOne()
db.car_details.deleteOne(
{ 'model_name' : 'city' }
)
deleteMany()
This method deletes the all the documents that match the conditions specified. If no condition is specified then all documents in the collection are deleted.
Eg:-
db.car_details.deleteMany(
{ 'body_type' : 'suv' }
)
db.collection_name.drop()
db.car_details.drop()
db.dropDatabase()
NOTE : The official documentation is more than sufficient to get a hold of both basic and advanced concepts in mongoDB as it has an exhaustive no. of examples associated with every functionality. So, it is recommended to first have a look at the documentation before searching for a tutorial.