Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/mongodb/12.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Mongodb 引用另一个集合可能会导致大量查询_Mongodb - Fatal编程技术网

Mongodb 引用另一个集合可能会导致大量查询

Mongodb 引用另一个集合可能会导致大量查询,mongodb,Mongodb,我有两个收藏: 注释 { annotations: ["lion", "zebra", "cobra"], imagesId: 1234 } { annotations: ["lion", "zebra", "cobra", "dog"], imagesId: 12345 } 图像 {_id:1234, name: "africa" } {_id:12345, name: "america" } 我想找到所有带有注释的图像名称,例如zebra 这是我的伪方法:

我有两个收藏:

注释

{
  annotations: ["lion", "zebra", "cobra"],
  imagesId: 1234
}

{
  annotations: ["lion", "zebra", "cobra", "dog"],
  imagesId: 12345
}  
图像

{_id:1234,
 name: "africa"
}
{_id:12345,
 name: "america"
}  
我想找到所有带有注释的图像名称,例如
zebra

这是我的伪方法:

let annotations = Annotations.find({annotations: {'$in': 'zebra'}});  

getOnlyUniqueItems(annotations);  

Images.find({_id: {'$in': annotations}});  
它会工作的,问题是当
注释
包含例如1M
ID
时,查询本身将超过1MB的文本,它可能会杀死我的数据库

问题:解决这个问题的最佳方法是什么?

谢谢!:-)

如果我们不考虑将
注释
保存为由
分隔的自由文本,
是一个坏主意,对于您可能尝试的特定用例,性能/响应时间不是问题,例如:

Annotations.aggregate([
  { $match: { annotations: { '$in': 'zebra' } } },
  {
      $lookup:
         {
            from: "Images",
            localField: "imagesId",
            foreignField: "_id",
            as: "image"
        }
   }
]);

annotations
不是一个自由文本,它是{annotations,imagesid}对象的集合。是的,但是
annotations
内的属性
annotations
集合是一个自由文本。我的错误是,它应该是一个字符串数组:
annotations:['zebra',lion',cobra]
它会改变你的答案吗?不是真的,无论如何,您必须使用
$lookup