Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/user-interface/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
用于字段检查列表中单词百分比的MongoDB查询_Mongodb_Mongodb Query - Fatal编程技术网

用于字段检查列表中单词百分比的MongoDB查询

用于字段检查列表中单词百分比的MongoDB查询,mongodb,mongodb-query,Mongodb,Mongodb Query,因此,我试图对mongodb文档中的字段进行模糊搜索。如果我有6个我正在搜索的关键词,比如说 ['red'、'crisp'、'sweet'、'crunchy'、'pie'、'durable']我正在搜索一个包含苹果品种描述的字段,我想返回所有包含我正在搜索的6个关键词中的任意4个的苹果。您将如何使用MongoDB实现这一点 这里有两个示例文档仅供参考 { name: "Golden Delicious", description: "Golden Delicious is a l

因此,我试图对mongodb文档中的字段进行模糊搜索。如果我有6个我正在搜索的关键词,比如说

['red'、'crisp'、'sweet'、'crunchy'、'pie'、'durable']我正在搜索一个包含苹果品种描述的字段,我想返回所有包含我正在搜索的6个关键词中的任意4个的苹果。您将如何使用MongoDB实现这一点

这里有两个示例文档仅供参考

{
    name: "Golden Delicious",
    description: "Golden Delicious is a large, yellowish-green skinned cultivar and very sweet to the taste. It is prone to bruising and shriveling, so it needs careful handling and storage. It is a favorite for salads, apple sauce, and apple butter."
}

{
    name: "Jonagold",
    description: "Jonagold is a cultivar of apple which was developed in 1953 in New York State Agricultural Experiment Station of Cornell University's College of Agriculture and Life Sciences, a cross between the crisp Golden Delicious and the blush-crimson Jonathan. They form a large sweet fruit with a thin skin. "
}

因此,在玩了一点这个之后,我能够想出一种方法来使用正则表达式查找特定数量的匹配项。这就是我想到的

db.getCollection('apples').find({
description: {
    $regex: '((red|crisp|sweet|crunchy|pie|durable).*?){4}',
    $options: 'i'
}

})

你能发布示例文档吗?我去添加了几个示例文档,只是为了帮助将问题形象化。检查4到6个单词是否匹配是可能的,但你需要规范你存储
描述的方式。因此,如果您可以将描述存储为诸如``descriptionWords
:[“golden”,“delicious”]…
之类的单词数组(小写),这将是一种更简单的方法。你的情况可能吗?