The following works for me. I suggest that you recreate it and see where it differs from your code.
model/app/claim.rb
class Claim
include Mongoid::Document
field :paid_on, type: Time
field :paid_amt, type: Float
end
test/unit/claim_test.rb
require 'test_helper'
require 'pp'
class ClaimTest < ActiveSupport::TestCase
def setup
Claim.delete_all
end
test "replace null date field" do
docs = [
{
:paid_on => DateTime.parse("2013-03-08T22:25:24.973Z"),
:paid_amt => 25.5
},
{
:paid_on => nil,
:paid_amt => 12.25
}
]
Claim.create(docs)
pipeline = [
{
"$project" => {
"paid_on" => {"$ifNull" => ["$paid_on", Date.new(1980, 1, 1).mongoize]},
"paid_amt" => 1
}
}
]
puts "\nMongoid::VERSION:#{Mongoid::VERSION}\nMoped::VERSION:#{Moped::VERSION}"
puts "pipeline:#{pipeline.inspect}"
pp Claim.collection.aggregate(pipeline)
end
end
$ rake test
Run options:
# Running tests:
[1/1] ClaimTest#test_replace_null_date_field
Mongoid::VERSION:3.1.5
Moped::VERSION:1.5.1
pipeline:[{"$project"=>{"paid_on"=>{"$ifNull"=>["$paid_on", 1980-01-01 00:00:00 UTC]}, "paid_amt"=>1}}]
[{"_id"=>"5272720a7f11ba4293000001",
"paid_on"=>2013-03-08 22:25:24 UTC,
"paid_amt"=>25.5},
{"_id"=>"5272720a7f11ba4293000002",
"paid_on"=>1980-01-01 00:00:00 UTC,
"paid_amt"=>12.25}]
Finished tests in 0.039125s, 25.5591 tests/s, 0.0000 assertions/s.
1 tests, 0 assertions, 0 failures, 0 errors, 0 skips