跳到主內容

MongoDB教育訓練-03

image-1639632341110.png

 

 

image-1639632437556.png

count 儘量不要用

db.inspections.countDocuments() #精確查詢(1)

db.inspections.count() #

image-1639632701684.png

image-1639632747819.png

insertOne 插入一筆資料

#插入一條資料
insertOne 

image-1639633018479.png

id 本身會包含時間

getTimestamp #取得時間

image-1639633066887.png


insertMany 插入多筆資料


 

image-1639633247450.png


有序插入&無序插入

{ordered: true | false}

  • 有序可保證順序,速度較慢,中間錯誤會全部返回
  • 使用無序不影響,中間錯誤紀錄

image-1639633329359.png

 


搜尋

  • find()
  • findOne()

image-1639633549742.png

image-1639633608684.png


返回特定欄位

image-1639633633419.png

image-1639633835708.png

 

Find 搜尋20筆資料

for(var i = 0; i < 200; i++) db.taxis.insertOne({plate: i})

# plate = 5 
db.taxis.find({plate:5})

# plate >= 5 
db.taxis.find({plate:{$gt: 5}})

#下20筆
it 

image-1639633889739.png

image-1639634201304.png

image-1639634238534.png

 

image-1639634282001.png

image-1639634354192.png

curosr 10分鐘會被丟棄

image-1639634486948.png

image-1639634849381.png

db.diaries.drop()

db.diaries.insertMany([
{ name: "dug", day: ISODate("2014-11-04"), txt: "went for a walk"},
{ name: "dug", day: ISODate("2014-11-06"), txt: "saw a squirrel"},
{ name: "ray", day: ISODate("2014-11-06"), txt: "met dug in the park"},
{ name: "dug", day: ISODate("2014-11-09"), txt: "got a treat"}
])

#找出 name = dug

#輸出 name, text

子查詢

# city = "New York" 即可
address.city :"New York"
#{ city:"New York"} 完整匹配
address : { city:"New York"}} 

image-1639635141931.png


範圍查詢

image-1639635381525.png

 


Boolen 查詢

$or : [ {species:"cat", color:"black"},
		{species:"dog", color:"brown"}}]
#等於

where (species = "cat" and color = "black") or (species = "dog" and color = "brown")

image-1639635516284.png

image-1639635768002.png

db.grades.find({ student_id : {$lt: 65}})

db.inspections.find({$or :[ {result:"Pass"},{result:"Fail"}]})