Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Convert string to Mongo ObjectID in Javascript (Meteor)

Tags:

mongodb

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?

like image 331
JoeTidee Avatar asked May 07 '26 19:05

JoeTidee


1 Answers

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);
like image 117
JoeTidee Avatar answered May 09 '26 13:05

JoeTidee