云开发-聚合查询

//普通数据查询
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))

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值