Within my application, I have two models called Alarm
and Alert
.
The AlertSchema
includes a field named created_by
that references the object ID of an Alarm
.
If I have an array of Alarm
objects, how can I retrieve all the corresponding alerts?
I attempted the following, but it did not yield the desired results:
// Retrieve array of alarm objects
Alarm.find({unit: req.unit._id}).exec(function(err, alarms){
// Use array of alarm objects to find all alerts
Alert.find({created_by: alarms})
Would it be better to extract the _id from each object and pass them as an argument like shown below:
Alarm.find({unit: req.unit._id}).exec(function(err, alarms){
var alarm_ids = alarms.map(function(o){return o._id});
// Use array of alarm objects to find all alerts
Alert.find({created_by: alarm_ids})