Renan Basso
Renan Basso

Reputation: 805

Mongoose grouping error

I got a trouble to filter and group on mongodb. I really didn't undertanded how it works.

For example, in this query:

Room.aggregate(
[{
    "$where": { "roomId" : myIdHere },
    "$group": {
        "_id": '$mobileUser.genderType',
        "genderTypeCount": {
            "$sum": 1
        }
    }
}]

Room model:

var roomModelSchema = mongoose.Schema({
    roomId: String,
    mobileUser: {
        facebookId: String,
        email: String,
        name: String,
        photoUrl: String,
        genderType: String,
        birthday: Date,
        city: String    
    },
    insertDate: Date
})

What i should do to filter by roomId and group by genderType?

Upvotes: 0

Views: 28

Answers (1)

JohnnyHK
JohnnyHK

Reputation: 311835

Use $match instead of $where, and put each pipeline operation into its own object:

Room.aggregate([
    { "$match": { "roomId" : myIdHere } },
    { "$group": {
        "_id": '$mobileUser.genderType',
        "genderTypeCount": {
            "$sum": 1
        }
    }}
], function(err, result) {...})

Upvotes: 1

Related Questions