猫鼬搜索关键字并在两个集合之间保存

时间:2020-04-09 10:03:14

标签: node.js mongodb search mongoose

我是猫鼬的新手。我正在创建一个食谱搜索应用程序。我尝试从成分字符串生成关键字。通过使用猫鼬“ $ lookup”。我有两个猫鼬模式食谱 FoodLibrary ,它们的集合是“ 食谱”,“ foodlibraries ”。

食谱架构集合如下:

title: "Kung Pao Tofu"
ingredients:
 0: "1 14-ounce package extra firm tofu, drained"
 1: "¼ cup plus 2 tablespoons reduced sodium or gluten-free soy sauce"
 2: "3 tablespoons cornstarch, divided"
 3: "1 2-inch piece ginger, minced"

FoodLibrary模式集合如下所示:

name: "almond"
category: "nuts and beans"

name: "cucumber"
category: "vegetables"

我想查看每个成分数组元素的foodlibraries集合并匹配最接近的词,然后像这样保存它:

title: "Kung Pao Tofu"
ingredients:
 0: "1 14-ounce package extra firm tofu, drained"
 1: "¼ cup plus 2 tablespoons reduced sodium or gluten-free soy sauce"
 2: "3 tablespoons cornstarch, divided"
 3: "1 2-inch piece ginger, minced"
keywords:
 0: "tofu"
 1: "soy sauce"
 2: "cornstarch"
 3: "ginger"

我尝试了这段代码,但在这种情况下似乎不起作用:

Recipe.aggregate([
      { "$unwind": { "path": "$ingredients" }},
      { "$lookup": {
        "from": "foodlibraries",
        "localField": "ingredients",
        "foreignField": "name",
        "as": "keywords"
      }},
    ])

谢谢您的帮助。

0 个答案:

没有答案