#db: appdata
#table: company
#表结构:_id,utime,province,branch,src
#branch结构 [{"name":"分公司","code":"hn01","ctime":"2022-01-01 00:00:00"}]
#src结构 {"url":"http://127.0.0.1/index","status":"ok"}
1.切换用户
use appdata;
2.查看collection列表
show collections;
3.查看数据库列表:
show dbs;
4.查询数据 select *
db.appdata.company.find();
# 输入it翻页
5.查询数据 限制返回条数 10
db.appdata.company.find().limit(10);
6.查询数据 跳过数据条数 10
db.appdata.company.find().skip(10);
7.查询数据 排序
db.appdata.company.find().sort({"_id":1}) #升序
db.appdata.company.find().sort({"_id":-1}) #降序
8.查询数据 where province="河北"
db.appdata.company.find({province:"河北"})
db.appdata.company.find({"province":"河北"})
9.查询数据 where utime>'2022-01-01 00:00:00' and utime<='2023-01-01 00:00:00'
db.appdata.company.find({utime:{"$gt":"2022-01-01 00:00:00","$lte":"2023-01-01 00:00:00"})
10.查询数据 where province in ("河北","河南")
db.appdata.company.find({province:{$in:["河北","河南"]}})
11.查询数据 where province in ("河北","河南") and utime>'2022-01-01 00:00:00'
db.appdata.company.find({province:{$in:["河北","河南"]},utime:{"$gt":"2022-01-01 00:00:00"}})
12.查询数据 where province in ("河北","河南") or utime>'2022-01-01 00:00:00'
db.appdata.company.find({"$or":[province:{$in:["河北","河南"]},utime:{"$gt":"2022-01-01 00:00:00"}]})
13.查询嵌套数据 src.status:"ok"
db.appdata.company.find({"src.status":"ok"})
14.查询嵌套数据 branch.ctime>'2022-01-01 00:00:00'
db.appdata.company.find({"branch.ctime":{"$gt":"2022-01-01 00:00:00"}})
15.查询嵌套数据 branch第一个元素包含name且是cdsn的
db.appdata.company.find({"branch.0.name":"cdsn"})
16.查询嵌套数据 branch至少有一个元素name是cdsn的
db.appdata.company.find({"branch":{"$elemMatch":{"name":"cdsn"}}})
17.查询嵌套数据 branch第一个元素包含name字段
db.appdata.company.find({"branch.0.name":{"$exists":true}})
08-04
723
07-06
2116
01-20
8561