I use MongoDB and Mongoose as my ODM and I'm trying to make a query using populate
and group by
in the same statement.
Here is my simple documents models :
var userSchema = new Schema({
username: String
});
var messageSchema = new Schema({
from: { type: Schema.ObjectId, ref: 'User' },
to: { type: Schema.ObjectId, ref: 'User' },
message: String,
date: { type: Date, default: Date.now }
});
I'm just trying to get every messages for one user, group by each users he talks with. I tried like this :
this.find({ 'to': user })
.sort({ 'date': 1 })
.group('from')
.populate(['from', 'to'])
.exec(callback);
But, unfortunately, my model doesn't have group
method. Do you have any solution, to get this working ?
Thank you.
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…