[{"createTime":1735734952000,"id":1,"img":"hwy_ms_500_252.jpeg","link":"https://activity.huaweicloud.com/cps.html?fromacct=261f35b6-af54-4511-a2ca-910fa15905d1&utm_source=V1g3MDY4NTY=&utm_medium=cps&utm_campaign=201905","name":"华为云秒杀","status":9,"txt":"华为云38元秒杀","type":1,"updateTime":1735747411000,"userId":3},{"createTime":1736173885000,"id":2,"img":"txy_480_300.png","link":"https://cloud.tencent.com/act/cps/redirect?redirect=1077&cps_key=edb15096bfff75effaaa8c8bb66138bd&from=console","name":"腾讯云秒杀","status":9,"txt":"腾讯云限量秒杀","type":1,"updateTime":1736173885000,"userId":3},{"createTime":1736177492000,"id":3,"img":"aly_251_140.png","link":"https://www.aliyun.com/minisite/goods?userCode=pwp8kmv3","memo":"","name":"阿里云","status":9,"txt":"阿里云2折起","type":1,"updateTime":1736177492000,"userId":3},{"createTime":1735660800000,"id":4,"img":"vultr_560_300.png","link":"https://www.vultr.com/?ref=9603742-8H","name":"Vultr","status":9,"txt":"Vultr送$100","type":1,"updateTime":1735660800000,"userId":3},{"createTime":1735660800000,"id":5,"img":"jdy_663_320.jpg","link":"https://3.cn/2ay1-e5t","name":"京东云","status":9,"txt":"京东云特惠专区","type":1,"updateTime":1735660800000,"userId":3},{"createTime":1735660800000,"id":6,"img":"new_ads.png","link":"https://www.iodraw.com/ads","name":"发布广告","status":9,"txt":"发布广告","type":1,"updateTime":1735660800000,"userId":3},{"createTime":1735660800000,"id":7,"img":"yun_910_50.png","link":"https://activity.huaweicloud.com/discount_area_v5/index.html?fromacct=261f35b6-af54-4511-a2ca-910fa15905d1&utm_source=aXhpYW95YW5nOA===&utm_medium=cps&utm_campaign=201905","name":"底部","status":9,"txt":"高性能云服务器2折起","type":2,"updateTime":1735660800000,"userId":3}]
//普通数据查询 db.collection("china") //获取集合china的引用 .where({ //查询的条件操作符where gdp: _.
gt(3000) //查询筛选条件,gt表示字段需大于指定值。 }) .field({ //显示哪些字段 _id:false, //默认显示_id,这个隐藏
city: true, province: true, gdp:true }) .orderBy('gdp', 'desc') //排序方式,降序排列 .
skip(0) //跳过多少个记录(常用于分页),0表示这里不跳过 .limit(10) //限制显示多少条记录,这里为10,默认是20 .get()
//获取根据查询条件筛选后的集合数据 .then(res => { console.log(res.data) }) .catch(err => {
console.error(err) }) //聚合查询,大家写聚合操作的时候,可以参考这样的一个模板来写 db.collection('china').
aggregate() //发起聚合操作 .match({ //类似于where,对记录进行筛选 gdp: _.gt(3000) }) .project({
//类似于field,在这里可以新增字段 _id:false, //默认显示_id,这个隐藏 city: true, province: true, gdp:
true }) .sort({ //类似于orderBy gdp: -1, }) .skip(5) //类似于skip .limit(1000)
//类似于limit,不填默认是20,没有上限 .end() //注意,end标志聚合操作的完成 .then(res => console.log(res))
.catch(err => console.error(err))
这里的match、project、sort、skip、limit、end都是聚合阶段在写聚合查询时有几个需要注意的点:
aggregate()是发起一个聚合操作
match是根据条件过滤文档,进行的是查询匹配,语法和where比较类似;
project把指定的字段传递给下一个流水线,指定的字段可以是某个已经存在的字段,也可以是计算出来的新字段,它和field不同的是可以新增一些不存在的字段(只是显示用,没写进数据库);
sort根据指定的字段,对输入的文档进行排序,1 代表升序排列(从小到大);-1 代表降序排列(从大到小),功能和orderBy类似;
小程序端limit默认
20,也就是如果你使用聚合查询,你查询到的数据都会默认显示20条数据,但是你可以设置更多(但是不要太大,基本没有上限,除了整个数据不能超过16M外),而普通查询是不能超过20条的;
end()标志着聚合操作的完成
注意聚合返回的结果是list数组对象,res.list才是数组,这和普通数据查询有所不同
云数据库命令与SQL语句对应理解
云开发数据库是非关系文档型数据库,和MySQL这种关系型数据库在使用上有不少的差异和联系,下面是云数据库与SQL语句之间的对应立即理解: