0

I'm building out an app that basically allows users to manage an online address book.

I'm a SQL guy and am used to normalizing my Db.

I'm developing my first app using the MEAN stack and I need help understanding best practice for MongoDb.

I realize you can do joins in Mongo, but from what I've read most advice is to store everything into a single record within a collection.

In my case, I have a user, who could have over 1,000 or more contacts in their account, with over 100 fields each.

I'm hoping someone with a lot of Mongo development can chime in and tell me what a best practice would be. Would you really have one document with over a thousand contacts? How does that scale?

cnak2
  • 1,711
  • 3
  • 28
  • 53

1 Answers1

2

There is a general question, in NoSQL, which is: to embed or to have a separate collection?

My general recommendation is to think of the relationship:

  • One to one? Embed.
  • One to few? Do as they told you.. Put inside and avoid joins
  • One to many? A MongoDB record can hold 16MB. So it's hard you pass it. But if each record is heavy, please make select queries specifying the desired fields... otherwise the queries would be heavy.
  • If you really can't do it by embedding.. you need separate collections. And you won't have real joins.. you'll have to do "manual joins": first query to get the user.. second query to get his contacts. But be careful with N+1 query problem (tip: you can use in operator or you can rely on driver capabilities to do joins which I never tried).

But don't forget: if the embedded entity is to be used elsewhere... you can't embed as you end up with repeated data (in principle). Or you have to deal with that (in some cases it's not a problem).

Community
  • 1
  • 1
Luís Soares
  • 5,726
  • 4
  • 39
  • 66