Convert string to Mongo ObjectID in Javascript (Meteor)

谁说我不能喝 提交于 2019-12-22 12:01:25

问题


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?


回答1:


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);


来源:https://stackoverflow.com/questions/29462623/convert-string-to-mongo-objectid-in-javascript-meteor

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!