【问题标题】:Finding items is similar in mongodb在 mongodb 中查找项目是相似的
【发布时间】:2020-01-25 13:36:33
【问题描述】:

我对 Mongo 非常陌生(以前也没有关于 Db 结构的知识)和 Python 环境,我的要求是创建一个配方 Db。添加数据后,我可以在其中获取带有成分的食谱。

在monog db成分中查找项目是相似的,

例子

食谱 - 1. 咖喱鸡 - (鸡肉、洋葱、西红柿、油),准备文本 2.鸡肉卷-(鸡肉,洋葱,小麦粉,油),准备文本 3. Paneer Roll -(芝士、洋葱、小麦粉、油),准备文本 4. Paneer Sandwich - (Paneer, onion, bread, oil), 准备文本

所以在我的 API 调用中应该有什么查询来获取配方成分。 例子, 1.查询-Paneer:结果食谱:Paneer Roll,Paneer Sandwich 2.查询-Paneer, onion, bread: Paneer Sandwich 3.查询-鸡肉,西红柿:咖喱鸡

它应该返回相关的食谱。请帮助解决这个问题。如果有其他适合此要求的数据库,请告诉我。

{
    "category": "Indian",
    "recipe_url": "https://www.allrecipes.com/recipe/222667/dal-makhani-indian-lentils/",
    "recipe_name": "dal makhani (indian lentils)",
    "recipe_short_description": "\"ever go to an indian restaurant and wonder how they make those lentils? i hated lentils before i discovered indian food then i scoured the internet to figure out how they achieved them and through mixing and matching recipes and methods on videos ive arrived at this recipe which i think is pretty close this version is very rich but you can leave out the cream to make it lighter kasuri methi (fenugreek leaves) is almost impossible to find in the us even in nyc but it gives this dish something very special\"",
    "image": [
        {
            "url": " \"https://images.media-allrecipes.com/userphotos/560x315/1257894.jpg\""
        }
    ],
    "recipe_calories": "390 cals",
    "recipe_preptime": "4 h 15 m",
    "recipe_servings": "\"6\"",
    "recipe_ingredient": [
        {
            "ingredient_desc": "1 cup lentils",
            "amount": "1 ",
            "ingr": "cup lentils",
            "content": "lentils"
        },
        {
            "ingredient_desc": "1/4 cup dry kidney beans (optional)",
            "amount": "1/4 ",
            "ingr": "cup kidney beans",
            "content": "beans"
        },
        {
            "ingredient_desc": "water to cover",
            "amount": "",
            "ingr": "water",
            "content": "water cover"
        },
        {
            "ingredient_desc": "5 cups water",
            "amount": "5 ",
            "ingr": "cups water",
            "content": "water"
        },
        {
            "ingredient_desc": "salt to taste",
            "amount": "",
            "ingr": "salt taste",
            "content": "taste"
        },
        {
            "ingredient_desc": "2 tablespoons vegetable oil",
            "amount": "2 ",
            "ingr": "vegetable oil",
            "content": "oil"
        },
        {
            "ingredient_desc": "1 1/2 tablespoons ginger paste",
            "amount": "1/2 ",
            "ingr": "ginger paste",
            "content": "paste"
        },
        {
            "ingredient_desc": "1 1/2 tablespoons garlic paste",
            "amount": "1/2 ",
            "ingr": "paste",
            "content": "paste"
        },
        {
            "ingredient_desc": "1/2 teaspoon ground turmeric",
            "amount": "1/2 teaspoon",
            "ingr": "ground",
            "content": "turmeric"
        },
        {
            "ingredient_desc": "1 pinch cayenne pepper or more to taste",
            "amount": "1 ",
            "ingr": "pinch cayenne pepper",
            "content": "pepper taste"
        },
        {
            "ingredient_desc": "1 cup canned tomato puree or more to taste",
            "amount": "1 ",
            "ingr": "cup tomato puree",
            "content": "canned puree taste"
        },
        {
            "ingredient_desc": "1 tablespoon chili powder",
            "amount": "1 ",
            "ingr": "chili powder",
            "content": "powder"
        },
        {
            "ingredient_desc": "add all ingredients to list",
            "amount": "",
            "ingr": "",
            "content": ""
        }
    ],
    "recipe_description": "\"ever go to an indian restaurant and wonder how they make those lentils? i hated lentils before i discovered indian food then i scoured the internet to figure out how they achieved them and through mixing and matching recipes and methods on videos ive arrived at this recipe which i think is pretty close this version is very rich but you can leave out the cream to make it lighter kasuri methi (fenugreek leaves) is almost impossible to find in the us even in nyc but it gives this dish something very special\"",
    "recipe_procedure": [
        {
            "step": "0",
            "desc": "place lentils and kidney beans in a large bowl; cover with plenty of water soak for at least 2 hours or overnight drain"
        },
        {
            "step": "1",
            "desc": "cook lentils kidney beans 5 cups water and salt in a pot over medium heat until tender stirring occasionally about 1 hour remove from heat and set aside keep the lentils kidney beans and any excess cooking water in the pot"
        },
        {
            "step": "2",
            "desc": "heat vegetable oil in a saucepan over medium-high heat cook cumin seeds in the hot oil until they begin to pop 1 to 2 minutes add cardamom pods cinnamon stick bay leaves and cloves; cook until bay leaves turn brown about 1 minute reduce heat to medium-low; add ginger paste garlic paste turmeric and cayenne pepper stir to coat"
        },
        {
            "step": "3",
            "desc": "stir tomato puree into spice mixture; cook over medium heat until slightly reduced about 5 minutes add chili powder coriander and butter; cook and stir until butter is melted"
        },
        {
            "step": "4",
            "desc": "stir lentils kidney beans and any leftover cooking water into tomato mixture; bring to a boil reduce heat to low stir fenugreek into lentil mixture cover saucepan and simmer until heated through stirring occasionally about 45 minutes add cream and cook until heated through 2 to 4 minutes"
        }
    ],
    "recipe_nutrition": "per serving: 390 calories; 215 g fat; 371 g carbohydrates; 132 g protein; 48 mg cholesterol; 420 mg sodium full nutrition",
    "_id": 

这是 mongo db 中的示例配方代码。请让我知道这是否是将配料放入 mongo 的正确方法。如果它是什么方法来获取类似的项目。请帮忙

【问题讨论】:

标签: python mongodb flask


【解决方案1】:

我已经稍微更新了你的结构。你怎么看这样的事情:

[{
    "_id": {
        "$oid": "5e2acfc83a695633379fe01c"
    },
    "date_added": ISODate("2020-01-24T00:00:00.000Z"),
    "category": "Indian",
    "recipe": "Chicken Curry",
    "short_description": "Best Curry",
    "images": [{
            "size": "small",
            "url": "http://cdn..."
        }, {
            "size": "large",
            "url": "http://cdn..."
        },

    ],
    "calories": "500",
    "persons": 2,
    "ingredients": [{
            "ingredient": "chicken breast",
            "type": "chicken",
            "amount": "100"
        }, {
            "ingredient": "onion",
            "type": "vegetable",
            "amount": "2"
        }, {
            "ingredient": "olive oil",
            "type": "oil",
            "amount": "1"
        }
    ],
    "instructions": [{
            "step": 1,
            "description": "Slice the chicken..."
        }, {
            "step": 2,
            "description": "Slice the onion"
        }
    ]
}

]

当您想按成分获取时,可以使用 $in 函数的 find。

db.collection.find({"ingredients.type": {$in: ["chicken"]}})

在 MongoPlayground (https://mongoplayground.net/p/A9n9D1zGtPQ) 上查看此示例

你还没有尝试自己解决这个挑战...... 您可以将 $or 与 $in 和 JavaScript 正则表达式一起使用。

db.recipes.find({ 
    $or:[
        {"recipe_ingredient.ingr": {$in: [/.*lentils.*/i]}},
        {"recipe_ingredient.ingredient_desc" : {$in: [/.*lentils.*/i]}}
    ]
 })

【讨论】:

  • 确定@Kevin 让我试试这个,如果它不起作用我会发布。
  • 嗨@kevin我发现你的解决方案只有在成分类型完全是鸡肉时才有效,像鸡肉一样的查询是什么,因为我的数据库中有一段时间我没有类型所以我必须检查用成分键,所以我必须在鸡肉和泰国鸡肉之间进行检查,或者类似的东西。请帮帮我,我会评价你的回答。谢谢
  • 嗨@Kevin 我已经编辑了我的问题,将我的数据库结构更改为这个。请帮我找出我应该写什么查询才能找到带有成分名称的食谱。 ingr_des 是我的内容,我使用 spcy lib 根据句子名词、代词、动词将 ingr 和内容分开。那么是否有任何查询可以找到类似的项目,即像扁豆一样对成分.ingr 或成分.内容的 LIKE 调用???
猜你喜欢
  • 1970-01-01
  • 2016-03-14
  • 1970-01-01
  • 2019-01-03
  • 2015-06-16
  • 2020-09-07
  • 2015-11-27
  • 1970-01-01
  • 2013-08-03
相关资源
最近更新 更多