By default, mongoid has the ability to delete empty fields. If you mongoid some fields are empty, mongoid will delete them when pasting.
in the example below i skipped fields & rect
class User include Mongoid::Document field :key, type: String field :element, type: String field :rect, type: Array embeds_one :home end >> u=User.new(:key=>'ram').to_json => "{"_id":"4f1c3722b356f82e4a000001","_type":"key":"ram"}"
and it works great. But if you put a zero value in the field
>> u=User.new(:key=>'ram',:element=>nil).to_json => "{"_id":"4f1c3722b356f82e4a000001","_type":"User","key":"ram","element":null}"
This is inserted. I assume that it is the problem in your code. This way you can get around this by converting the JSON hash representation using as_json and removing the nil fields
x=u.as_json.reject! {|k,v| v.nil?} => "{"_id":"4f1c3722b356f82e4a000001","_type":"User","key":"ram"}"
But to go to the inner levels, you cannot use as_json . check the code below
>>h=Home.new(:address=>'xxxx',:dummy=>nil) >>u.home = h >>x=u.as_json.reject! {|k,v| v.nil?} =>{"_id"=>BSON::ObjectId('4f1c39b4b356f82e4a000003'), "_type"=>"User","key":"ram","home"=>#<Home _id: 4f1c3c5db356f82e4a000004,address:'xxxx' , dummy: nil >}
Now you see that the dummy inside the built-in document house is still with zero. so my best advice is: do not put zero values ββin dB at all . To do this, before_save for your models (also built-in) and delete the empty fields.
I will also show you how to remove nil fields from nested objects. Use it if there is no other way
We can use the attributes the mongoid model to get a hash representation of the object, including nested levels
x=u.attributes => {"_id"=>BSON::ObjectId4f1c39b4b356f82e4a000003,"key"=>"ram","element"=>nil,"home"=>{"address"=>"xxxx", "_id"=>BSON::ObjectId4f1c3c5db356f82e4a000004,"dummy"=>nil}}
and you must find out if there is a hash inside the mongoid object, if it is, we must use reject! {|k,v| v.nil?} reject! {|k,v| v.nil?} reject! {|k,v| v.nil?} reject! {|k,v| v.nil?} reject! {|k,v| v.nil?} reject! {|k,v| v.nil?} reject! {|k,v| v.nil?} reject! {|k,v| v.nil?} reject! {|k,v| v.nil?} to this hash too
collect everything
def to_json(obj) obj.reject! {|k,v| v.nil?} obj.find_all {|x| x[1].class==BSON::OrderedHash}.each do |arr| obj[arr[0]] = to_json(arr[1]) end obj end
and call it with the attributes of the model,
>> to_json u.attributes => {"_id"=>BSON::ObjectId4f1c39b4b356f82e4a000003,"key"=>"ram","home"=>{"address"=>"xxxx", "_id"=>BSON::ObjectId4f1c3c5db356f82e4a000004}}
It's all. Hope help