MongoDB查询以对列表中单词的百分比进行字段检查

时间:2018-09-24 20:07:01

标签: mongodb mongodb-query

因此,我试图对mongodb文档中的字段进行一些模糊搜索。例如,如果我说6个要搜索的关键字,

['红色','酥脆','甜','松脆','派','耐用'],我正在搜索一个描述苹果品种的字段,我想返回所有有任何苹果的苹果我要搜索的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. "
}

1 个答案:

答案 0 :(得分:0)

因此,在玩了一点之后,我想出了一种使用Regex查找特定数量的比赛的方法。这就是我想出的

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

})