云开发联表查询怎么把查询到数据计数?

发布于 6 年前作者 ngao12976 次浏览最后编辑 6 年前来自 ask
    const _ = db.command
    const $ = db.command.aggregate
    var result = await db.collection('animal').aggregate()
      .lookup({
        from:'order',
        let:{
          animal:'$_id'
        },
        pipeline:$.pipeline().match(_.expr($.and([
          $.eq(['$goods_id','$$animal']),
          $.gte(['$state',1]),
          $.lte(['$state',3])
        ]))).project({
          _id:0
        }).done(),
        as :'percent'
      }).end()
    return result


怎么把percent计数 count()在这里怎么用

1 回复
xiuying76
xiuying761 楼4 年前
  const _ = db.command
    const $ = db.command.aggregate
    var result = await db.collection('animal').aggregate()
      .lookup({
        from:'order',
        let:{
          animal:'$_id'
        },
        pipeline:$.pipeline().match(_.expr($.and([
          $.eq(['$goods_id','$$animal']),
          $.gte(['$state',1]),
          $.lte(['$state',3])
        ]))).project({
          _id:0
        }).done(),
        as :'percent'
      })
.addField({
  size: $.size('$percent')
})
.end()
    return result