【发布时间】:2019-10-25 00:25:54
【问题描述】:
我有两个系列。在一个集合中,我存储了搜索字符串,而在其他实际数据中存在。我需要根据搜索集合中存储的搜索字符串从数据集合中查找记录。
(1) 搜索集合:
[{_id:123, search: paris},
{_id:123, search: london},
{_id:123, search: tokyo}]
(2) 数据收集:
[{_id:123, content: Paris is capital of France},
{_id:123, content: Have you ever went to London?},
{_id:123, content: Where is Tokyo?},
{_id:123, content: I heard about Singapore as well}]
问题是我有超过 50 个搜索字符串,并且在数据收集中我有数百万条记录。我目前的实现如下:
const src = await SearchCollection.find();
for (let i = 0; i (is less than) src.length; i++) {
let msg = await DataCollection.find({
content: {
$regex: new RegExp(src[i].search)),
$options: "gi"
}
});
显然这个查询很慢。如果有 50 个搜索字符串,它将循环 50 次,并且数据内容以百万计。如何处理我目前的情况?
【问题讨论】:
标签: javascript node.js mongodb performance mongoose