MongoDB按数组字段中每个单词的第一个字母搜索字符串 [英] MongoDB search string by first letter of each word in an array field
问题描述
我在 Mongo DB 中有一个表,名为商机"(检查下图).
I have a table in Mongo DB called 'Business Opportunities'(Check below image).
目前,我正在创建节点 js 服务,它返回表中 business_opportunities 字段的起始字母的所有匹配值.(在图片中突出显示一个)
Currently, I am creating node js service where it returns all the matching values for starting letter for business_opportunities field in the table. (highlight one in picture)
因此,如果我通过 'A' 它应该只返回数据库中字母 A 的所有匹配项,但由于 'business_opportunities' 是一个数组字段,它返回具有匹配字段的所有值,如下所示,
So, if i pass 'A' it should only return all the matching ones for letter A in the database but since 'business_opportunities'is an array field it return all the values with matching field like below,
{"status":"success","code":200,"data":{"tags":[{"_id":"5aae9de344fdc45f6a5faf08","business_opportunities":["Web Design","app Design ","Coding","Software Engineer"]},{"_id":"5aae9de344fdc45f6a5faf14","business_opportunities":["audit","Accounting"]},{"_id":"5b86180c44fdc4427245ec71","business_opportunities":["Apps, website, development, programming, php, java, javascript, ruby, react, native","aws cloud services, ","ios, android, "]},{"_id":"5bb70d8c44fdc442dd41b702","business_opportunities":["accounting","quickbooks","xero","cloud accounting","financial","audit","consulting"]},{"_id":"5bbac54944fdc40d9f40f071","business_opportunities":["accommodation","rent","condo"]}]}}
这是我当前的代码,
static async findRelatedTags(opts, params) {
assert.object(params, 'params')
assert.string(params.tag, 'params.tag')
assert.number(params.limit, 'params.limit')
assert.object(opts, 'opts')
assert.object(opts.mongo_db, 'opts.mongo_db')
const { mongo_db: db } = opts
const query = [
{
$match: {
business_opportunities: { '$regex': '^'+params.tag+'' }
}
},
{
$project: {
business_opportunities: '$business_opportunities'
}
}
]
const cursor = db.collection('business_opportunities').aggregate([
...query
]).limit(params.limit)
return cursor.toArray()
}
所以我想要这样的输出(如果我搜索A")
{
"status":"success",
"code":200,
"data":{
"tags": [
accounting,
app design,
aws
]
}
}
推荐答案
您需要先展开business_opportunities,然后再将它们与正则表达式匹配.以下是更新后的代码.
You need to unwind the business_opportunities before matching them with regular expression. Below is the updated code.
static async findRelatedTags(opts, params) {
assert.object(params, 'params')
assert.string(params.tag, 'params.tag')
assert.number(params.limit, 'params.limit')
assert.object(opts, 'opts')
assert.object(opts.mongo_db, 'opts.mongo_db')
const { mongo_db: db } = opts
const query = [
{ $unwind : "$comments" },
{
$match: {
business_opportunities: { '$regex': '^'+params.tag+'', $options: 'i' }
}
},
{ "$group": {
"_id": "$comments"
}
}
]
const cursor = db.collection('business_opportunities').aggregate([
...query
]).limit(params.limit)
var agg_results = cursor.toArray()
return agg_results.map(obj => obj._id)
}
MongoDB 聚合将为您提供这样匹配的 business_opportunities
MongoDB aggregation will give you the matched business_opportunities like this
[{
"_id" : "accounting"
},
{
"_id" : "app design"
},
{
"_id" : "aws"
}]
为了将其转换为 ["accounting", "app design", "aws"] 我在上面的示例中添加了 agg_results.map(obj => obj._id)
.
To convert it to ["accounting", "app design", "aws"] I have added agg_results.map(obj => obj._id)
in example above.
除了在您的代码中正则表达式搜索区分大小写,所以我使用了 { '$regex': '^'+params.tag+'', $options: 'i' }
使其不区分大小写,如果不需要,请删除 $options.
Except that in your code regex search is Case Sensitive so I have used { '$regex': '^'+params.tag+'', $options: 'i' }
to make it Case Insensitive, remove $options if you don't need it.
这篇关于MongoDB按数组字段中每个单词的第一个字母搜索字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!