Ask: mongodb Post Class Table Design

  mongodb, question

1. Posts have content and replies. If only one table is used in mongodb, it can be designed like this

{
 Title:' balabalabala',
 Content:' balabalabala',
 comments:[
 {
 User:123,// response from user 123
 content:'123'
 },
 {
 User:456,// reply from user 456
 content:'456'
 }
 ]
 }

How to modify the content in user:456?

1) Is it similar to array subscript?  Comments [1].  How to write?

If you already know{user: 456}Subscript, can use array subscript way to change:

db.coll.update({...}, {$set: {"comments.1.content": "567"}})

Or according to the query conditions:

db.coll.update({"comments.user": 456}, {$set: {"comments.$.content": 567}})

among them$Represents an array element on a match. However, this method only modifies the first matching array element. Therefore, it is important to note that your conditions must be exactly matched to the element you want to change. Take your data as an example. If this user commented twice, there would be a problem in writing.