Nested Comments in MongoDB

后端 未结 2 1302
忘掉有多难
忘掉有多难 2021-01-06 04:57

I\'m quite new to MongoDB and trying to build a nested comment system with it. On the net you\'re finding various document structures to achieve that, but I\'m looking for s

2条回答
  •  不知归路
    2021-01-06 05:11

    Have you considered storing the comments in all documents that need a reference to them? If you have a document for the user, store all of that user's comments in it. If you have a separate document for objects, store all comments there also. It feels sort of wrong after coming from a relational world where you try to have exactly one copy of a given piece of data, and then reference it by ID, but even with relational databases you have to start duplicating data if you want queries to run quickly.

    With this design, each document that you load would be "complete". It would have all the data you need, and indexes on that collection would keep reads fast. The price would be slightly slower writes, and more of a headache when you need to update the comment text, since you need to update more than one document.

提交回复
热议问题