MongoDB教育訓練-03
count 儘量不要用
db.inspections.countDocuments() #精確查詢(1)
db.inspections.count() #
insertOne 插入一筆資料
#插入一條資料
insertOne
id 本身會包含時間
getTimestamp #取得時間
insertMany 插入多筆資料
有序插入&無序插入
{ordered: true | false}
- 有序可保證順序,速度較慢,中間錯誤會全部返回
- 使用無序不影響,中間錯誤紀錄
搜尋
- find()
- findOne()
返回特定欄位
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
curosr 10分鐘會被丟棄
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"}}
範圍查詢
Boolen 查詢
$or : [ {species:"cat", color:"black"},
{species:"dog", color:"brown"}}]
#等於
where (species = "cat" and color = "black") or (species = "dog" and color = "brown")
db.grades.find({ student_id : {$lt: 65}})
db.inspections.find({$or :[ {result:"Pass"},{result:"Fail"}]})