※自分用の備忘録です。
Gemfile
gem 'mongoid'
gem 'mongoid_paranoia'
models/user.rb
class User
include Mongoid::Document
include Mongoid::Timestamps
include Mongoid::Paranoia
field :deleted_at, type: DateTime
end
pry
pry(main)> User.count
=> 1
User.first.destroy
=> true
User.count
=> 0
# モデル名+deletedで論理削除されたデータが全て表示される。
User.deleted.count
=> 1
User.deleted.first
=> #<User _id: 61b5d828746fc2954d25e61a, created_at: 2021-12-12 11:08:24.417 UTC, updated_at: 2021-12-12 11:43:42.249 UTC, deleted_at: 2021-12-12 11:43:34.534 UTC, email: "sample@gmail.com", password: "HKc7ZcfTAk2a5l8WmOGTLL9CMzIog4VsDoVgX5/YpsJBiC1XB9d25eIjY3Tj8gwWICZuYFo83stQE86StmxsywBsi0Y0Jb247DOs3S+iQpCdBQ==--43Y+l/g0UMhK9Eik--rjhdsO1gz+rBFg1+0eNy7Q==">
pry(main)> User.count
MONGODB | localhost:27017 req:279 conn:1:1 sconn:60 | event_book_development.aggregate | STARTED | {"aggregate"=>"users", "pipeline"=>[{"$match"=>{"deleted_at"=>nil}}, {"$group"=>{"_id"=>1, "n"=>{"$sum"=>1}}}], "cursor"=>{}, "$db"=>"event_book_development", "lsid"=>{"id"=><BSON::Binary:0x19900 type=uuid data=0x08c6bdf294194fac...>}}
MONGODB | localhost:27017 req:279 | event_book_development.aggregate | SUCCEEDED | 0.003s
=> 0