Is it okay to have non-symmetrical documents in a collection?

Hello,

I know MongoDb allows this, but I was still wondering whether this is more of an edge case than an everyday feature:

Is it okay to have Documents in a Collection that are structurally not identical to each other?

I am currently working on a Products model whereby it is possible that some properties are not available (based on some user settings) and thus some fields would be completely missing whereas in other Products documents, again based on user settings, those fields might exist.

Is this a good / acceptable practice or is this a sign of poor data planning? Can this create pitfalls for optimisation/ errors down the road?

Thank you!

The answer to all your questions is “Yes” :grin:

  • It is okay.
  • It is an acceptable practice.
  • It could be a sign of poor data planning.
  • It could create pitfalls.

I create my collections with validators. If you can’t create a reasonable validator for your collection, that’s probably a sign you’re in trouble.

1 Like

Hello,

Thanks for your witty answer! Could you kindly elaborate more on how a validator could save me from trouble in this particular scenario?

Thank you!

p.s. what would you say scales better - default / empty values or completely missing fields?

Validators of course help by establishing the validity of data entered.
But they also make you explain your design to yourself. It’s a sanity check on the developer.

In the MongoDB model of databasing (really, document management) I’d go for missing fields rather than emulating a RDBMS by having, effectively, NULLs in fields.

2 Likes

Thank you very much for your kindness and for taking a moment to share your expertise.

Much appreciated

1 Like

You’re welcome … have fun … and when modelling solutions, there are generally no wrong answers, just better answers.

1 Like

This topic was automatically closed 5 days after the last reply. New replies are no longer allowed.