Now that we had a look at a one-to-many relationship where embedding made sense,
let's have a look at a relationship, a one-to-many relationship where splitting it in collections might
make sense. Here
I got a couple of cities like New York, Berlin and so on and I got the citizens, let's say we have a database,
a collection which holds all the major cities of the world and then a list of every person living in
that city, like the registry of the state where all people are registered, let's say.
So we get something like this,
the problem we'll face here,
theoretically that would make sense to be a one-to-many relationship where you embed your documents
but for one from application perspective, you might often be interested in fetching all the cities with
their metadata but you don't want to fetch all the citizens along with the cities because in New York
where we have like millions of people, that would be a lot of overhead data which we don't need which
takes very long to fetch and which is a lot of data to transfer over the wire.
And additionally for big cities with millions of citizens, you might indeed hit this 16mb limit
for the overall document
and keep in mind, that limit counts towards a document in nested documents,
so it's not 16mb per nested element, it's 16mb for the overall document,
so you might face this limit too or might hit this limit too.
So for technical and application reasons, here
splitting it up might make sense.
So here, you might have your city data database and in there, you have your cities
collection where you insert a city like the name, New York City coordinates which is a nested document
with a latitude
and this is arbitrary,
this is not a real latitude and longitude, something like this
and you store no data about the citizens in there,
so you also have no citizens array in here with all the IDs of the citizens because that would be a super
long array too if you have millions of citizens.
So you just have the metadata
and there
I got to access square bracket.
So you just have
that stored
and therefore if you find that one city, it looks like this.
And you might also store the citizens but you will do this,
citizens in a separate collection and then there you might have your different citizens with the name
Max Schwarzmuller and the city ID maybe
and that ID could be in New York City to match it by the name or you simply use that object ID of
course, you want to use something which is unique so let's go for the objectId.
So that would be one citizen and then we have another citizen where you also have a name,
Manuel Lorenz and
we're not really living in New York
but just as an example here, just store that like this.
Now insertMany needs square brackets around all the objects you're inserting, so don't forget to
add these, hit enter
and I got a typo, I was missing the closing parentheses after objectId.
And now I inserted my citizens
and if I inspect my citizens, if I find them all and I pretty print this,
here are my citizens
and now I can always retrieve just as citizens and match them to their city with the city ID if I need
to or fetch just the cities and I'll not hit the document limitation of 16mb for the city because
I don't embed all my citizens and I also don't unnecessarily fetch all the citizens along with the city
if I'm only interested in the city metadata.