MongoDB里,我將評(píng)論以dict形式存放在list里,由于要考慮到評(píng)論的修改和刪除,所以不知道怎樣給這些評(píng)論創(chuàng)建id,各位有沒(méi)有好辦法?
{ "_id" : ObjectId(), "comments" : [ { "id" : '#就是這里的id#', "text" : "", }, ] }
歡迎選擇我的課程,讓我們一起見(jiàn)證您的進(jìn)步~~
Can you use MongoDB’s ObjectId
UPDATE:
Comments should be attached to the article, not subordinate to the article
So, create another collection, put comments, and then it contains an indexed field pointing to the objid of the article.
@felix021: Well, add a field for the current maximum floor, and the deleted floor will not affect other floors.
{ "_id" : ObjectId(), "comments_max_id" : int, "comments" : [ { "id" : int, "text" : "", }, ] }
comments_max_id starts from 1 and is 0 when there are no comments
When adding a new one, assign the new id and comments_max_id to comments_max_id+1 at the same time
Deleting will not affect other replies
ObjectId() and the index of comments can determine a comment
ObjectId() and 0 can be known to be the first reply to the article with id = ObjectId()
If you need to manage all replies, use the method above