Mongoid Capped Collection

412 Views Asked by At

I'm trying to create a capped collection with Mongoid. I have a definition as follows:

class Customer
  include Mongoid::Document
  store_in(collection: 'customers')

  field: n, type: String, as: :name
  field: a, type: String, as: :address
  field: z, type: String, as: :zip
end

I've been referencing the documentation but can't figure out how to make a capped collection in this portion of the code. I've tried removing the store_in line and replacing it with session.command(create: "customers", capped: true, size: 10000000, max: 1000) to no avail. Is session supposed to be replaced with something? Or am I going about this incorrectly?

2

There are 2 best solutions below

0
On

Mongoid does not provide a mechanism for creating capped collections on the fly - you will need to create these yourself via the Mongo console.

0
On

Resurrecting this as it's the first Google result for "Mongoid capped collection".

Mongoid now offers the ability to specify a capped collection: documentation

class Name
  include Mongoid::Document
  store_in collection_options: {
    capped: true,
    size: 1024 # in bytes
  }
end