mongo數據庫的各類查詢語句示例

一、db.users.find() 
  -- db.getCollection('users').find({})
  -- select * from users
  
二、db.users.find({"age" : 27}) 
  -- select * from users where age = 27  
三、db.users.find({"username" : "joe", "age" : 27}) 
  -- select * from users where "username" = "joe" and age = 27  

四、db.users.find({}, {"username" : 1, "email" : 1}) 
  -- select username, email from users  
五、db.users.find({}, {"username" : 1, "_id" : 0}) 
  -- no case  
  -- 即時加上了列篩選,_id也會返回,必須顯式的阻止_id返回

六、db.users.find({"age" : {"$gte" : 18, "$lte" : 30}}) 
  -- select * from users where age >=18 and age <= 30 
  -- $lt(<) $lte(<=) $gt(>) $gte(>=)  
七、db.users.find({"username" : {"$ne" : "joe"}}) 
  -- select * from users where username != "joe"  

八、db.users.find({"ticket_no" : {"$in" : [725, 542, 390]}}) 
  -- select * from users where ticket_no in (725, 542, 390)  
九、db.users.find({"ticket_no" : {"$nin" : [725, 542, 390]}}) 
  -- select * from users where ticket_no not in (725, 542, 390)  

十、db.users.find({"$or" : [{"ticket_no" : 725}, {"winner" : true}]}) 
  -- select * form users where ticket_no = 725 or winner = true  

十一、db.users.find({"username" : {"$in" : [null], "$exists" : true}}) 
  -- select * from users where username is null 
  -- 若是直接經過find({"username" : null})進行查詢,那麼連帶"沒有username"的紀錄一併篩選出來 
十二、db.users.find({"username" : {"$nin" : [null]}}) 
  -- db.users.find({"username" : {"$ne" : null}}) 
  -- select * from users where username is not null
相關文章
相關標籤/搜索