Uploaded image for project: 'Mongoid'
  1. Mongoid
  2. MONGOID-5582

Support shard keys based on subdocument fields

    • Type: Icon: Bug Bug
    • Resolution: Fixed
    • Priority: Icon: Minor - P4 Minor - P4
    • 8.1.0, 9.0.0, 8.0.4, 7.5.3
    • Affects Version/s: None
    • Component/s: None
    • None

       

      Currently, mongoid does not support `shard_key`s containing fields from subdocuments.

      Using a subdocument field as part of the shard key is explicitly supported by mongodb, but it is not supported by mongoid in any way that I can see. Because of this, I believe this is more of a bug than a missing feature, but I leave it to you to recategorize as you see fit.

      Given collection `inventory`

       

      db.inventory.insertMany( [
         { item: "journal", qty: 25, size: { h: 14, w: 21, uom: "cm" }, status: "A" },
         { item: "notebook", qty: 50, size: { h: 8.5, w: 11, uom: "in" }, status: "A" },
         { item: "paper", qty: 100, size: { h: 8.5, w: 11, uom: "in" }, status: "D" },
         { item: "planner", qty: 75, size: { h: 22.85, w: 30, uom: "cm" }, status: "D" },
         { item: "postcard", qty: 45, size: { h: 10, w: 15.25, uom: "cm" }, status: "A" }
      ]);

       

       

      I expect the following to succeed:

       

      class Inventory
          include Mongoid::Document
      
          shard_key({'size.h' => 1, 'size.w' => 1})
      end 

       

       

      However this fails because it does not know how to parse the subdocument out of the string. That is somewhat expected because fields can contain periods so this could just be a valid field on the top level document. However given the method signature of `shard_key` I don't see a valid way to specify a subdocument field in the shard key definition.

      I believe the main hinderance to this working are the shard_key_selector_in_db and shard_key_selector methods in Mongoid::Shardable. I was able to override these methods to get this working. However my solution is extremely hacky and does not allow for fields containing periods.

      I suspect the `shard_key` interface will need to change to properly support this behavior, given the allowance of periods in field names. This would likely have significant cascading downstream effects for the shard_key_fields method. I don't pretend to know the whole impact though.

            Assignee:
            jamis.buck@mongodb.com Jamis Buck
            Reporter:
            jeff.gandt@withpersona.com Jeff Gandt
            Votes:
            0 Vote for this issue
            Watchers:
            2 Start watching this issue

              Created:
              Updated:
              Resolved: