Node.js 如何使用子文档';s阵列?

Node.js 如何使用子文档';s阵列?,node.js,mongoose,mongoose-populate,Node.js,Mongoose,Mongoose Populate,我试图用子文档填充mongoose结果,但我不能 我尝试了此代码,但它得到了以下错误: MissingSchemaError:尚未为模型“department.subDepartment”注册架构 猫鼬模式: const subDepartmentSchema = new Schema({ name: { type: String, required: true }, city: { type: String, required: true } }) const DepartmentS

我试图用子文档填充mongoose结果,但我不能

我尝试了此代码,但它得到了以下错误:

MissingSchemaError:尚未为模型“department.subDepartment”注册架构

猫鼬模式:

const subDepartmentSchema = new Schema({
  name: { type: String, required: true },
  city: { type: String, required: true }
})

const DepartmentSchema = new Schema({
  name: { type: String, required: true },
  city: { type: String, required: true },
  subDepartment: [subDepartmentSchema]
}
)

const UserSchema = new Schema({
  name: { type: String, required: true },
  city: { type: String, required: true },
  department: { type: Schema.Types.ObjectId, required: true, ref: 'department' },
  subDepartment: { type: Schema.Types.ObjectId, required: true, ref: 'department.subDepartment' },
  active: { type: Boolean, required: true, default: true }
}
)

const Department = mongoose.model('department', DepartmentSchema)
const User = mongoose.model('user', UserSchema)
功能:

const result = await User.findById('5d31dfeec4d0af0cb2f448fc').populate('subDepartment')
console.log(JSON.stringify(result.subDepartment))
在DB上,我必须只有两个文档(部门和用户),子部门应该是部门的子文档


如何填充“subDepartment”?

您没有创建
subDepartment
模型,只是架构,因此您在
部门架构中引用了错误的类型

const subDepartment = mongoose.model('subDepartment', subDepartmentSchema) // this line is missing
const DepartmentSchema = new Schema({
  name: { type: String, required: true },
  city: { type: String, required: true },
  subDepartment: [{ type: Schema.Types.ObjectId, ref:'subDepartment'}]
}

不能使用嵌套字段(如
departments.subDepartments
)填充
。相反,您应该填充部门,然后为用户选择部门的子部门

index.js

models.js

GET
/users
的请求将返回

{
    "user": {
        "active": true,
        "_id": "5d8aa2c36ac2502b3c33794b",
        "name": "John Wick",
        "city": "NY",
        "department": {
            "_id": "5d8aa2c36ac2502b3c337949",
            "name": "ICT",
            "city": "CA",
            "subDepartments": [
                {
                    "_id": "5d8aa2c36ac2502b3c33794a",
                    "name": "Yolo",
                    "city": "Solo"
                }
            ],
            "__v": 0
        },
        "__v": 0
    }
}

我尝试过,但现在结果是空的,在数据库上我必须只有两个文档(部门和用户),我将使用解决方案添加一个新文档。
const mongoose = require("mongoose");

const Schema = mongoose.Schema;

const DepartmentSchema = new Schema({
  name: { type: String, required: true },
  city: { type: String, required: true },
  subDepartments: [
    {
      name: { type: String, required: true },
      city: { type: String, required: true },
    },
  ],
});

const UserSchema = new Schema({
  name: { type: String, required: true },
  city: { type: String, required: true },
  department: {
    type: Schema.Types.ObjectId,
    required: true,
    ref: "department",
  },
  active: { type: Boolean, required: true, default: true },
});

exports.Department = mongoose.model("department", DepartmentSchema);
exports.User = mongoose.model("user", UserSchema);
{
    "user": {
        "active": true,
        "_id": "5d8aa2c36ac2502b3c33794b",
        "name": "John Wick",
        "city": "NY",
        "department": {
            "_id": "5d8aa2c36ac2502b3c337949",
            "name": "ICT",
            "city": "CA",
            "subDepartments": [
                {
                    "_id": "5d8aa2c36ac2502b3c33794a",
                    "name": "Yolo",
                    "city": "Solo"
                }
            ],
            "__v": 0
        },
        "__v": 0
    }
}