用于字段检查列表中单词百分比的MongoDB查询
因此,我试图对mongodb文档中的字段进行模糊搜索。如果我有6个我正在搜索的关键词,比如说 ['red'、'crisp'、'sweet'、'crunchy'、'pie'、'durable']我正在搜索一个包含苹果品种描述的字段,我想返回所有包含我正在搜索的6个关键词中的任意4个的苹果。您将如何使用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
{
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”]…
之类的单词数组(小写),这将是一种更简单的方法。你的情况可能吗?