mosquito87
mosquito87

Reputation: 4440

Save object id or embedded document?

I have a Company schema:

var CompanyEntityModel = new Schema({
    name: String,
    street: String,
    zipCode: String,
    city: String,        
    members: [Useraccount]
});

And a Useraccount schema:

var UseraccountEntityModel = new Schema({
    firstName: String,
    lastName: String,
    email: String,
    password: String,
    companies: [Company],
});

In my scenario I save the useraccounts in a useraccount collection (inside the db) and as embedded document inside the company. So a company can have n useraccounts. I do the same with companies (they are saved inside a company collection and as embedded document inside a useraccount. A useraccount can have n companies).

I could also just save the objectIds to members (in Company schema) and to companies (in Useraccount schema).

What is better? If I stick to my solution and the company gets updated ... do I have to update the document in my companies collection and also all the companies which are embedded documents inside the useraccounts?

Upvotes: 1

Views: 931

Answers (1)

user3415653
user3415653

Reputation: 335

that might help you: http://docs.mongodb.org/manual/reference/database-references/

My preferred way is saving the object id into the second collection and perform on application side an second query to get related data.

Upvotes: 1

Related Questions