.NET 云原生架構師訓練營(模塊二 基礎鞏固 MongoDB 介紹和基礎)--學習筆記
2.5.1 MongoDB -- 介紹
mysql vs mongo
快速開始
mysql vs mongo
| 對比 | mysql | mongo |
|---|---|---|
| 數(shù)據(jù)存儲 | table 二維表結構,需要預先定義結構 | json 類文檔,不需要預先定義結構。可隨意新增或刪除字段,新增字段不會對已存在的字段產(chǎn)生影響 |
| 查詢語法 | sql (structured query language) | mongo |
| 索引 | 如果不定義索引,則進行全表掃描 | 如果不定義索引,則進行全表掃描 |
| 集群 | 支持主從復制 | 內(nèi)置副本集、分片、和自動選舉 |
| 場景 | 關系型結構,在多行插入時需要事務保障 | 實時數(shù)據(jù)分析、內(nèi)容管理、iot設備、移動設備(事務需要有內(nèi)置副本才可以做) |
| 數(shù)據(jù)結構 | 結構化、數(shù)據(jù) schema 定義清晰 | 未知數(shù)據(jù)結構類型 |
| 風險 | sql 注入攻擊 | 相對來說風險更低 |
| 分析 | 確實需要關系型數(shù)據(jù)庫來保障 | 寫入并發(fā)高,沒有 DBA |
快速開始
安裝 mongo in docker
docker run -it --volume=/root/docker/mongo01/data:/data/db -p 27017:27017 --name mongo01 -d mongo
robt 3t 下載地址:
https://download.studio3t.com/robomongo/windows/robo3t-1.4.2-windows-x86_64-8650949.exe
新增數(shù)據(jù)庫books,新增集合author
增刪改查
// 插入
db.author.insertOne({"name":"mingson", "age":25})
db.author.insertOne({"name":"jesse", "age":18})
db.author.insertOne({"name":"bobo", "age":18})
// 查詢
db.getCollection('author').find({"name":"mingson"})
db.getCollection('author').find({"name":{$eq:"mingson"}})
// 更新
db.author.updateOne({"name":"mingson"},{$set:{"age":20}})
// 刪除
db.author.deleteOne({"name":"bobo"})
// 返回字段,1返回,0不返回
db.getCollection('author').find({"name":"mingson"},{"name":1,"_id":0})
2.5.2 MongoDB -- 基礎
mongo db 文檔:
https://docs.mongodb.com/manual/introduction/
中文 mongo db 手冊:
https://mongoing.com/docs/tutorial/insert-documents.html
數(shù)據(jù)庫/集合/文檔
database/collection/document
| mongo | mysql |
|---|---|
| database | database |
| collection | table |
| document | row |
| filed | column |
數(shù)據(jù)庫
數(shù)據(jù)庫的名稱是大小寫敏感
不能包含以下字符(win):/\."$*<>:|?
不能包含以下字符(unix/linux):/\."$
不能超過64個字符
集合
不能包含$
不能為空,不能包含null
不能以system.開頭
字段名
不能為空,不能包含null
頂級字段不能以$開頭
_id是保留字段名稱
BosnTypes
https://mongoing.com/docs/reference/bson-types.html
| string | string |
|---|---|
| bool | Boolean |
| int | int |
| long | long |
| decimal | decimal |
| double | double |
| date | date |
| timestamp | timestamp |
| null | null |
object
array
objectid
regex
javascripe
課程鏈接
評論
圖片
表情
