Re: [mongoid] Mongoid saving null for keys with no values

700 views
Skip to first unread message

Durran Jordan

unread,
Jul 26, 2012, 1:33:09 PM7/26/12
to mon...@googlegroups.com
I'm not sure exactly what you mean here - Mongoid will not store any nil values unless you *explicitly* set them to nil. Are you simply just referring to it's inspect method which will show all the fields you defined with nil values, but the documents stored in the database actually have nothing for these fields?

I'll need a more concrete example, but check what's actually in the database and see first, then I need a way to see your issue if they actually have nils.

2012/7/26 Ben Petro <benp...@gmail.com>
I'm new to Mongo and Mongoid, but I'm currently dealing with an issue of many null values being created in my documents for keys that have no values. To me, this seems to completely contradict what a schema-less database is all about.

Eg: I have a 'model' for Products.  My products cover all sorts of apparel: shoes, tshirts, hats etc.  Now obviously these 'objects' are very different in real life and thus, I want to store different key/values for them, however they ARE all products.  Ie, my tshirts have a collar style, but my hats have a brim style. This is just to name one of the many differences between these objects.

Now, in my rails app and in my Mongoid models, I have to specify all of these keys, which is fine. The problem is if I try to save a hat, my document is created with a null value for collar style and vice versa.

See what I mean?  What's the point of using a schema-less database if it basically acts exactly like a relational database in that every document has exactly the same schema, albeit with a bunch of null values?

Is there anyway to save a Mongoid document without these null values?  In Mongomapper you can by simply specifying the key name in the model, and omitting the key type. Save a document in Mongomapper, and ONLY the keys you send to the save are actually created in the document.  

I've looked around and only found one Stackoverflow article that did not solve the problem, which leads me to believe people are using Mongoid in a very wrong way and not getting any benefit from MongoDB.

Durran Jordan

unread,
Jul 26, 2012, 1:37:46 PM7/26/12
to mon...@googlegroups.com
A quick example of what I mean:

class Example
  include Mongoid::Document
  field :one, type: String
  field :two, type: String
  field :three, type: String
  field :four, type: String
end

example = Example.new
p example
#<Example _id: 50118004dbf89ba16a000001, _type: nil, one: nil, two: nil, three: nil, four: nil> # Looks like they are nil, but they actually don't exist.

example.save
  MOPED: 127.0.0.1:27017 INSERT       database=mongoid_sandbox collection=examples documents=[{"_id"=>"50118004dbf89ba16a000001"}] flags=[] (0.1192ms)

You can see what is actually inserted is the id, nothing else.

Ben Petro

unread,
Jul 26, 2012, 4:59:46 PM7/26/12
to mon...@googlegroups.com
Aha! Sorry Durran, you are correct. I was examining the JSON response from Mongoid and made the assumption that the nils were being stored In the database. My mistake. All is good now, thanks for correcting me.

Durran Jordan

unread,
Jul 26, 2012, 8:56:45 PM7/26/12
to mon...@googlegroups.com
No worries, it's a common misconception. :)

2012/7/26 Ben Petro <benp...@gmail.com>
Reply all
Reply to author
Forward
0 new messages