案例不敏感的搜索电子邮件字段,作为mongoDB中的索引

发布于 2025-02-10 20:36:55 字数 608 浏览 1 评论 0原文

我的用户收集(用户)由电子邮件 field(以及其他一些索引)索引。 试图查找具有带有电子邮件的电子邮件的所有文档,该电子邮件以搜索术语案例不敏感开头(试图实现Serach-suggest逻辑)。

我正在使用以下查询:

db.user.getCollection('user').find({
    "email" :  /^myEmail@gmail/i
})

正如我所理解的,当使用Regex时,我们无法使用索引功能,因此我的查询运行速度非常慢。

是否有一种实现此类的方法以搜索逻辑,而不是使用mongoDB(或其他Atlas功能)提交的索引,该索引利用索引字段超过了大型集合,或者我应该为此使用其他技术吗?

编辑 尝试了文本索引,它没有我预期的。 mongo文本搜索对待myemail@gmail作为2个不同的文本令牌myemail and code> gmail ,因此我得到的结果匹配myemail和结果匹配gmail。这不是我想要的。

My collection of users (user) is indexed by the email field (and some other indexes).
Trying to find all documents that has an email that has an email which starts with a search term case insensitive (trying to implement serach-suggest logic).

I'm using the following query:

db.user.getCollection('user').find({
    "email" :  /^myEmail@gmail/i
})

As I understood, when using regex we are not able to use the index capabilities hence my query runs very slow.

Is there a way to implement such starts with search logic over indexed filed using MongoDB (or additional Atlas capabilities) leveraging the indexed field over large collection or should I use different technology for that matter?

Edit
Tried text indexes and it didn't work as I expected. Mongo text search treats myEmail@gmail as 2 different text tokens myEmail and gmail so I'm getting results matching myEmail and results matching gmail. It is not what I'm looking for.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文