猫鼬:填充objectIds的嵌套数组

时间:2018-10-15 06:23:41

标签: node.js mongodb api express mongoose

我有一个User模型,其架构如下所示,我试图填充dealswishes中的子类别,但不能。

image: { type: String, default: 'NA' },
firstName: { type: String, default: 'first name' },
lastName: { type: String, default: 'last name' },
email: { type: String, lowercase: true, unique: true, trim: true },
password: { type: String, min: 6 },
deals: [
  {
    _id : false,
    category: { type: Schema.Types.ObjectId, ref: 'Category' },
    subCategory: [{ type: Schema.Types.ObjectId, ref: 'Subcategory' }]
  }
],
wishes: [
  {
    _id : false,
    category: { type: Schema.Types.ObjectId, ref: 'Category' },
    subCategory: [{ type: Schema.Types.ObjectId, ref: 'Subcategory' }]
  }
]

这是一个示例JSON响应

"deals": [
  {
      "subCategory": [
          "5bc419cea25fc606153bdbe4",
          "5bc419cea25fc606153bdbe3"
      ],
      "category": "5bc419cea25fc606153bdbe2"
  }
],
"wishes": [
  {
      "subCategory": [
          "5bc41a40a25fc606153bdbe5",
          "5bc41a40a25fc606153bdbe7"
      ],
      "category": "5bc41a40a25fc606153bdbe5"
  }
]

我可以填充deals.category,但不能填充deals.subcategory

// method to get users with populated deals and wishes
allUsers: async (req, res, next) => {
  const users = await User.find({})
    .populate('deals.category', 'id name')
    .populate('wishes.category', 'id name')
    .populate('deals.subCategory');

  return respondSuccess(res, null, users);
},

// currently the response i'm getting
"deals": [
  {
      "subCategory": [],
      "category": {
          "_id": "5bc419cea25fc606153bdbe2",
          "name": "Auto"
      }
  }
],
"wishes": [
  {
      "subCategory": [
          "5bc41a40a25fc606153bdbe5",
          "5bc41a40a25fc606153bdbe7"
      ],
      "category": {
          "_id": "5bc41a40a25fc606153bdbe5",
          "name": "Baby Sitting"
      }
  }
]

寻找急需的帮助。

谢谢

0 个答案:

没有答案
相关问题