Thursday, March 18, 2021

Mongo DB

1. show dbs: show database names  

<!--Create Database & records—>
  1. use [Database_Name] : create/switch to database 
  2. db : show your current db
  3. db.[tableName].insertOne({_id: 1, name: “Pen” }) : insert new record
  4. show collections : show all the tables in the db 

<!— Read Database  —>
  1. db.products.find() : select all records in the table “products”
  2. db.products.find({ [column_name] : [value] }) : search a specific column and display all records. 
  3. db.products.find({ price : {$gt: 1}}) : select * where price > 1 
  4. db.products.find({ [column_name] : [value}, { [fields that we want back] })

<!— Update records—>
  1. db.[tableName].updateOne({ [column_ name] : [field] }, {$set: { [column_name] : [value] }})
  2. db.products.find() : select all records in the table “products”

<!— Delete records—>
  1. db.[tableName].deleteOne({ [column_name] : [value]})
  2. db.products.find() : select all records in the table “products”

<!— Relationship —>
1 Rubber to M review

On Mac
[Connect MongoDB to node.js]
  1.  mkdir [folderName] : make a new directory
  2. cd [folderName] : change directory
  3. touch app.js : create a new file “app.js"
  4. npm init -y: say yes to everything for npm init
  5. npm i mongodb : install mongodb driver
  6. code app.js : Open up app.js file in vscode.

No comments:

Post a Comment