【发布时间】:2016-05-29 06:10:07
【问题描述】:
这是我的 JSON:
{
"title": "This an item",
"date":1000123123,
"data": [
{
"type": "html",
"content": "<h1>Hi there, this is a H1</h1>"
},
{
"type":"img",
"content": [
{
"title": "Image 1",
"url": "www.google.com/1.jpg",
"description":"This is the first image"
}
]
},
{
"type": "map",
"content": [
{
"lat":323434555,
"lng":4444343434,
"description":"this is just a place"
}
]
}
]
}
如您所见,“data”字段存储了一个对象数组,其中“content”字段是可变的。
我应该如何在 Mongoose 中建模?
这就是我定义架构的方式:
module.exports = mongoose.model('TestObject', new Schema({
title: String,
date: Date,
data: [
{
type: String,
content: Object
}
]
}));
这是“数据”字段的响应:
"data": [
{
"type":"img",
"content": [ "[object Object]" ]
},
{
"type":"map",
"content": [ "[object Object]" ]
}
]
在 Mongoose 中为对象定义可变数据类型的正确方法是什么?
【问题讨论】: