Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

mongo store document meta data

Tags:

mongodb

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
}
like image 820
Ryan Schumacher Avatar asked Apr 21 '26 02:04

Ryan Schumacher


1 Answers

I would store meta data with document for few reasons:

  1. It will look more natural if you keep the document and its metadata together.

  2. 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 } );

  3. 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:

  1. The size limit of a document is 16MB. If you're planning to have a lot of metadata, then better go with a separate collection. But I usually start from embedding the data, and later migrate it to a separate collection if it becomes too big.

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.

like image 74
Andrew Orsich Avatar answered Apr 22 '26 20:04

Andrew Orsich



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!