Mongo and Node.js: Finding a document by _id using a UUID (GUID)

这一生的挚爱 提交于 2019-12-06 04:18:52

Ok i've found the solution to get the base64 string into a GUID format within node, to convert it this needs to be done:

var mongo.require('mongodb');
var GUID = new mongo.Binary(new Buffer(<base65String>, 'base64'), 3);

and now i can query the database like this:

collection.findOne({'_id' : GUID}, function(err, item) {
                    res.send(item);
                });

You will have to convert the id in your GET method to a BinData() object just like it is shown in your sample document.

In your current code it searches for _id matching a string (MH+t3q6PD0SxVR5z7/pzfw== in your sample).

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