Following commands can be used in mongo db shell.
show dbs
- list of all the databases
use shops
- switch to a specific database or creates a database (shops)
db.createCollection("products")
- creating the collection (products)
show collections
- display a list of all the collections
db.products.insertOne({"name":"prod_name1"})
- insert a document into a collection
db.products.insertMany([{"name":"prod_name2"},{"name":"prod_name3"}])
- insert many products in one command
db.products.insertOne({"name":"test_prod4","vendor":{"name":"vendor_1","address":"address_1"}})
- insert many products in one command with multiple objects
db.student.insertOne({"name":"test_prod5",,"vendor":{"name":"vendor_1","address":{"address_line1":"line1","address_line2":"line2","city":"city"}}})
- insert many products in one command with multiple objects
db.products.find()
- retrieve all documents
db.products.update({name:"test_prod1"},{$set:{price:"120.00"}})
- update a document in the "products" collection. updates first document only for name:"test_prod1"
db.student.update({"name":"test_6"},{$set:{age:26}},{multi:true})
- updating all document
db.student.update({_id:ObjectId("64dc5856e4cebb07084d8c45")},{$unset:{price:"120.00"}})
- updating the document using object id and remove price attribute
db.teaches.update({"th_name":"test_th_2"},{$set:{student:[{"std_name":"test_1"},{"std_name":"test_2"}]}})
- adding the Student object field
db.student.find({$and:[{"name":"test_4"},{"age":"40"}]})
- AND operation. returns all objects with name =test_4 and age=40
db.student.find({$or:[{"name":"test_4"},{"name":"test_2"}]})
- OR operation and returns name = test_2 or test_4
db.student.drop()
- drop student collection
db.teaches.remove({"name":"t_name1"})
- remove document name =t_name1
db.teaches.deleteOne({"name":"t_name2"})
- remove one document with name=t_name2
db.student.aggregate([{$lookup:{from:"teaches",localField:"std_name",foreignField:"student.std_name",as:"studentDetails"}}])
- aggregation operation in MongoDB to join the "student" collection with the "teaches" collection
db.mycol.find().pretty()
- return results in formatted way