Simple Basic Commands For MongoDB
1. Create a database named “mydb”
> Use mydb
2. Show all databases
> Show dbs
3. Show currently selected database.
> DB
4. Create a collection named “emp”.
> db.createCollection(“emp”)
5. Show all collections of the selected database.
> Show collections
6. Insert following documents in emp collection:
> db.emp.insert(
[
{ empno : 1,ename : "Sachin",sal : 60000,desige : "Manager",dept : "Purchase" },
{ empno : 2,ename : "Kohali",sal : 50000,desige : "Manager",dept : "Sales" }])
Find()
1. List all documents.
> db.emp.find()
2. List all documents with formatted output.
> db.emp.find.pretty()
3. List the document of an employee whose name is “Sachin”
> db.emp.find({ename:”Sachin”})
4. List the documents of employee whose salary is less than 30000
> db.emp.find({sal : {$lt : 30000}})
5. List employees whose designation is manager and department is sales.
> db.emp.find( { desige : "Manager",dept : "Sales"})
6. List employees whose salary is less than 50000 and designation is manager or department is admin.
> db.emp.find(
{sal : {$lt : 50000},
$or : [
{ desige : "Manager"},{dept : "Admin"}
]})
7. Arrange the records by name in descending order.
> db.emp.find().sort({ename : -1})
8. List first 3 documents of emp collection.
> db.emp.find().limit(3)
1. Skip first 3 documents of emp collection.
> db.emp.find().skip(3)
2. List 3rd and 4th documents of emp collection.
> db.emp.find().limit(2).skip(2)
3. Count no. of employees.
> db.emp.find().count()
4. List distinct designation.
> db.emp.distinct(“desige”)
Aggregate()
1. Calculate annual salary of employees.
> db.emp.aggregate( [ { $project:{_id:0,ename:1,sal:1,desige:1,dept:1,Annual_Salary:{$multiply:["$sal",12]}}}]).pretty()
2. Display no. of employees designation wise.
>db.emp.aggregate( [ { $group : { _id:"$dept",Employees:{$sum:1}}}] )
3. Display total salary of employees department wise.
> db.emp.aggregate( [ { $group: { _id:"$dept",Tot_Sal:{$sum:"$sal"} }} ] )
4. Display employee whose salary is highest.
> db.emp.aggregate( [ { $sort:{sal: -1}}, { $limit: 1} ] )
5. List highest salary of the employee department wise.
> db.emp.aggregate( [ { $group:{ _id : "$dept",Highest_Sal : {$max:"$sal"} } } ] )
6. Calculate total salary of employees department wise and list whose total is greater than 70000.
> db.emp.aggregate( [ { $group: {_id:"$dept",total_sal:{$sum:"$sal"}}},{ $match: {total_sal:{$gt:70000}}}])
Index()
7. Create index on empno field.
> db.emp.ensureIndex( { empno:1} )
8. Check the statistics of the emp collection.
> db.emp.stats()
9. Get the list of all indexes created on the emp collection.
> db.emp.getIndexes()
10. Use hint method.
> db.emp.find( { empno:3}).hint({empno:1})
11. Use explain method.
> db.emp.find( { empno:3} ).hint({ empno:1}).explain()
Update()
12. Increment salary by 2000 whose name is Sachin.
> db.emp.update({ ename:"Sachin"},{$inc:{sal:2000}})
13. Increment salary by 2000 of all the employees.
> db.emp.update({}, { $inc:{sal:2000}},{multi:true})
14. Update designation of Sachin with CEO.
> db.emp.update({ename:"Sachin"},{$set:{desig:"CEO"}})
15. Update designation of Sachin with ‘MD’ and insert as new document.
> db.emp.update({ename:"Sachin"},{$set:{desig:"MD"}},{upsert:true})
16. Add a new field “remark” to document with name “dhoni” and set remark “head”
> db.emp.update({ename:"Dhoni"},{$set:{remaek:"head"}})
17. Remove the added new field.
> db.emp.update({ename:"Dhoni"},{$unset:{remaek:"head"}})
Save()
18. Replace the whole document whose _id is 1.
> db.emp.save( {_id: ObjectId("59860ee940b855fbf3566a20") , empno:1,ename:"Tendulkar",sal:"40000",desig:"Clerk",dept:"Admin"})
Regular Expression :
19. To find documents from the emp collection where the ename begins with “S”. (with and without regex)
> db.emp.find({ename:/^S/})
> db.emp.find({ename:{$regex:"^S"}})
20. To find documents from the emp collection where the ename ends with “n”. (with and without regex)
> db.emp.find({ename:/n$/})
> db.emp.find({ename:{$regex:"n$"}})
21. Modify above query with case insensitivity.
> db.emp.find({ename:/N$/i})
> db.emp.find({ename:{$regex:"n$",$options:"i"}})
22. To find documents from the emp collection where ename has an “a” in any position.
> db.emp.find({ename:/a/})
> db.emp.find({ename:{$regex:".*a.*"}})
Dealing with null values :
23. Update the document with null value in designation field where name is “Kohli”.
> db.emp.update( {ename:"Kohali"}, {$set:{desig:null}} )
24. To search for null values in designation field.
> db.emp.find( {desig:null})
25. To remove designation field having null values in emp collection where name is “Kohli”.
> db.emp.update( { ename:"Kohali"},{$unset:{desig:null}})
DAA : ALGORITHAM :
flocgeo0trich_yo Miranda Burden https://wakelet.com/wake/HLV_pZ5LVqxjuGtwkAktm
ReplyDeletemephasynbi
OguttuYcrusda_1984 Evan Morgan click here
ReplyDeletedaywatchpopep