How to count records with one distinct field in mongoose?

后端 未结 5 1816
春和景丽
春和景丽 2021-01-12 10:30

While exploring mongoose for nodejs I ran into the problem of needing to know the amount of user in my collection:

My collection has records, each record has a user

5条回答
  •  青春惊慌失措
    2021-01-12 11:24

    Aggregation will work for you. Something like that:

    Transaction.aggregate(
        { $match: { seller: user, status: 'completed'  } }, 
        { $group: { _id: '$customer', count: {$sum: 1} } }
    ).exec() 
    

提交回复
热议问题