Sha256: bb63d354e4d8e350b8736aca10dccaafca39165d390b8ba8da0a38b72f048019

Contents?: true

Size: 776 Bytes

Versions: 2

Compression:

Stored size: 776 Bytes

Contents

class ShardedModel
  include MongoMapper::Document

  key :first_name, String
  key :last_name, String
  shard_key :first_name
end

if ENV.fetch("ENABLE_SHARDING", "0") == "1"
  client = MongoMapper.connection
  database = MongoMapper.database

  # https://www.mongodb.com/docs/manual/reference/command/enableSharding/#mongodb-dbcommand-dbcmd.enableSharding
  client.use(:admin).command(enableSharding: database.name)

  # https://www.mongodb.com/docs/manual/reference/command/shardCollection/#mongodb-dbcommand-dbcmd.shardCollection
  # Note: this command automatically creates the index for the empty collection
  client.use(:admin).command(
    shardCollection: [database.name, ShardedModel.collection.name].join("."),
    key: {
      first_name: "hashed",
    },
  )
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
mongo_mapper-0.16.0 spec/support/sharded_model.rb
mongo_mapper-0.15.6 spec/support/sharded_model.rb