Help setting Foreign Key and Relationship using MongoDB


(Faizul Hassan) #1

I want user to update their profile with additional information.

I just want to map the user to the specified collection.

Is there a way in MGO (or any other driver) to set relationship/foreign key for the table ?

thank you !

Sample existing application:

user name
password

I want to add Address, Contact Number, Age, ■■■, DOB details to the current user.

How can I achieve this using MGO or any other driver that could help achieve this ?

Have achieved the same using Ruby on Rails with Mongo as a DB but could not get this done using Go Language.

Kindly suggest , thank you :slight_smile:


(Curtis Allyn Green) #2

MongoDB being NoSQL is a little different than the SQL method in how you can accomplish this: https://medium.com/@dis_is_patrick/mongodb-relations-26201385b919

Couple of choices there, references or embedding. But if you want to know more about references in mongo using mgo you’ll need the DBRef type: https://github.com/go-mgo/mgo/blob/v2/session.go#L3295

Here are the mongo docs pages on the subject of references: https://docs.mongodb.com/manual/reference/database-references/


(Faizul Hassan) #3

thank you so much @CurtGreen :slight_smile:

Now I am slightly clear about setting the reference. Will try those :slight_smile:

Thanks again !


(system) #4

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.