Relationship in Mongodb

Asked

Viewed 3,114 times

1

Folks good afternoon, I’m in need of some help from someone who has experience with Mongodb to clear me a doubt. I’m studying about Mongodb and I came across the following problem:

I have 2 Collections:

Customers and Users

Each customer has their user list referenced by the respective Objectid. Each user has a Proprietary field to identify the Customer he belongs to.

The point is that I need to create a structure where a client can father other clients, that is to say in my client object I will have a property [Children], where it will contain the Objectid of each Son, and will have tbm the proprietary field to identify if he is the son of someone.

My problem is: How to filter all the users that are mine and the users that are my children’s and my children’s and successively.

I need to create this architecture in Mongodb and I have no idea how to do it, I’m using Node.JS + Mongoose + Mongodb.

Thanks for your help.

1 answer

0


The problem is that you are using an sql modeling in a nosql database. The correct thing is that you do not have references, but rather the objects themselves, instead of storing an id of a parent object in a child object, place a Collection of children directly in this parent object. In nosql there are no joins it is quite different than the conventional sql,to do "joins" in simple cases you would use Aggregation: https://docs.mongodb.org/manual/core/aggregation-pipeline/ and in more complex cases map-reduce: https://docs.mongodb.org/manual/core/map-reduce/

both are much more performative than conventional sql joins, but are not as simple as the same.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.