javascript - How can i save json data in mongoose - Stack Overflow

This is my json data I want to save it in a mongoose database but I am confused how to design my mongoo

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
Add a ment  | 

2 Answers 2

Reset to default 4

You 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

相关推荐

  • javascript - How can i save json data in mongoose - Stack Overflow

    This is my json data I want to save it in a mongoose database but I am confused how to design my mongoo

    7天前
    20

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信