我有这个 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 关系来做到这一点,并将相应的 id 添加到问题和选项中,但在这种情况下我需要通过 Mongo 执行(创建 json 并通过路由获取它)。
我怎样才能以编程方式做到这一点?
提前致谢。