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}});
它会工作的,问题是当注释
包含例如1MID
时,查询本身将超过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