Relationship in MongoDB

1

Good afternoon, I'm in need of help from someone who has experience with MongoDB to get me out of a doubt. I'm studying about MongoDB and I came across the following problem:

I have 2 Collections:

Customers and Users

Each client has its list of users referenced by the respective ObjectID. Each user has an Owner field to identify the Client that it belongs to.

The issue is that I need to create a structure where a client can be the parent of other clients, ie on my client object I will have a [Children] property, where it will contain the ObjectID of each Son, and will also have the owner field to identify if it is someone's child.

My problem is: How to filter all users who are mine and the users who are my children and my children's children, and so on.

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 in advance for your help.

    
asked by anonymous 14.03.2016 / 18:46

1 answer

0

The problem is that you are using an sql model in a nosql database. The correct thing is that you do not have references but the objects themselves, instead of storing an id of a parent object in a child object, directly put a collection of children in this parent object. In nosql there are no joins it is quite different than the conventional sql, for joins in simple cases you would use aggregation: link and in more complex cases map-reduce: link

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

    
14.03.2016 / 19:36