猫鼬使用相同的路径多次填充

时间:2018-07-27 23:18:42

标签: mongodb mongoose

我需要多次在路径:“ suppliesData.supplies”,路径“ altRecipes.altRecipe”和路径“ employee”处使用相同的路径“ recipes”填充模型。

  Table.updateTable(id, order, {}, (err, order) => {
  if (err) {
    throw err;
  }
  Order.populate(
    order,
    {
      path: "recipes",
      populate: {
        path: "suppliesData.supplies",
      }
    },
    (err, order) => {
      if (err) {
        throw err;
      }
       Order.populate(
    order,
    {
      path: "employee",
      }
    },
    (err, order) => {
      if (err) {
        throw err;
      }
     Order.populate(
    order,
    {
      path: "recipes",
      populate: {
        path: "altRecipes.altRecipe"
      }
    },
    (err, order) => {
      if (err) {
        throw err;
      }
      res.json(order);
    }
  );
});
    }
  );
});
    }
  );
});

}

documentation中明确指出:

如果您使用相同的路径多次调用populate(),则只有最后一个才会生效。

//下面的第二个populate()调用将覆盖第一个,因为它们 //都填充“粉丝”。

Story.
  find().
  populate({ path: 'fans', select: 'name' }).
  populate({ path: 'fans', select: 'email' });
// The above is equivalent to:
Story.find().populate({ path: 'fans', select: 'email' });

在我的情况下,路径“ suppliesData.supplies”和“ altRecipes.altRecipe”都填充了配方,因此,我可以理解发生了什么,但是,它们没有解释如何解决该情况,因此我找不到任何示例关于如何做。

非常感谢任何帮助。

Json是:

 {
    "orders": [
        "pizza x(1) "
    ],
    "recipes": [
        {
            "state": true,
            "_id": "5b50f78924d76e204c0a2d83",
            "name": "Baby Biff",
            "suppliesData": [
                {
                    "_id": "5b50f78924d76e204c0a2d88",
                    "qt": "1",
                    "un": "kg",
                    "supplies": "5b32c4096a257d45950d7b12"
                }],
            "altRecipes": [
                {
                    "_id": "5b50f78924d76e204c0a2d89",
                    "qt": "25",
                    "un": "gr",
                    "altRecipe": {
                        "adc": true,
                        "state": true,
                        "_id": "5b4fc8ddbf9d613b47705554",
                        "costCenter": "5b43e061cb1b773ce3cd1a0d",
                        "adic": {
                            "_id": "5b4fc8ddbf9d613b47705555",
                            "qr": "350",
                            "un": "kg"
                        },
                        "add": {
                            "_id": "5b4fc8ddbf9d613b47705556",
                            "qt": "100",
                            "un": "gr"
                        },
                        "name": "french fries",
                        "suppliesData": [
                            {
                                "_id": "5b4fc8ddbf9d613b47705558",
                                "qr": "1",
                                "un": "kg",
                                "supplies": "5b3f9f13be9d461735f63f4a"
                            },
                            {
                                "_id": "5b4fc8ddbf9d613b47705557",
                                "qt": "1",
                                "un": "gr",
                                "supplies": "5b32b86b97473f36a3d62297"
                            }
                        ],
                        "min": "3",
                        "max": "5",
                        "opt": "4",
                        "crit": "2",
                        "exp": "3",
                        "price": "8",
                        "__v": 0
                    }
                }
            ],
            "__v": 0
        }
    ],
    "_id": "5b5bb5d536131a4d1d6d2f6c",
    "employee": "59c46d202e2164b5c1aa1b93",
    "sent": true,
    "table": "5b3305ca337b8e2846e33e67",
    "date": "2018-07-28T00:16:21.698Z",
    "orderNumber": 210,
    "__v": 0

编辑:很抱歉,看错了杰森,这就是那个

更新:我做了显示的更改,现在它填充了雇员和路径:“ altRecipes.altRecipe”,但没有填充路径:“ suppliesData.supplies”(或者是,它确实填充了“ suppliesData.supplies”,但是被覆盖由“ altRecipes.altRecipe”),因此它正按文档所述运作。.问题仍然相同,如何处理?

0 个答案:

没有答案