英文:
Mongo Atlas Search: Find Exact Document Matches
问题
我尝试使用Mongo Atlas Search聚合来查找精确匹配我的查询的文档,查询中包含100 - 200个单词的列表。
开始我的聚合查询
{
'$search': {
'index': 'default',
'text': {
'query': 'yellow pizza blue ',
'path': 'word'
}
}
}, {...
我的集合
{
"word": "yellow card"
},
{
"word": "pizza"
},
{
"word": "blue"
}
我需要它返回“pizza, blue”,而不是返回“yellow card, pizza, blue”。
英文:
Trying to use Mongo Atlas Search aggregation to find documents that precisely match my query which contains a list of 100 - 200 words.
Start of my aggregation query
{
'$search': {
'index': 'default',
'text': {
'query': 'yellow pizza blue ',
'path': 'word'
}
}
}, {...
My collection
{
"word": "yellow card"
},
{
"word": "pizza"
},
{
"word": "blue"
}
I need it to return “pizza, blue", instead it returns “yellow card, pizza, blue”
答案1
得分: 1
你可能只需要使用$indexOfCP
进行简单的子字符串搜索
db.collection.find({
$expr: {
$ne: [
-1,
{
$indexOfCP: [
"yellow pizza blue ",
"$word"
]
}
]
}
})
如果你的搜索列表是以空格分隔的列表,你可以考虑使用$split
将words
字段和你的搜索列表拆分成数组,然后使用$setIsSubset
来查找匹配项。
db.collection.aggregate([
{
"$addFields": {
"searchList": "yellow pizza blue investment "
}
},
{
"$addFields": {
"searchList": {
"$split": [
"$searchList",
" "
]
},
tokens: {
"$split": [
"$word",
" "
]
}
}
},
{
"$match": {
$expr: {
"$setIsSubset": [
"$tokens",
"$searchList"
]
}
}
},
{
$unset: [
"tokens",
"searchList"
]
}
])
英文:
You probably just need a simple substring search using $indexOfCP
db.collection.find({
$expr: {
$ne: [
-1,
{
$indexOfCP: [
"yellow pizza blue ",
"$word"
]
}
]
}
})
If your search list is a space-separated list, you can consider $split
the words
field and your search list into arrays and perform $setIsSubset
to find the matches.
db.collection.aggregate([
{
"$addFields": {
"searchList": "yellow pizza blue investment "
}
},
{
"$addFields": {
"searchList": {
"$split": [
"$searchList",
" "
]
},
tokens: {
"$split": [
"$word",
" "
]
}
}
},
{
"$match": {
$expr: {
"$setIsSubset": [
"$tokens",
"$searchList"
]
}
}
},
{
$unset: [
"tokens",
"searchList"
]
}
])
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论