Question

Using Rails 3.2.14, Ruby 1.9.3, Mongoid 3.1.5:

I'm querying over documents that look like this:

{
   paid_on: ISODate("2013-03-08T22:25:24.973Z"),
   paid_amt: 25.5
}

Sometimes paid_on is null, but I need to capture those documents as well, so I was attempting to use $ifNull to substitute a default date:

Claim.collection.aggregate(
    {
        "$project" => {
            "paid_on"  => {"$ifNull" => ["$paid_on", Date.new(1980, 1, 1).mongoize]},
            "paid_amt" => 1,
        }
    }
)

The query sent to Mongo looks like this (just captured the $project part):

{
"$project"=>{
     "paid_on" => {"$ifNull"=>["$paid_on", 1980-01-01 00:00:00 UTC]},
     "paid_amt" => 1
 }

and fails with:

failed with error 16006: "exception: can't convert from BSON type String to Date"

I've tried several other ways to send the date with no luck:

  • 'ISODate("1980-01-01")'
  • { "$date" => 315554400000 }
Was it helpful?

Solution

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
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top