Techioz Blog

モンゴイド キャップ コレクション

概要

Mongoid でキャップ付きコレクションを作成しようとしています。私は次のような定義を持っています。

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

ドキュメントを参照していますが、コードのこの部分で上限付きコレクションを作成する方法がわかりません。 store_in 行を削除して session.command(create: “customers”, capped: true, size: 10000000, max: 1000) に置き換えてみましたが、無駄でした。セッションは何かに置き換えることになっていますか?それとも私のやり方が間違っているのでしょうか?

解決策

「Mongoid capped collection」の最初の Google 結果であるため、これを復活させます。

Mongoid は、キャップ付きコレクションを指定する機能を提供するようになりました: ドキュメント

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