本篇笔记记录:mongodb类比关系型数据库语句写法;
1,查询:
查询集合中所有:
mongo | mysql |
---|
db.getCollection(“deviceLog_20190118”).find({}) | select * from user |
1.1 查询:
查询方式 | mongo | mysql |
---|
"eq” | db.getCollection(“deviceLog_20190118”).find({“sn”:“585d48c52f6686b6”}) | select * from user where user_name = ‘ming’ |
“like” | db.getCollection(“deviceLog_20190118”).find({“sn”:{"$regex":/585d48/}}) | select * from user where user_name like ‘%ming%’ |
| db.getCollection(“deviceLog_20190118”).find({“sn”:{"$regex":/^585d48/}}) | select * from user where user_name like ‘ming%’ |
“distinct” | db.getCollection(“deviceLog_20190118”).distinct(“sn”) | select distinct(user_name) from user |
and | db.getCollection(“deviceLog_20190118”).find({“sn”:“585d48c52f6686b6”,“severity”:“1”}) | select * from user where user_name = ‘ming’ and user_id = ‘1’ |
| db.getCollection(“deviceLog_20190118”).find({“sn”:“585d48c52f6686b6”,“severity”:“1”},{“sn”:1,“severity”:1,“dspMsg”:1}) | select user_name,user_id,user_age from user where user_name = ‘ming’ and user_id = ‘1’ |
“or” | db.getCollection(“deviceLog_20190118”).find({ “$or”: [{“sn”:“585d48c52f6686b6”},{“severity”:“1”}] },{“sn”:1,“severity”:1,“dspMsg”:1}) | select user_name,user_id,user_age from user where user_name = ‘ming’ or user_id = ‘1’ |
<, <=, >, >= ($lt, $lte, $gt, $gte ) | db.getCollection(“deviceLog_20190118”).find({ “score”:{“
g
t
e
"
:
20
,
"
gte":20,"
gte":20,"lte”:30} }) | select * from user where user_age >=20 and user_age<=30 |
in, not in ($in, $nin) | db.getCollection(“deviceLog_20190118”).find({ “score”:{"$in": [20,30,40]} }) | select * from user where user_age in (20,30,40); |
“count” | db.getCollection(“deviceLog_20190118”).count() | select count(1) from user |
不等于 $ne | db.getCollection(“deviceLog_20190118”).find({ “score”:{"$ne": 20} }) | select * from user where user_age != 20 |