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!');
});
发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744336387a4569137.html
评论列表(0条)