JoeTidee
JoeTidee

Reputation: 26034

Convert string to Mongo ObjectID in Javascript (Meteor)

I have a Meteor application whereby I initially use the _id field from each record in my collection when naming list items in my template.

When get the _id field, I convert it to a string to use in the template.

Now I want to update these records in Mongo and am passing the _id back to a Meteor.method, but these are still in string format and Mongo is expecting an ObjectID(). Is there a simple way to convert this string to the ObjectID()? If not, what alternatives do I have?

Upvotes: 3

Views: 3128

Answers (1)

JoeTidee
JoeTidee

Reputation: 26034

Ok, found it! On the /server, within your Meteor method function do this to convert it:

var mid = new Mongo.ObjectID(str_id_sent_to_server);

Upvotes: 9

Related Questions