Reputation: 7084
I've come across (and been stumped by) this error a couple times in Rails apps, when trying to save some data in a Hash field using MongoID:
'$oid' is an illegal key in MongoDB
I'm not sending any data with a key $oid
into the hash, so I can't figure out what is causing it.
Upvotes: 1
Views: 816
Reputation: 7084
It turns out the problem is when I use the id
field of another object in one of these hashes. The id field of MongoID objects are not strings, they are BSON::ObjectID
s, so if you try to stick them into a hash like this:
ObjectWithHash.update(hash_field: {name: a_name, id: other_object.id})
MongoId will try to convert other_object.id
to Hash format, and come up with {"$oid" => "......."}
, causing the error to show up.
To solve this, you can first convert it to a string and store that: other_object.id.to_s
Upvotes: 2