paulkon
paulkon

Reputation: 1855

Nested vs. flat structures in mongodb?

What metrics should I use to decide whether I want to structure my document like:

{ count: { comments: Integer, views: Integer } }

vs.

{ commentCount: Integer, viewCount: Integer }

Since indexing count.comments vs. commentCount and querying on both is essentially equivalent, which structure would make sense in this situation?

Upvotes: 1

Views: 1530

Answers (1)

pedrommuller
pedrommuller

Reputation: 16066

Typically that will depend on how many writes/reads you are going to perform over the collection, in the case of you're going to update the counts for views and comments very often I'd suggest to go with a plain approach, other downside is that I look for creating a child complex type is the level of verbosity, if creating those kind of objects are not completely required try to keep it as much simple as possible, the queries would be a lot of easier to implement in more complex scenarios.

my two cents.

Upvotes: 1

Related Questions