Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/475.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 如何在Mongoose中填充嵌套在对象数组中的文档?_Javascript_Node.js_Mongodb_Typescript_Mongoose - Fatal编程技术网

Javascript 如何在Mongoose中填充嵌套在对象数组中的文档?

Javascript 如何在Mongoose中填充嵌套在对象数组中的文档?,javascript,node.js,mongodb,typescript,mongoose,Javascript,Node.js,Mongodb,Typescript,Mongoose,我想填充IngCreditsList。 是否可以使用填充的成分字段获取阵列? 如果我只想从ingredientsList中选择一个包含填充成分的对象,我该怎么做 希望你能帮忙:) 我的模式如下所示: export const RecipeSchema = new Schema({ name: { type: String, required: 'Enter a name', }, ingredientsList: [ { ingredient: {

我想填充IngCreditsList。 是否可以使用填充的成分字段获取阵列? 如果我只想从ingredientsList中选择一个包含填充成分的对象,我该怎么做

希望你能帮忙:)

我的模式如下所示:

export const RecipeSchema = new Schema({
  name: {
    type: String,
    required: 'Enter a name',
  },
  ingredientsList: [
    {
      ingredient: {
        type: Schema.Types.ObjectId,
        ref: 'Ingredient',
      },
      value: {
        type: Number,
        default: 1,
      },
    },
  ],
});
我的配料模型如下所示:

export const IngredientSchema = new Schema({
  name: {
    type: String,
    required: 'Enter a name',
  },

  created_date: {
    type: Date,
    default: Date.now,
  },
  amount: {
    type: Number,
    default: 1,
  },
});

作为字段,
ingredientsList
,是来自
成分
集合的引用文档数组-您可以使用
model.populate()
,如下所示:

const recipes = await Recipe.find().populate('ingredientsList');

res.send({ data: orders });
这将返回所有
配方
,并为每个配方展开或填充
ingredientsList

假设您只想填充
ingredientsList
中每个文档的
name
字段,具体操作如下:

const recipes = await Recipe.find().populate('ingredientsList', 'name');

res.send({ data: orders });
这将返回所有
配方
,但仅填充每个配方的
ingredientsList
上的
name
字段