摘要:MongoDB 指令研究
儲存資料
db.inboxs.save({inbox_id:"tomlai",tags:["tom","lai"]})
查詢
db.inboxs.find()
db.inboxs.find({inbox_id:"tomlai"})
陣列 OR 查詢
db.inboxs.find({tags:{"$in":["lai"]}})
陣列AND查詢
db.inboxs.find({tags:{"$all":["lai"]}})
大於小於查詢
db.inboxs.find({age:{"$lt":30,"$gt":20}})
存在查詢
db.posts.find(tags:{$exists:true})
Regular expressions
db.posts.find({author:/^r*/i})
Counting
db.posts.find({author:"tomlai"}).count()
update , add new comment
db.posts.update({i_d:"....",new_info}
new_info{"$push":{comments:new_,comment},"$inc":{comments_count:1}}
設定索引
db.posts.ensureIndex({"comments.author":1})
階層式查詢
db.posts.find({comments.author:"tomlai"})
遞減排序兼取前五個
db.posts.find().sort({date:-1}).limit(5)
db.jobs.findAndModify({query:{},sort:{},update:{$set:{}},new:true})