Advertisement
AnindyaBiswas

CRUD

May 16th, 2023
223
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.38 KB | None | 0 0
  1. Start MongoDB in cmd -> mongo
  2.  
  3. Show all databases -> show dbs
  4.  
  5. Select/Create database -> use <DB name>
  6. > use circus
  7.  
  8. Show all collections in a database -> show collections
  9.  
  10. Create collection -> db.createCollection(<name in quotes>)
  11. > db.createCollection("clowns")
  12.  
  13.  
  14.  
  15. ---------[C] R U D----------
  16.  
  17. Create one document -> db.<collection_name>.insertOne(<field and values in curlies>)
  18. > db.clowns.insertOne({name:"Puspendu", skill:"blabbering", salary:12000, partner:"Aditi", required:true})
  19.  
  20. Create many documents -> db.<collection_name>.insertMany([<field and values in curlies>, <field and values in curlies>, ...])
  21. > db.clowns.insertMany([{name:"Prianshu", skill:"coding", salary:50000, partner:"Priyanka", required:true}, {name:"Sankhadip", skill:"running"}])
  22.  
  23.  
  24.  
  25. ---------C [R] U D----------
  26.  
  27. Read docs from a collection -> db.<collection_name>.find(<condition in curlies>, <projection in curlies>).pretty()
  28. > db.clowns.find().pretty()
  29. > db.clowns.find({name:"Sankhadip"}, {}).pretty()
  30. > db.clowns.find( {},{_id:0, name:1}).pretty()
  31. > db.clowns.find( {salary:{$gt:13000}}, {_id:0, name:1}).pretty()
  32. > db.clowns.find( {salary:{$exists:false}}, {_id:0, name:1}).pretty()
  33.  
  34.  
  35.  
  36. ---------C R [U] D----------
  37.  
  38. Update one document -> db.<collection_name>.updateOne(<selection condition>, <set field values>)
  39. > db.clowns.updateOne({name:"Sankhadip"}, {$set:{partner:"Uber Kaku"}})
  40. > db.clowns.updateOne({name:"Puspendu"}, {$set:{partner:"Soumya Sen", skill:"BIZNEZZ"})
  41.  
  42. Update many documents -> db.<collection_name>.updateMany(<selection condition>, <set field values>)
  43. > db.clowns.updateMany({salary:{$gt:20000}}, {$set:{required:false}})
  44.  
  45. Replace one document -> db.<col_name>.replaceOne(<selection condition>, <new fields and values>)
  46. > db.animals.replaceOne({name:"Pratim"}, {name:"Bawaseer", type:"Ape", habitat:"South Calcutta"})
  47.  
  48.  
  49.  
  50.  
  51. ---------C R U [D]----------
  52.  
  53.  
  54. Delete one document -> db.<col_name>.deleteOne(<selection condition>)
  55. > db.animals.deleteOne({name:"Sayantan"})
  56. > db.animals.deleteOne({}) \\Delete 1st one
  57.  
  58.  
  59. Delete many document -> db.<col_name>.deleteMany(<selection condition>)
  60. > db.animals.deleteMany({skill:"Office Work"})
  61. > db.animals.deleteMany({}) \\Delete all
  62.  
  63.  
  64.  
  65.  
  66. -------------Extra Stuff----------------
  67.  
  68. Drop collection -> db.<col_name>.drop()
  69. > db.tax_records.drop()
  70.  
  71. Delete database -> db.dropDatabase()
  72.  
  73.  
  74.  
  75.  
  76.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement