I'm currently using the following code to sift through two separate collections. One collection holds a total list of messages sent to users. The second collection holds all responses that have come from users.
From the first collection, I'm building an array of messages sent -- then querying the second collection to search for all responses that don't have a match within that array.
Two questions:
1) Is there a better way of outputting Mongoose/MongoDB results into an array than the for loop approach I am using?
2) Is there a better way to compare these collections than I'm using?
// 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})
});
});