MongoDB学习整理之查询
查询操作符
1)条件操作符
简单的操作符、>=如下面的代码所示:
//field 大于 value
db.collection.find({"field":{$gt:value}})
//field 小于 value
db.collection.find({"field":{$lt:value}})
//field 大于等于 value
db.collection.find({"field":{$gte:value}})
//field 小于等于 value
db.collection.find({"field":{$lte:value}})
同时满足多个条件:
//value1 < field < value2
db.collection.find({"field":{$gt:value,$lt:value2}})
2)$all匹配所有,类似于SQL语法in,不同是in只需要匹配括号内的某一个值,而$all必须满足括号内的所有值,如:
db.users.find({age:{$all:[6,8]}})
3)$exists判断字段是否存在,如查询所有存在age字段的纪录:
db.user.find({age:{$exists:true}})
4)null:即包括空值,也保存不存在的字段
5)$mod取模运算,例如查询age取模10等于1的数据:
db.student.find({age:{$mod:[10,1]}}