Serializing and deserializing document

38 views
Skip to first unread message

TheR

unread,
Sep 15, 2014, 9:37:32 AM9/15/14
to mon...@googlegroups.com
mongoid 4.0.0.

I am trying to copy selected records from one collection (development) to another (production) with help of clipboard ;-)

I am kinda stuck with creation of new document. To clear what I am trying to do here is some demo code:

doc = Model.find(params[:id]]
json = doc.as_document.to_json
render :text json

# on other action

doc = Model.new(ActiveSupport::JSON.decode(json_field_with_copied_above_text) )
doc.save! 

# dies with 

The operation: #<Moped::Protocol::Command @length=82 @request_id=3758 @response_to=0 @op_code=2004 @flags=[] @full_collection_name="model.$cmd" @skip=0 @limit=-1 @selector={:getlasterror=>1, :w=>1} @fields=nil> failed with error 2: "illegal object for _id" See https://github.com/mongodb/mongo/blob/master/docs/errors.md for details about this error.


I was pretty sure that new should create a new _id value. But it is not. print of doc_id returns BSON::ObjectId('5350dbf05874650292130000') in first case and {"$oid"=>BSON::ObjectId('5350dbf05874650292130000')} in second case. Which is probably the source of an error. Since types are not the same.

1. How do I tell mongoid to forget old document id and create a new id for new document?
2. Is this the proper way of doing this?


by
TheR


Reply all
Reply to author
Forward
0 new messages