Relationship between collections MongoDB

2

I'd like to know if it's a good practice to create a relationship between two collections in MongoDB.

Suppose I have a collection for users, with a scheme something 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 groups in the collection "groups".

Is this a good practice or should I use SQL for this? If it's good practice, how can I create this kind of thing in MongoDB?

    
asked by anonymous 16.03.2018 / 21:13

1 answer

2
  

Is this a good practice or should I use SQL for this?

Yes, this practice (referential ID storage on object) is common in implementations that use MongoDB .

  

If it's a good practice, how can I create this kind of thing in MongoDB?

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

    
16.03.2018 / 21:19