CodePark/ questions / SQL vs NoSQL & MongoDB
START PRACTICINGPRACTICE
Backend question · Databases & SQL › SQL vs NoSQL & MongoDB

A MongoDB posts document embeds every comment in a comments array. Some posts receive tens of thousands of comments. What is the main problem?

The document grows without bound toward the 16 MB document limit, and every read and update handles a huge document

The document grows without limit. A BSON document (MongoDB's binary JSON format) cannot be larger than 16 MB. An ever-growing array also makes every read and write of the post heavier.

The fix: store comments in their own collection, with a
postId field that references the post. You can also embed only the latest few comments in the post.

The other options are wrong:

• "Arrays cannot be indexed in MongoDB" is false.
Multikey indexes index each element of an array.
• "MongoDB limits arrays to 1,000 elements" is false. There is no such limit.

• "Embedded documents cannot be queried with filters" is false. You can filter on embedded fields, for example with dot notation like
comments.author.