这是我的 json 数据,我想将其保存在 mongoose 数据库中,但我很困惑如何设计我的 mongoose 架构。
data is { 'posts[0][commentId]': '0',
'posts[0][id]': '1',
'posts[0][postName]': 'hi',
'posts[0][img]': '',
'posts[0][postBy]': ' Neeraj',
'posts[1][commentId]': '0',
'posts[1][id]': '2',
'posts[1][postName]': 'hii',
'posts[1][img]': '',
'posts[1][postBy]': ' Neeraj',
'posts[1][comments][0][commentId]': '1',
'posts[1][comments][0][commentValue]': 'hlo how are u???',
'posts[1][comments][0][commentBy]': ' Neeraj',
'posts[1][comments][0][upCounter]': '5',
'posts[1][comments][0][downCounter]': '6' }
这是我的帖子架构,但我不知道如何实际设计它。
var postSchema=new Schema({
posts:[{
commentId: Number ,
comments : [{
commentBy : String,
commentId : String,
commentValue:String,
date:Date,
downCounter:Number,
upCounter:Number
}],
id:String,
img:String,
postBy:String,
postDate:Date,
postName:String
}]
});
最佳答案
您只需将 posts 属性设置为 mongoose 架构中的对象并存储整个对象即可。
像这样的事情:
var postSchema = new Schema({
posts: {
type: Object,
required: false //depends on whether the field is mandatory or not
}
});
关于javascript - 如何在 mongoose 中保存 json 数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47700472/