How to count records with one distinct field in mongoose?

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

    I just needed the number of distinct musicians, but some of the code above did not work for me. If I used count and distinct together I just got the total number.

    This was my solution:

    /**
     * Get number of distinct musicians
     */
    
    myList.find()
        .distinct('musicianName')
        .exec(function (err, count) {
            console.log(count.length);
        });
    

提交回复
热议问题