jeudi 2 avril 2015

Mongodb / Mongoose : Equivalent to GROUP BY + ORDER BY

I have a mongodb database and I'm using Mongoose above it.


I have this model for a Message :



var MessageSchema = new mongoose.Schema({
room: {
type: ObjectId,
ref: 'Room'
},
text: {
type: String
},
date: {
type: Date,
default: Date.now,
}});


I would like to get the latest message for each room, ordered by date. In SQL, it would give something like :



SELECT * FROM messages GROUP BY room ORDER BY date ASC


How can I achieve this? Thanks


Aucun commentaire:

Enregistrer un commentaire