gpt4 book ai didi

node.js - 使用 Node js 在 mongodb 中的多个字符串字段中进行高效的文本搜索

转载 作者:太空宇宙 更新时间:2023-11-03 22:51:17 28 4
gpt4 key购买 nike

mongo 中存储了大约 10000 条这样的记录。如何在 Node.js 中高效搜索以下内容。搜索条件可以基于 mongo 中以下 3 个字段中的任意一个。搜索条件将来自前端,他们可以搜索用户 ID 或员工姓名或部门。对于搜索,仅采用一个输入,即只有一个字段可以与这些字段中的任何一个相匹配,例如谷歌搜索。

{
"userID": "01000900",
"employeeName": "A Abc",
"department": "Replenishment Boston"
},
{
"userID": "01001024",
"employeeName": "K Gbc",
"department": "Sales S-II - MA Core Urb"
},
{
"userID": "01001023",
"employeeName": "Ga Va",
"department": "Sales Phoenix"
},
{
"userID": "01000282",
"employeeName": "D Din",
"department": "Sales S-II - California - Me"
}

最佳答案

假设searchText是您从客户端获得的值。使用正则表达式。

{
$or:[
{userId:{$regex: searchText, $options: 'i'}},
{employeeName:{$regex: searchText, $options: 'i'}},
{department:{$regex: searchText, $options: 'i'}}
]
}

关于node.js - 使用 Node js 在 mongodb 中的多个字符串字段中进行高效的文本搜索,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42973943/

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