How to return the last push () inline document

I'm stuck trying to figure out if this is possible.

Using the mongoose blog example to demonstrate, however, my actual use case is a bit more complicated:

var Comments = new Schema({ title : String , body : String , date : Date }); var BlogPost = new Schema({ author : ObjectId , title : String , body : String , date : Date , comments : [Comments] }); var BlogPost = mongoose.model('BlogPost'); var post = new BlogPost(); 

I need to create a new comment and return a new comment to the client. Having comments as an embedded document is really convenient for me and works well. However, I do not want to return every comment to the client every time I add a new one.

 post.comments.push({ title: 'My comment' }); 

Since I use mongoose, if I could, how to get the id new comment, which will help, and also allow me to add other functions, such as editing a comment or deleting a comment.

The only way to do this is to have comments as your own collection?

+1
source share
1 answer

I assume that the problem you are trying to solve is how to refresh the page after the client submits a comment, right?

So you are inserting a new comment. This means that you already have your data. Paste and return the data to the client. Or, even better, return a simple question, as the client also has full comments and can display it on its own.

+1
source

Source: https://habr.com/ru/post/1392792/


All Articles