jackyesind
jackyesind

Reputation: 3373

Mongo Db schema

I have to save the different profiles of a user from various social media.for example user may have 1 facebook and 2 twitter profile. if i save the each profile it is inserted as a new document in different collections like facebook and twitter is a collections and how would i relate this document to another. I used dot notation in mongodb. if i relate the facebook and twitter profile of a same user means it is easy to fetch the record.How would i design the schema ? and also how would i avoid duplication? Is any possible to use the facebook id twitter id as unique? I am new to mongoDB. How would i join the documents from different collections

Upvotes: 0

Views: 122

Answers (1)

davissp14
davissp14

Reputation: 775

This is a ruby implementation, but hoping your able to translate the schema logic into your java code.

 class User
    has_one :facebook_profile
    has_one :twitter_profile
 end

 class TwitterProfile
    belongs_to :user 
 end

 class FacebookProfile
   belongs_to :user
 end

You can enforce unique indexes on the collections like so:

 db.twitter_profiles.ensureIndex({user_id: 1 }, {unique: true})
 db.facebook_profiles.ensureIndex({user_id: 1}, {unique: true})

You can use whatever field you want to enforce uniqueness at the user level.

Hope that helps.

Upvotes: 1

Related Questions