如何在mongodb中的数组中查找文本搜索

如何在mongodb中的数组中查找文本搜索,mongodb,mongodb-query,Mongodb,Mongodb Query,以上是我的mongo模式模式,用于我的项目需求。当我只想搜索蔬菜时,蔬菜名称必须出现。如果我同时搜索蔬菜和水果,那么番茄和苹果这两个名字都应该出现 我像这样使用了“$in”操作符 { "_id":objectId(23651478), "name":"Tomatos" "array":[ {"title":"Vegetables"} ] "description":"Vegitables are good to

以上是我的mongo模式模式,用于我的项目需求。当我只想搜索蔬菜时,蔬菜名称必须出现。如果我同时搜索蔬菜和水果,那么番茄和苹果这两个名字都应该出现

我像这样使用了
“$in”
操作符

 {
    "_id":objectId(23651478),
    "name":"Tomatos"
    "array":[
             {"title":"Vegetables"}
            ]
    "description":"Vegitables are good to health"
 },
 {
    "_id":objectId(45761244),
    "name":"Apples"
    "array":[
             {"title":"Fruits"}
            ]
    "description":"Fruits are good to health, vegitables are also good to health"
 },
 {
    "_id":objectId(45761244),
    "name":"Apples"
    "array":[
             {"title":"Vegetables-home-made"}
            ]
    "description":"Fruits are good to health, vegitables are also good to health"
 }
它只提供标题为
蔬菜
的文档,而不提供
自制蔬菜
。为此,我尝试使用
$elemMatch

{"array":{$in:[{"title":"Vegetables"},{"title":"Fruits"}]}}
但当我们在elemMatch中搜索倍数,即,
蔬菜
水果
时,它给出了一个错误,我只希望在数组中搜索特定的标题键,即

 {"array":{$elemMatch:[{"title":"Vegetables"},{"title":"Fruits"}]}}

MongoDB中的常规文本搜索如下所示:

{"array":{$text:{:$search:[{"title":"Vegetables"},{"title":"Fruits"}]}}}
请检查您正在运行的搜索文本。单词中包含拼写错误:

db.collectionName.find( { $text: { $search: "Vegetables" } } )
以下是您想要的:


您可以在$in运算符中使用正则表达式,如下所示:

{"array":{$text:{:$search:[{"title":"Vegitables"},{"title":"Fruits"}]}}}

您可以使用正则表达式$regex或$text。试试这个:{“array”:{$in:[{“title”:{$regex:{/players/}}]}
{array: { $elemMatch: { title: { $in: [/Vegetables/, /Fruits/] }}}}