我有这个json:
{
"data": [
"id": "1",
"name": "Sample test",
"description": "this is a sample test",
"category": "tests",
"points": 100,
"startDate":"2018-02-15 00:00:00",
"endDate":"2018-02-22 00:00:00",
"isActive":true,
"alreadyAnswered":false,
"questions":[
{
"id": 1,
"text": "What is your name",
"type": "text",
},
{
"id": 2,
"text": "What is your favorite color",
"type": "select",
"options": [
{
"id": 1,
"text": "Red",
"value": "red"
},
{
"id": 2,
"text": "Blue",
"value": "blue"
}
]
}
]
]
}
我需要将此json创建到mongo数据库中,以便可以通过节点应用程序获取它。
这是我当前的模式:
let TestSchema = new Schema({
id: Number,
name: String,
description: String,
category: String,
points: Number,
startDate: Date,
endDate: Date,
isActive: Boolean,
alreadyAnswered: Boolean
});
我最大的问题是我不知道如何在我的架构中添加其他对象以复制json,在MySQL中,我会使用hasmany关系将其添加到问题和选项中,但是在这种情况下我需要通过Mongo(创建json并通过路由获取)。
我该如何编程? 预先感谢。
答案 0 :(得分:5)
data: [
id: String, //or number, whatever you need
name: String,
description: String,
category: String,
points: Number,
startDate: Date,
endDate: Date,
isActive: Boolean,
alreadyAnswered: Boolean,
questions:[{
id: String, //or again, number
text: String,
type: String,
options: [
{
id: String, //or number
text: String,
value: String
}
]
}
]
]
这应该是此JSON的架构
答案 1 :(得分:0)
回答@Prathamesh More
:
device_meta: [{
// For example
device_name: String,
ID: Number
}]
数组隐式具有默认值[](空数组),因此不需要default
关键字