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


i'm developing restapi using node.js , i'm trying query mongo collection. can query using strings (for example "companyname") need able query "_id" element in document.

in mongo _id stored (as guid):

{ "_id" : new bindata(3, "mh+t3q6pd0sxvr5z7/pzfw=="),  "companyname" : "testcompany", "databasename" : "testdatabase", } 


current method looks like:

exports.getbusinesscarddata = function(req, res) {     var id = req.params.id;     //"id" = mh+t3q6pd0sxvr5z7/pzfw==      tradeshowadmindb.collection('clients', function(err, collection) {         collection.findone({'_id': id}, function(err, item) {             res.send(item);         });     }); }; 

the "id" coming method base64 string format ("mh+t3q6pd0sxvr5z7/pzfw==") , using query returns nothing (i'm assuming it's not correct type/format). question how "id" correct format can query mongo using _id?

i've been looking ages , can't seem find solution , documentation seems vague on isn't 24 character hex objectid. appreciated!

additional information:

i'm using node v0.10.33 , express v4.x

the mongo driver using base javascript mongo driver node.
(found here: http://docs.mongodb.org/ecosystem/drivers/node-js/)

ok i've found solution base64 string guid format within node, convert needs done:

var mongo.require('mongodb'); var guid = new mongo.binary(new buffer(<base65string>, 'base64'), 3); 

and can query database this:

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

Comments

Popular posts from this blog

java - Oracle EBS .ClassNotFoundException: oracle.apps.fnd.formsClient.FormsLauncher.class ERROR -

c# - how to use buttonedit in devexpress gridcontrol -

nvd3.js - angularjs-nvd3-directives setting color in legend as well as in chart elements -