代码之家  ›  专栏  ›  技术社区  ›  rap-2-h

Mongoose何处计数

  •  1
  • rap-2-h  · 技术社区  · 6 年前

    假设我有这个Mongoose查询:

    const actors = await Actor.aggregate([{ 
        "$group": {_id: "$loc.coordinates", count:{$sum:1}}  
    }])
    

    有没有办法只得到 actors 有一个 count > 1 ?

    1 回复  |  直到 6 年前
        1
  •  2
  •   Ashh    6 年前

    使用 $match 之后的阶段 $group

    const actors = await Actor.aggregate([
       { "$group": { "_id": "$loc.coordinates", "count": { "$sum": 1 }} },
       { "$match": { "count": { "$gt": 1 }} }  
    ])