0
我目前使用下面的代码来筛选两个单独的集合。一个集合包含发送给用户的消息的总列表。第二个集合包含来自用户的所有回复。Mongoose Aggregate Field结果数组
从第一个集合开始,我构建了一个发送的消息数组 - 然后查询第二个集合以搜索在该数组中没有匹配的所有响应。
两个问题:
1)是否有输出猫鼬/ MongoDB的更好的办法产生到一个数组比我现在用的for循环的方法呢?
2)有没有更好的方法来比较这些集合比我使用?
// Find all responses (read) messages associated with a patient.
Response.find({patientID: patientID}, function(err, responses){
// Store all msgIDs into an array.
for (var i = 0; i < responses.length; i++){
openedArray.push(responses[i].msgID);
}
// Find all messages associated with that same patient, but that were not included in the response list
Message.find({patientList: patientID, msgID: { $ne: openedArray}}, function(err, unopenedMessages){
// Store all msgIDs into an array.
for (var j = 0; j < unopenedMessages.length; j++){
unopenedArray.push(unopenedMessages[j].msgID);
}
// Output a list of unopened messages (msgIDs) and a count of total unread messages
res.json({unread: unopenedArray, count: unopenedArray.length})
});
});
感谢这个async.waterfall是一个很好的技巧以及独特的技巧。欣赏它! –