Relationship between Mongodb collections

Asked

Viewed 440 times

2

I wonder if it is good practice to create a relationship between two collections (Collections) in Mongodb.

Suppose I have a collection for users, with a scheme like this:

{ 
  "_id": ObjectId(...),
  "username": "",
  "groups": [
    ObjectId(...),
    ObjectId(...)
  ]
}

But the ID’s placed in the array groups would be referring to the ID’s of the collection groups "groups".

This is good practice or should I use SQL for this?
If it’s good practice, how can I create this kind of thing in Mongodb?

1 answer

2


This is good practice or should I use SQL for this?

Yes, this practice (referential ID storage on object) is common in implementations using Mongodb.

If it’s good practice, how can I create this kind of thing in Mongodb?

Using, in the object that stores the references, the same type of data used on the property _id of the referenced object.

Browser other questions tagged

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