我希望在访问其他值模型的引用时赶上消息ID和用户对象
comments: {
user: { // <---reference users PROBLEM
type: mongoose.Schema.Types.ObjectId,
ref: 'users'
},
message: {// <---reference messages PROBLEM
type: mongoose.Schema.Types.ObjectId,
ref: 'messages'
},
text: {type: String, required: true},
created_at: { type: Date, "default": Date.now}
}
db.getCollection('comments').aggregate([
{$match: {'message': ObjectId("564854b1b7eb16fb511d90c0")}},
{
$group: {
_id: '$user',
count: {$sum: 1},
author: {$last: '$user'},
messages_test: {$last: '$text'},
msg_id: {$last: '$message._id'}
},
},
{
$project: {
count : 1,
messages_test : 1,
msg_id : 1,
username: { $concat: [ "$author.name", " ", "$author.surname" ] }
}
},
]);`
{
"result" : [
{
"_id" : ObjectId("563f2db0e2bf6c431b297d45"),
"count" : 3.0000000000000000,
"messages_test" : "x",
"msg_id" : null, //<-------EMPTY
"username" : null//<-------EMPTY
},
{
"_id" : ObjectId("563f7c0a8db7963420cd5732"),
"count" : 3.0000000000000000,
"messages_test" : "333",
"msg_id" : null,//<-------EMPTY
"username" : null//<-------EMPTY
}
],
}
{
"result" : [
{
"_id" : ObjectId("563f2db0e2bf6c431b297d45"),
"count" : 3,
"messages_test" : "x",
"msg_id" : "563f2db0e2bf6c431b2asd5", //<-------NOT EMPTY
"username" : "John Travolta"//<-------NOT EMPTY
},
{
"_id" : ObjectId("563f7c0a8db7963420cd5732"),
"count" : 4,
"messages_test" : "333",
"msg_id" : "563f2db0e2bf6c431b2jgk6",//<-----NOT EMPTY
"username" : "Bill Gates"//<-------NOT EMPTY
}
],
}
message.aggregate([
{$match: {_id: mongoose.Types.ObjectId(id)}},
{$unwind: "$comments"},
{
$group: {
_id: '$comments.user_id',
count: {$sum: 1},
author: {$last: '$comments.username'},
messages_test: {$last: '$comments.text'},
msg_id: {$last: '$_id'}
}
}
],
function (err, msg) { ...
并且一切都恢复了,但在这种结构中,我无法应对这样的任务 click
我想如何做一些普遍的请求,但我不知道如何
请帮助我