Node.js Mongoose合计(单位:美元)
阅读后,我如何将其应用到我的案例中Node.js Mongoose合计(单位:美元),node.js,mongoose,nosql,Node.js,Mongoose,Nosql,阅读后,我如何将其应用到我的案例中 [UserSchema] name : String [ConversationSchema] participants : [User] [MessageSchema] sender : User conversation : Conversation content : String 我试图通过一个查询获得对话列表及其消息,仅当用户是对话的一部分时。参与者 感谢您的帮助。使用async.failter: async.waterfall([ f
[UserSchema]
name : String
[ConversationSchema]
participants : [User]
[MessageSchema]
sender : User
conversation : Conversation
content : String
我试图通过一个查询获得对话列表及其消息,仅当用户是对话的一部分时。参与者
感谢您的帮助。使用
async.failter
:
async.waterfall([
function (cb) {
Conversation.aggregate([
{
$lookup: {
from: Message.collection.name, // collection name in db, mongo uses plural in $lookup (nice trick preventing from "hard-coding" it)
localField: '_id',
foreignField: 'conversation',
as: 'messages'
}
},
{
$project: {
'messages.conversation': 0 // to hide the conversation id in Conversation.messages array
}
}
], function (err, conversations) {
cb(null, results);
});
},
function (results, cb) {
Conversation.populate(conversations, [{
path: 'participants',
model: 'User',
select: 'name'
}, {
path: 'messages.sender',
model: 'User',
select: 'name'
}], function (err, results) {
cb(null, results)
}
)
}], function (err, results) {
console.log(results); // finally :)
});