Search code examples
mongodbmongodb-querydata-collection

MongoDB Sharding Key with DBRef field?


I have a collection that contains DBRefs to a root document. I have tens of thousands documents that link to a single root document in my Data collection. That's why I chose to not nest the data of the root document.

Data Collection:
    _id : Auto-Created ID
    ts : TimeStamp 
    field_1 : Whatever
    ...
    field_n : Whatever 
    root : DBRef

"Root" Document Collection:
    _id: Filename (unique)
    field_1 : Whatever
    ...
    field_n : Whatever 

I think the best way to shard my collection is using the fields ts and root._id as a compound key.

Is that possible? If not, what's the best solution? Simply copy the root._id field into a normal field in the Data collection?


Solution

  • To answer my own question, here's what I found out:

    Apparently, this is not possible because all DBRefs have to be retrieved on the client side in a subsequent query.

    I have ingested a new field sh_key that gets created on-the-fly while importing the data on the application side. The new field will hold the value of the "compound sharding key".

    The value is a concatenation of the root._id value as well as the hour of the ts in the fashion of {root._id}:{hour of ts}.