admin管理员组文章数量:1402415
This is my json data I want to save it in a mongoose database but I am confused how to design my mongoose schema.
data is { 'posts[0][mentId]': '0',
'posts[0][id]': '1',
'posts[0][postName]': 'hi',
'posts[0][img]': '',
'posts[0][postBy]': ' Neeraj',
'posts[1][mentId]': '0',
'posts[1][id]': '2',
'posts[1][postName]': 'hii',
'posts[1][img]': '',
'posts[1][postBy]': ' Neeraj',
'posts[1][ments][0][mentId]': '1',
'posts[1][ments][0][mentValue]': 'hlo how are u???',
'posts[1][ments][0][mentBy]': ' Neeraj',
'posts[1][ments][0][upCounter]': '5',
'posts[1][ments][0][downCounter]': '6' }
This is my json data I want to save it in a mongoose database but I am confused how to design my mongoose schema.
data is { 'posts[0][mentId]': '0',
'posts[0][id]': '1',
'posts[0][postName]': 'hi',
'posts[0][img]': '',
'posts[0][postBy]': ' Neeraj',
'posts[1][mentId]': '0',
'posts[1][id]': '2',
'posts[1][postName]': 'hii',
'posts[1][img]': '',
'posts[1][postBy]': ' Neeraj',
'posts[1][ments][0][mentId]': '1',
'posts[1][ments][0][mentValue]': 'hlo how are u???',
'posts[1][ments][0][mentBy]': ' Neeraj',
'posts[1][ments][0][upCounter]': '5',
'posts[1][ments][0][downCounter]': '6' }
this is my Post schema but i dont know how to actually design it.
var postSchema=new Schema({
posts:[{
mentId: Number ,
ments : [{
mentBy : String,
mentId : String,
mentValue:String,
date:Date,
downCounter:Number,
upCounter:Number
}],
id:String,
img:String,
postBy:String,
postDate:Date,
postName:String
}]
});
Share
Improve this question
edited Dec 8, 2017 at 20:42
Eugene Lisitsky
12.9k6 gold badges41 silver badges62 bronze badges
asked Dec 7, 2017 at 17:22
Neeraj JoshiNeeraj Joshi
361 silver badge4 bronze badges
2 Answers
Reset to default 4You can just set your posts property as an Object in your mongoose schema and store the whole object.
Some thing like this :
var postSchema = new Schema({
posts: {
type: Object,
required: false //depends on whether the field is mandatory or not
}
});
You can store JSON to database directly:
var post_schema = mongoose.Schema({data : JSON});
var post_model = mongoose.model('collection_name', post_schema);
var newData = new post_model({data : <json_object>});
//saving json schema to mongodb
newData.save(function(err){
if (err) {
throw err;
}
console.log('INSERTED!');
});
本文标签: javascriptHow can i save json data in mongooseStack Overflow
版权声明:本文标题:javascript - How can i save json data in mongoose - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744336387a2601212.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论