简体   繁体   中英

How can i build mongoose model from this JSON

I'm a beginner trying to parse this json into a mongoose model so i can save the data.

This is my model right now


const commentSchema = new Schema([{

  sectionId: String,comments:
 [{

      id: String,
      authorAvatarUrl: String,
      authorName: String,
      authorId: String,
      authorUrl: String,
      comment: String,

replies:
 [{
        id: String,
        authorAvatarUrl: String,
        authorName: String,
        authorId: String,
        authorUrl: String,
        comment: String,
        parentId: String
      }]
  }]

}]);

const Comment = mongoose.model("Comment", commentSchema);
module.exports = Comment;

and this is the Json i'm trying to map

var existingComments = [{
    "sectionId": "1",
    "comments":
 [{
        "id": 88,
        "authorAvatarUrl": "support/images/jon_snow.png",
        "authorName": "Jon Sno",
        "authorId": 1,
        "authorUrl": "http://en.wikipedia.org/wiki/Kit_Harington",
        "comment": "I'm Ned Stark's bastard",
        "replies":
 [{
          "id": 100,
          "authorAvatarUrl": "support/images/jon_snow.png",
          "authorName": "Jon Sno",
          "authorId": 1,
          "authorUrl": "http://en.wikipedia.org/wiki/Kit_Harington",
          "comment": "P.S.: I know nothing.",
          "parentId": 88
        }]
    }]
}]

On the server side i'm trying to get the comment like this

//Comment posted
router.post("/comments", (req, res, next) => {
  //Save the comment on database
  const [{
    sectionId,

comments:
 [{
      id,
      authorAvatarUrl,
      authorName,
      authorId,
      authorUrl,
      comment,

      replies: 
[{
        id,
        authorAvatarUrl,
        authorName,
        authorId,
        authorUrl,
        comment,
        parentId
      }]
    }]
  }] = req.body;


  const newComment = new Comment([{
    sectionId,comments:
 [{ id, }]

 }]);

  newComment
    .save()
    .then(comment => {
      res.redirect("/index");

    })

    .catch(err => {
      console.log(err);
    });

});

But is not working, any help will be much appreciated because i'm currently trying to uinderstand moongose ODM better. Thanks!!!!

Your comment schema is a bit confusing. You created a "Comment" schema (note this is singular) yet you are trying to use the comment schema as an array of comments.

Tip: Keep your schemas and singular.

To achieve your desired design whilst keeping your models singular you can try this:

Solution (1): Have a single "Section" collection storing all the data Note: although you have multiple schemas, we only ever model the SectionSchema and it will be our only collection where every Section document contains all the info you need.

const sectionSchema = new Schema({
    name: {
        type: String,
        required: [true, 'Section name is required']
    },
    comments: {
        type: [commentSchema]
    }
});

const replySchema = new Schema({
    //id: String, //not needed, mongoose will automatically add an "_id" property when you save 
    authorAvatarUrl: String,
    authorName: String,
    authorId: String,
    authorUrl: String,
    comment: String,
    parentId: String
});

const commentSchema = new Schema({
    //id: String, //not needed, mongoose will automatically add an "_id" property when you save 
    authorAvatarUrl: String,
    authorName: String,
    authorId: String,
    authorUrl: String,
    comment: String,
    replies:{
        type: [replySchema]
    }
});

module.exports =  mongoose.model("Section", sectionSchema);

In the above solution, you can still have routes to address only specific comments, for example:

router.get("/sections/:id") //gets the entire section document matching the ID
router.post("/sections/:id/comments/") //creates a new comment within section with matching ID. Simply push a new comment into the comments array
router.get("/sections/:id/comments/") //gets all the comments for this section. Simply return only the comments property of the section document.
router.get("/sections/:id/comments/:commentId") //gets a specific comment within a specific section. Filter the comments array to get the comment matching the commentId and then only return that.

etc..

Final note: there are other ways you can model your data. This is just one example. For instance, you can have a section collection and a comment collection, where a comment document stores a section Id indicating the section that that comment document relates to.

Have a look at https://mongoosejs.com/docs/subdocs.html and https://docs.mongodb.com/manual/core/data-modeling-introduction/ it might help you to understand the different ways you can model your data and also how mongoose subdocuments work and how to use them.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM