【发布时间】:2018-06-18 01:19:45
【问题描述】:
我有这个 Mongoose 模型架构,其中包含“硬编码”子文档 而且我不知道如何将 _id 重命名为子文档的 id。
对于父级,我也使用以下代码来包含 id 属性,但这似乎不适用于子文档。
menuItemSchema.set('toJSON', {
virtuals: true
})
我尝试了什么:
menuItemSchema.set('toJSON', {
virtuals: true,
extras: { // <-- child
virtuals: true,
}
})
架构
const menuItemSchema = mongoose.Schema({
name: {
type: String
},
extras: [{ // <-- child
name: {
type: String
},
...otherProps
}],
...otherProps
})
menuItemSchema.set('toJSON', {
virtuals: true
})
有办法吗?我是否需要创建一个单独的架构来启用
.set('toJSON', { virtuals: true })
结果
{
"_id": "5b2691666034483916a59fe8",
"name": "Margharita",
"extras": [
{
"_id": "5b2691666034483916a59fed",
"name": "Sauce"
// ^ got no id
},
...
],
"__v": 0,
"id": "5b2691666034483916a59fe8" // <- got id
}
【问题讨论】: