Web開發學習筆記22 — SQL、NoSQL、MongoDB簡易操作


Posted by Teagan Hsu on 2021-01-19

什麼是SQL?

SQL(Structured Query Language)是一種結構化查詢語言,專門用來管理與檢索關聯式資料庫(Relational database)。


什麼是關聯式資料庫(Relational database)?

關聯式資料庫是根據關聯模型(Relational model)所設計、開發的資料庫。管理關聯式資料庫的系統稱為RDBMS(Relational Database Menagement System),例如:Oracle Database、Microsoft SQL Server、MySQL、PostgreSQL、DB2、FileMaker、H2 Database都是RDBMS。

關聯式資料庫的特徵在於資料是用表格(table)所儲存的,每一筆資料都是table中的一個 record。而且每個table之間具有明確的關係。


什麼是NoSQL?

NoSQL等於"Not only SQL",就是指其他不是使用SQL作為查詢資料庫語言的資料庫管理系統,例如MongoDB、DynamoDB、Cassandra、Couchbase等。NoSQL比起SQL更加靈活、不用被侷限在table中,它更重視資料最終的結果,舉例來說,有一篇fb文章在這一秒剛好收集到100個讚,但A使用者可能這一秒就看到結果,B使用者可能會延遲十幾秒才會看到文章有100個讚,這時過程的延誤性可以被接受,只要結果準確就好。因為fb要處理的資料太過龐大,使用NoSQL可以更加靈活。


MongoDB

CRUD操作

  • 連結Mongo伺服器
    mongo
    
  • 現在的database
    db
    
  • 看所有的database(如果還沒傳入資料,則不會顯示)
    show dbs
    
  • 使用某個database(如果沒有此名稱的database,則會自動建立一個)
    use [db名稱]
    
  • 看此database中所有的collections
    show collections
    
  • insert 插入、新增資料(可以設定_id,如果無設定則自動給予一組_id
    db.collection.insert()
    
  • 檢索剛剛插入的資料,搜尋collection,db.collection.find會返回cursor
    db.collection.find() //查詢全部
    db.collection.find({ name: "Amy" }) //查詢特定資料
    
  • update 更新
    db.collection.update({query, operator})
    db.collection.updateOne({query, operator})
    db.collection.updateMany({query, operator})
    //example:
    db.collection.update({ age: 13 }, { $set: { breed: "Corgi" } })
    db.collection.update({ age: 11 }, { $currentDate: { dateChanged: true } })
    
  • delete 刪除
    db.collection.deleteOne({filter})
    db.collection.deleteMany({filter})
    //example:
    db.collection.deleteOne({ name: "Hebe" })
    db.collection.deleteMany({ breed: "Corgi" })
    

Query and Projection Operators

Comparison

Name Description
$eq equal 等於
$gt greater than 大於
$gte greater than and equal to 大於等於
$in 匹配符合陣列中的特定值
$lt less than 小於
$lte less than and equal to 小於等於
$ne 匹配所有不等於的特定值
$nin 匹配"不符合"陣列中的特定值

範例:

db.collection.find({ qty: { $eq: 300 } })  //$eq
db.collection.find({ qty: { $in: [300, 400] } })  //$in

Logical

Name Description
$and and
$not not
$nor nor
$or or

範例:

db.collection.find({ $and: [{ qty: { $exists: true } }, { qty: { $ne: 200 } }] })  //找qty存在並且值不等於200

db.collection.find({ $nor: [{ qty: 200 }, { price: { $lt: 100 } }] })  //qty不等於200以及price不小於100的

Element

Name Description
$exists 匹配document有沒有特定field
$type 資料型別等於BSON Type

範例:

db.collection.find({ qty: { $exists: true } })
db.collection.find({ qty: { $type: 1 } })

Update Operators

Fields

Name Description
$currentDate true OR false
$inc increase
$min 如果特定值比現存field值小,更新field值至特定值
$max 如果特定值比現存field值大,更新field值至特定值
$unset 移除特定filed

範例:

//example:
{ "_id" : 1, "qty" : 300, "price" : 100 }
{ "_id" : 2, "qty" : 100, "price" : 100 }
{ "_id" : 3, "qty" : 100, "price" : 300 }
{ "_id" : 4, "qty" : 300, "price" : 300 }

//$min
db.dogs.update({ _id: 1 }, { $min: { qty: 50 } })
db.dogs.find()
//{ "_id" : 1, "qty" : 50, "price" : 100 }
//{ "_id" : 2, "qty" : 100, "price" : 100 }
//{ "_id" : 3, "qty" : 100, "price" : 300 }
//{ "_id" : 4, "qty" : 300, "price" : 300 }

//$max
db.dogs.find()
//{ "_id" : 1, "qty" : 50, "price" : 100 }
//{ "_id" : 2, "qty" : 200, "price" : 100 }
//{ "_id" : 3, "qty" : 100, "price" : 300 }
//{ "_id" : 4, "qty" : 300, "price" : 300 }

//unset
db.dogs.update({ _id: 3 }, { $unset: { qty: "", price: "" } })
db.dogs.find()
//{ "_id" : 1, "qty" : 50, "price" : 100 }
//{ "_id" : 2, "qty" : 200, "price" : 100 }
//{ "_id" : 3 }
//{ "_id" : 4, "qty" : 300, "price" : 300 }

參考資料:

  1. SQL/NoSQL是什麼?認識資料庫管理系統DBMS
  2. 関係データベース - wiki
  3. The MongoDB 4.4 Manual
  4. MongoDB 學習筆記

#SQL #NoSQL #mongoDB







Related Posts

Hoisting

Hoisting

九月 秋天 心裡暖烘烘的

九月 秋天 心裡暖烘烘的

人性較量Day01~圖靈測試(Turing test)

人性較量Day01~圖靈測試(Turing test)


Comments