MongoDB CRUD Commands for Beginners
If you are very new to MongoDB and do not have MongoDB installed in your machine, refer the below link to install mongodb on windows 7.
http://www.ngdeveloper.com/install-mongodb-windows-7/
This is how data stores in MongoDB,
Database -> collections -> Documents (structured and unstructured)
Where In RDBMS,
Schema -> Database -> Tables -> Rows and columns.
Variable “db” is the mongodb shell variable, it is set to the current database.
For eg:
db.product.insert({type:"Books",name:"Eat that frog", store:"Flipkart",price:"150", offer="50", offer_type="perc"})
here,
- db -> mongodb shell variable to use the selected database.
- product -> collection
- {type:”Books”,name:”Eat that frog”, store:”Flipkart”,price:”150″, offer=”50″, offer_type=”perc”} -> document
Table of Contents
MongoDB CRUD Commands:
Collections & Documents refers same in mongodb. Mongodb can be used for both structured and unstructured data’s.
To List all databases:
show dbs
To list the collections:
show collections (or) show tables
To select/create a new database:
use products
Create Operations:
To insert/create the values to products:
Execute the below documents one by one to insert it into products collection. db.products.insert({type:"Books",name:"Eat that frog", store:"Flipkart",price:150, offer:50, offer_type:"perc"}) db.products.insert({type:"Books",name:"Goals", store:"Amazon",price:"240", offer:"50", offer_type:"amount"}) db.products.insert({type:"Mobiles",name:"One Plus 3", store:"Flipkart",price:"20000", offer:"10", offer_type:"perc"}) db.products.insert({type:"Mobiles",name:"One Plus 3", store:"Paytm",price:"19800", offer:"20", offer_type:"perc"}) db.products.insert({type:"Mobiles",name:"One Plus 3", store:"Ebay",price:"17500", offer:"30", offer_type:"perc"}) db.products.insert({type:"Books",name:"Goals", store:"Snapdeal",price:"220", offer:"60", offer_type:"amount"}) db.products.insert({type:"Books",name:"No execuses", store:"Amazon",price:"300", offer:"30", offer_type:"perc"})
Read Operations:
To display all the documents from the products collection:
db.products.find()
To display all the documents from the products collection in a pretty way:
db.products.find().pretty()
To display the counts:
db.products.find().count() / db.products.count()
To display the documents which matches the store name as “Flipkart” from the products collection:
db.products.find({store:"Flipkart"})
Display the products price less than 250
db.products.find({price: {$lt:250}}).pretty()
db.products.find({price: {$lt:250}}).pretty()
Display one plus 3 products from all the stores (case sensitive):
db.products.find({name:"One Plus 3"}).pretty()
Display one plus 3 products from all the stores (case insensitive => / and i should be used):
db.products.find({name:/one plUS 3/i}).pretty()
db.products.find({name:/one plUS 3/i}).pretty()
Search query in Mongodb Community:
{"accounts":{$elemMatch: {"couponId":'26282781'}}} {$or:[{"couponId":726}, {"storeSlug":"flipkart-coupons"}]}
Update Operations:
- updateOne => updates only for the first matched result.
- updateMany=> updates all the matches.
db.products.updateMany({"name":"One Plus 3"}, {$set:{price:15000}})
How to truncate the collection ?
db.<collection>.remove({});
Eg: db.coupons.remove({});
make sure to run this command before the above one.
show databases; use <your_database_name>;
Delete Operations:
To drop the collection products:
db.products.drop();
returns true and removes all the documents from the products. remove() function can be used to remove particular collection or can be used to remove all the documents.
We have something called “Upserts” which does insert on update operation when no matches/nothing is there to update. This we will look very clearly in the future posts.