gpt4 book ai didi

mongodb - 使用 mongoose 正则表达式与文本在 mongodb 中搜索

转载 作者:IT老高 更新时间:2023-10-28 13:35:57 25 4
gpt4 key购买 nike

谁能解释一下两者的区别:

db.collection.find({ $text: { $search: "dog cat" } })

Product.find({ "drug": { "$regex": "cols", "$options": "i" } })

我们什么时候应该去哪一个?

最佳答案

嗯,正则表达式和文本搜索 ($text) 都可以帮助您非常有效地搜索文本。两者各有优缺点,但有两个明显的区别

regex

  • 正则表达式不利用索引,除非您使用 ^ 运算符在字符串开头进行搜索。

  • Regex 允许您搜索部分文本。因此 .* 和许多其他模式。

  • 正则表达式不支持停用词或干扰词。

$text

mongodb 中的文本索引非常快,应该是首选。但是,MongoDB 并没有实现全功能的文本索引。一个主要缺点是,它不支持部分匹配。例如如果您正在搜索猫,它将只搜索猫和猫,而不是山猫或毛毛虫。

Bottom line is if you are looking to implement feature like RDBMS like operator, '$text' will not help you (at least in current implementations of MongoDB, but in future it may change).

关于mongodb - 使用 mongoose 正则表达式与文本在 mongodb 中搜索,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35812680/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com