我要添加的新属性基本上获取了名为“history”的数组中的最后一个对象,该对象位于文档的根目录下,并将其填充到名为“activeStatus”的属性中,该属性也位于文档的根目录下。
我试着用
Object.assign
像这样:
if (doc) {
const appendedDoc = Object.assign({ activeStatus: doc.history.slice(-1)[0] }, doc);
doc = appendedDoc;
}
res.send(doc);
但是我在最后一个文档中得到了额外的元数据——包括getter、严格模式状态等。
我怎样才能返回我想要的干净文档呢。
顺便说一下,我的初始文档如下所示:
{
"_id": <id value>,
"type": "permanent",
"gender": "female",
"history": [
{
"endDate": "2018-10-31T12:27:17.721Z",
"stage": "training",
"completed": true,
"startDate": "2018-10-30T13:41:18.714Z"
},
{
"stage": "active",
"completed": false,
"startDate": "2018-10-31T12:27:17.572Z"
}
]
}
这是我想要的文件:
{
"_id": <id value>,
"type": "permanent",
"gender": "female",
"history": [
{
"endDate": "2018-10-31T12:27:17.721Z",
"stage": "training",
"completed": true,
"startDate": "2018-10-30T13:41:18.714Z"
},
{
"stage": "employed",
"completed": false,
"startDate": "2018-10-31T12:27:17.572Z"
}
],
"activeStatus": {
"stage": "employed",
"completed": false,
"startDate": "2018-10-31T12:27:17.572Z"
}
}