【问题标题】:Query Optimization in MongoDBMongoDB中的查询优化
【发布时间】: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


    【解决方案1】:

    content 字段上创建文本索引并使用文本搜索。

    db.text.insertMany([
      { "_id" : 1, "content" : "Paris is capital of France" },
      { "_id" : 2, "content" : "Have you ever went to London?" },
      { "_id" : 3, "content" : "Where is Tokyo?" },
      { "_id" : 4, "content" : "I heard about Singapore as well" }
    ])
    db.text.createIndex({content:"text"})
    db.text.find({$text:{$search:"London"}})
    

    这会比正则表达式快得多,因为它会使用索引。

    参考资料:

    【讨论】:

    • 没关系。我的问题是我在另一个数组中有搜索字符串。所以我不想循环并运行查询 50 次。就像在您的解决方案中一样,我不仅想使用“伦敦”进行搜索,实际上我还想使用集合中存在的所有字符串进行搜索。
    • 您最大的问题是您在没有索引的情况下在数百万条记录中循环 50 次。那么试试这个:db.text.find({$text:{$search:"paris london tokyo"}}) - 看起来这就是你需要的。
    猜你喜欢
    • 1970-01-01
    • 2015-01-21
    • 2015-01-09
    • 2015-03-06
    • 1970-01-01
    • 2021-01-23
    • 1970-01-01
    • 1970-01-01
    • 2022-11-13
    相关资源
    最近更新 更多