Reputation: 1
I'm trying to populate a mongoose result with a subdocument, but I can't.
I tried this code, but it get this error:
MissingSchemaError: Schema hasn't been registered for model "department.subDepartment"
Mongoose schema:
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)
Function:
const result = await User.findById('5d31dfeec4d0af0cb2f448fc').populate('subDepartment')
console.log(JSON.stringify(result.subDepartment))
On the DB I must have only two documents (department and user), subDepartment should be a Department's subdocument
How can I populate 'subDepartment'?
Upvotes: 0
Views: 280
Reputation: 2648
You can't do populate
with nested fields like departments.subDepartments
. Instead, you shoud populate the department, and then pick out subdepartments of the departments for the user.
index.js
const app = require("express")();
const morgan = require("morgan");
const bodyParser = require("body-parser");
const mongoose = require("mongoose");
const models = require("./models");
app.use(morgan("dev"));
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: true }));
app.post("/init", async (req, res) => {
const department = await models.Department.create({
name: "ICT",
city: "CA",
subDepartments: {
name: "Yolo",
city: "Solo",
},
});
const user = await models.User.create({
name: "John Wick",
city: "NY",
department: department._id,
});
return res.status(201).send({
message: "Created",
data: {
user,
department,
},
});
});
app.get("/users", async (req, res) => {
const user = await models.User.findOne().populate("department");
return res.send({ user });
});
mongoose.connect("mongodb://root:[email protected]:27017/stackoverflow");
mongoose.connection.on("error", err => console.error(err));
app.listen(3000, () => console.log("Listening on :3000"));
models.js
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);
GET
request to /users
will return
{
"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
}
}
Upvotes: 0
Reputation: 5249
You have not created subDepartment
model, just schema, thus you are also referencing the wrong type in DepartmentSchema
.
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'}]
}
Upvotes: 0