I have two collections:
- Users
- Uploads
Each upload has a User
associated with it and I need to know their details when an Upload
is viewed. Is it best practice to duplicate this data inside the the Uploads record, or use populate() to pull in these details from the Users collection referenced by _id
?
OPTION 1
var UploadSchema = new Schema({
_id: { type: Schema.ObjectId },
_user: { type: Schema.ObjectId, ref: 'users'},
title: { type: String },
});
OPTION 2
var UploadSchema = new Schema({
_id: { type: Schema.ObjectId },
user: {
name: { type: String },
email: { type: String },
avatar: { type: String },
//...etc
},
title: { type: String },
});
With 'Option 2' if any of the data in the Users
collection changes I will have to update this across all associated Upload
records. With 'Option 1' on the other hand I can just chill out and let populate()
ensure the latest User data is always shown.
Is the overhead of using populate()
significant? What is the best practice in this common scenario?