我有2个模型,Alarm
和Alert
。
AlertSchema
有created_by
字段,该字段引用Alarm
对象ID。
给定一组Alarm
个对象,我怎样才能找到所有相应的警报?
这不起作用(我真的没想到)
// Get 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})
我最好的办法是将每个对象的_id提取到一个数组中,然后将其作为参数传递给它:
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})
答案 0 :(得分:1)
您实际上可以使用 $lookup
运算符来使用聚合框架和查询,从而可以对其他集合执行左外连接。
检查以下示例:
Alarm.aggregate([
{ "$match": { "unit": req.unit._id } },
{
"$lookup": {
"from": "alerts",
"localField": "_id",
"foreignField": "created_by",
"as": "alarm_alert"
}
},
{ "$unwind": "$alarm_alert "}
]).exec(function(err, result){
console.log(JSON.stringify(result, null, 4));
});
以上是有效的,因为它是一个原子查询并使用本机运算符。
继续你的解决方案尝试,你错过了 $in
操作符作为你的拼图游戏的最终选择,但有一件事将苍蝇放入庆祝软膏中是因为你正在重复调用服务器,即一个用于检索警报对象,另一个用于查询警报集合,因此它可能不如上面的聚合操作有效:
Alarm.find({ "unit": req.unit._id })
.lean() // <-- return plain javascript objects
.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": { "$in": alarm_ids } }).exec(callback);
});