Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- Start MongoDB in cmd -> mongo
- Show all databases -> show dbs
- Select/Create database -> use <DB name>
- > use circus
- Show all collections in a database -> show collections
- Create collection -> db.createCollection(<name in quotes>)
- > db.createCollection("clowns")
- ---------[C] R U D----------
- Create one document -> db.<collection_name>.insertOne(<field and values in curlies>)
- > db.clowns.insertOne({name:"Puspendu", skill:"blabbering", salary:12000, partner:"Aditi", required:true})
- Create many documents -> db.<collection_name>.insertMany([<field and values in curlies>, <field and values in curlies>, ...])
- > db.clowns.insertMany([{name:"Prianshu", skill:"coding", salary:50000, partner:"Priyanka", required:true}, {name:"Sankhadip", skill:"running"}])
- ---------C [R] U D----------
- Read docs from a collection -> db.<collection_name>.find(<condition in curlies>, <projection in curlies>).pretty()
- > db.clowns.find().pretty()
- > db.clowns.find({name:"Sankhadip"}, {}).pretty()
- > db.clowns.find( {},{_id:0, name:1}).pretty()
- > db.clowns.find( {salary:{$gt:13000}}, {_id:0, name:1}).pretty()
- > db.clowns.find( {salary:{$exists:false}}, {_id:0, name:1}).pretty()
- ---------C R [U] D----------
- Update one document -> db.<collection_name>.updateOne(<selection condition>, <set field values>)
- > db.clowns.updateOne({name:"Sankhadip"}, {$set:{partner:"Uber Kaku"}})
- > db.clowns.updateOne({name:"Puspendu"}, {$set:{partner:"Soumya Sen", skill:"BIZNEZZ"})
- Update many documents -> db.<collection_name>.updateMany(<selection condition>, <set field values>)
- > db.clowns.updateMany({salary:{$gt:20000}}, {$set:{required:false}})
- Replace one document -> db.<col_name>.replaceOne(<selection condition>, <new fields and values>)
- > db.animals.replaceOne({name:"Pratim"}, {name:"Bawaseer", type:"Ape", habitat:"South Calcutta"})
- ---------C R U [D]----------
- Delete one document -> db.<col_name>.deleteOne(<selection condition>)
- > db.animals.deleteOne({name:"Sayantan"})
- > db.animals.deleteOne({}) \\Delete 1st one
- Delete many document -> db.<col_name>.deleteMany(<selection condition>)
- > db.animals.deleteMany({skill:"Office Work"})
- > db.animals.deleteMany({}) \\Delete all
- -------------Extra Stuff----------------
- Drop collection -> db.<col_name>.drop()
- > db.tax_records.drop()
- Delete database -> db.dropDatabase()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement