Serializing and deserializing document

Skip to first unread message


Sep 15, 2014, 9:37:32 AM9/15/14
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 = )! 

# 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 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?


Reply all
Reply to author
0 new messages