Lee
Lee

Reputation: 2992

Unable to assign a random string to _id field.

I am trying to save a document into mongodb using MONK drive.

My object has a field called myid, which is a string of unknown length. I'm very inclined to use this ID as _id of my document but after I assign the value to _id field and save the document, I receive error as below:

Error: Argument passed in must be a single String of 12 bytes or a string of 24 
hex characters

Is this some restriction introduced by MONK or MongoDB driver? Is there anyway to workaround this? In mongodb shell, you can use any value as _id;

Upvotes: 2

Views: 303

Answers (1)

flaudre
flaudre

Reputation: 2298

It is an error thrown by monk. You can override the id function:

collectionName.id = function(str){return str;};

See: GitHub issue

Upvotes: 1

Related Questions