我创建了两个模式; userSchema 和 boardSchema 。
boardSchema 包含一系列子文档 groupSchema 。
groupSchema 包含一系列子文档 issueSchema 。
issueSchema 具有属性“ assigned_user ”,该属性引用 userSchema
//userSchema
const userSchema = new Schema(
{
username: {...
},
fullname: {
...
},
}
),
//boardSchema
const issueSchema = new Schema({
{
summary: {...
},
....
assigned_user: {
type: mongoose.Schema.Types.ObjectId,
ref: "User",
},
}
);
const groupSchema = new Schema(
{
title: {
type: String,
},
issue: [issueSchema],
},
);
const boardSchema = new Schema(
{
name: {...
},
groups: [groupSchema],
{ timestamps: true }
);
我试图像这样用当前用户填充问题文档;
const { id, group_id, issue_id } = req.params;
await Board.findById(id)
.populate(`groups.id(${group_id})
.issue_id(${issue_id})
.assigned_user`
有没有更简单的方法来解决这个问题?似乎无法找到一种方法来定位特定的Issue文档,而不用每个父级文档ID充斥请求对象。
这是董事会文件的示例
{
"board_type": "public",
"reports": [],
"_id": "5f2b672aa78eaa2ac0f5a30c",
"name": "survivor",
"groups": [
{
"issue": [
{
"resolved": false,
"tags": [],
"reporters": [],
"_id": "5f2b67a0a78eaa2ac0f5a30e",
"summary": "issue A",
"createdAt": "2020-08-06T02:14:56.730Z",
"updatedAt": "2020-08-06T11:44:51.098Z",
"assigned_user": "5ecdf5e059dc404f5caedb82"
}
],
"_id": "5f2b6753a78eaa2ac0f5a30d",
"title": "team A",
"createdAt": "2020-08-06T02:13:39.174Z",
"updatedAt": "2020-08-06T11:44:51.098Z"
}
],
"createdAt": "2020-08-06T02:12:58.815Z",
"updatedAt": "2020-08-06T11:44:51.099Z",
"__v": 1
}