MongoDB Cheat Sheet

MongoDB is a source-available cross-platform document-oriented database program. Classified as a NoSQL database program, MongoDB uses JSON-like documents with optional schemas.

to start server (mongod is mongo deamon)

mongod

to start shell

mongo
show dbs;
create/use database

Change database

use db_name;

show current database

db;

Collections are similar to table

show collections;
db.createCollection("students");

Drop collection/table

db.students.drop();

drop database

db.dropDatabase();

insertOne

db.students.insertOne({_id: 1, name: "shailesh"});

insertMany

db.students.insertMany([{_id: 2, name: "bagde"}, {_id: 3, name: "sheldon"}]);

count document

db.students.count();

select all documents/records

db.students.find();

select with projection

db.students.find({},{_id: 0});
db.students.find({},{name:0});

exists operator

db.students.find({age: {$exists: false}});

COMPARISON OPERATORS

in operator

db.students.find({_id: {$in: [2,3]}});

not equal operator

db.students.find({_id: {$ne: 2}});

equal operator

db.students.find({_id: {$eq: 2}});

less than $lt

less than equal $lte

greather than $gt

greater than equal $gte

LOGICAL OPERATORS

$and operator

db.students.find({$and : [{_id : {$gt:1}}, {_id: {$lt: 4}}]})

$or operator

db.students.find({$or : [{name : "bagde"}, {name: "shailesh"}]})

like case insensitive

db.students.find({name: /h/i})

exact match

db.students.find({name: /^BagDe$/i})

UPDATE

db.students.updateOne({_id: 1}, {$set : {age: 23}});
db.students.updateMany({age : {$exists: false}}, { $set : {age : 25}})
db.students.updateMany({}, {$set: {address: {city: "Mumbai", state: "MH"}}})
db.students.updateOne({_id: 4}, { $set : {address : {city: "Nagpur"}}});

Unset/Delete Fields

db.students.updateMany({}, {$unset : {city: ""}});

search within sub document

db.students.find({"address.city" : "Nagpur"}).pretty();

Delete Records

db.students.deleteOne({ $or : [{ age : {$lt : 25}}, {name : "sheldon"}] })

DISTINCT

db.students.distinct(“city”)

ORDER BY

db.students.find().sort({name: -1})

LIMIT

db.students.find().limit(10)
db.students.find().skip(5).limit(10)

Aggregation

Aggregate on all documents
db.emps.aggregate([{$group: {
	_id : null,
	total : {$sum : "$salary"},
	average : {$avg: "$salary"},
	min : {$min: "$salary"},
	max : {$max: "$salary"},
	count : {$sum: 1}	
}}]).pretty()


Aggregate on group by address
db.emps.aggregate([{$group: {
	_id : { address: "$address" },
	total : {$sum : "$salary"},
	average : {$avg: "$salary"},
	min : {$min: "$salary"},
	max : {$max: "$salary"},
	count : {$sum: 1}	
}}]).pretty()

ForEach Map

db.emps.find().forEach(x => { print(x.name) })

db.emps.find({salary: { $exists: -1 } }, {"salary": 1, _id: 0}).map(x => x.salary * 0.10)

MongoDB Assignment for Practice

var docs = [
{"name":"neha","contact_number":"9833910534","address":"mumbai","salary":30000,"employee_id":98821,"role":"manager"},
{"name":"mina","contact_number":"9833910535","address":"thane","salary":32000,"employee_id":98823,"role":"sales"},
{"name":"pankaj","contact_number":"9833910536","address":"bhopal","salary":40000,"employee_id":98824,"role":"hr"},
{"name":"mareena","contact_number":"9833910537","address":"meerut","salary":45000,"employee_id":98825,"role":"support"},
{"name":"pooja","contact_number":"9833910538","address":"delhi","salary":50000,"employee_id":98826,"role":"developer"},
{"name":"namita","contact_number":"9833910539","address":"surat","salary":52000,"employee_id":98820,"role":"sales"},
{"name":"sneha","contact_number":"9833910510","address":"baroda","salary":55000,"employee_id":98827,"role":"support"},
{"name":"anjali","contact_number":"9833910511","address":"ahmedabad","salary":60000,"employee_id":98828,"role":"tester"},
{"name":"harsha","contact_number":"9833910512","address":"mumbai","salary":20000,"employee_id":98829,"role":"operations"},
{"name":"varun","contact_number":"9833910512","address":"mehsana","salary":56000,"employee_id":98831,"role":"tester"},
{"name":"preeti","contact_number":"9833910513","address":"noida","salary":87000,"employee_id":98832,"role":"developer"},
{"name":"madhu","contact_number":"9833910525","address":"bangalore","salary":22000,"employee_id":98833,"role":"sales"}
];

db.createCollection("emps");
db.emps.insertMany(docs);
db.emps.find();

var docs = [{“name”:”neha”,”contact_number”:”9833910534″,”address”:”mumbai”,”salary”:30000,”employee_id”:98821,”role”:”manager”},{“name”:”mina”,”contact_number”:”9833910535″,”address”:”thane”,”salary”:32000,”employee_id”:98823,”role”:”sales”},{“name”:”pankaj”,”contact_number”:”9833910536″,”address”:”bhopal”,”salary”:40000,”employee_id”:98824,”role”:”hr”},{“name”:”mareena”,”contact_number”:”9833910537″,”address”:”meerut”,”salary”:45000,”employee_id”:98825,”role”:”support”},{“name”:”pooja”,”contact_number”:”9833910538″,”address”:”delhi”,”salary”:50000,”employee_id”:98826,”role”:”developer”},{“name”:”namita”,”contact_number”:”9833910539″,”address”:”surat”,”salary”:52000,”employee_id”:98820,”role”:”sales”},{“name”:”sneha”,”contact_number”:”9833910510″,”address”:”baroda”,”salary”:55000,”employee_id”:98827,”role”:”support”},{“name”:”anjali”,”contact_number”:”9833910511″,”address”:”ahmedabad”,”salary”:60000,”employee_id”:98828,”role”:”tester”},{“name”:”harsha”,”contact_number”:”9833910512″,”address”:”mumbai”,”salary”:20000,”employee_id”:98829,”role”:”operations”},{“name”:”varun”,”contact_number”:”9833910512″,”address”:”mehsana”,”salary”:56000,”employee_id”:98831,”role”:”tester”},{“name”:”preeti”,”contact_number”:”9833910513″,”address”:”noida”,”salary”:87000,”employee_id”:98832,”role”:”developer”},{“name”:”madhu”,”contact_number”:”9833910525″,”address”:”bangalore”,”salary”:22000,”employee_id”:98833,”role”:”sales”}];

  1. show employees in descending order – salary
  2. show all employees from Mumbai
  3. show all employees having salary more 50000
  4. show sum of salary from emps collection
  5. show all distinct address
  6. show names of employees having max salary
  7. show employees having 2nd highest salary
  8. count employees from mumbai only
  9. show all female employees
  10. show all male employees

Leave a Reply

Your email address will not be published. Required fields are marked *