如何在 Mongodb 中正确填充嵌套模式

how to populate nested schema in Mongodb correctly

提问人:Chayie lucrese 提问时间:8/21/2023 最后编辑:toyota SupraChayie lucrese 更新时间:8/21/2023 访问量:9

问:

这是我尝试过的。对我来说,这似乎是正确的,但问题是它只附带_ids,不包括构成架构 Class、Department 和 Level 的其他字段。

这是我的架构,我的架构的特定部分。

 Department: {
    type: Object,
    require: true,
    Name: { type: Schema.Types.ObjectId, ref: "Department", require:true },
    Level: { type: Schema.Types.ObjectId, ref: "Level" , require:true},
    Class: { type: Schema.Types.ObjectId, ref: "Class" , require:true},
  },

这是我的控制器:

 (
  const find_id = await Student.findById({ _id: id })
    .select(["Name", "Surname", "Gender", "Email", "Address", "Department","Level", "Class"])
    .populate("Department", "Name")
    .populate("Department.Level" )
    .populate("Department.Class");

这是我的结果:

"student": {
    "_id": "64e32afc1c22ff84509bb386",
    "Name": "Mrs Lucrese",
    "Surname": "chayie",
    "Gender": "female",
    "Email": "[email protected]",
    "Address": {
        "city": "yaounde",
        "country": "cameroon"
    },
    "Department": {
        "Name": "64df588a5325cc02fc18204d",
        "Level": "64b9d1cb7d3529d9d9011c75",
        "Class": "64df594d5325cc02fc18205f"
    }
},
"message": "success"
对象 猫鼬 嵌套 种群

评论


答: 暂无答案