How to count records with one distinct field in mongoose?

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

    You can do a distinct query.

    var Record = db.model('Record', yourSchema);
    Record.find().distinct('user').exec(callback);
    

    Mongoose Queries: http://mongoosejs.com/docs/queries.html

    MongoDB distinct query: http://www.mongodb.org/display/DOCS/Aggregation#Aggregation-Distinct

提交回复
热议问题