代码之家  ›  专栏  ›  技术社区  ›  runxc1 Bret Ferrier

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

  •  -1
  • runxc1 Bret Ferrier  · 技术社区  · 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. "
    }
    
    1 回复  |  直到 6 年前
        1
  •  0
  •   runxc1 Bret Ferrier    6 年前

    所以在玩了一点之后,我想出了一种方法,用正则表达式来寻找特定数量的匹配项。这就是我想到的

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

    })