How to count records with one distinct field in mongoose?

后端 未结 5 1817
春和景丽
春和景丽 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:12

    If you just want get the number of queried collections, you can use this:

    Record.find()
          .distinct('user_id')
          .count(function (err, count) {
              //The number of unique users is 'count'
          });
    

提交回复
热议问题