Reputation: 1886
Which is the best way to store meta data for a document, in the document itself or in separate collection?
I am working with a collection that has data like this:
{
status: {
joined: Date,
retired: [{
on: Date,
comment: String,
reinstated: {
on: Date,
comment: String
}
}],
suspended: [{
on: Date,
comment: String,
reinstated: {
on: Date,
comment: String
}
}],
//.....
I need to keep a log of when and who performs these changes, but I am not sure if I should add the metadata to each element or have a collection like Log.
// Log collection
{
by: UserId,
on: Date,
verb: String,
object: ObjectId,
comment: String
}
Upvotes: 3
Views: 4077
Reputation: 53685
I would store meta data with document for few reasons:
It will look more natural if you keep the document and its metadata together.
When you load document you can easily exclude metadata from the document to keep your documents light when metadata is not needed:
db.items.find( { }, { metadata : 0 } );
You can easily use paging to retrieve metadata using $slice
:
db.items.find({}, {metadata:{$slice: [20, 10]}}) // skip 20, at most 10
But, also keep in mind that:
2 .If you need to show the metadata across all documents somewhere, like in a grid, it can be a pain to load it, apply paging, etc.
Upvotes: 2