Skip to content

Instantly share code, notes, and snippets.

@cianclarke
Created October 26, 2012 19:49
Show Gist options
  • Save cianclarke/3961040 to your computer and use it in GitHub Desktop.
Save cianclarke/3961040 to your computer and use it in GitHub Desktop.
MongoDB Check for BSON _id based lookups
function _checkForBSONId(queryObject){
if (queryObject.hasOwnProperty('_id')){
// If we're keying on an autogenned one, MongoDB expects an objectId to look like: ObjectId( "505cfee6d94d451252000001" ) not just ""
if (queryObject._id.length === 24){
var BSON = require('mongodb').BSONPure;
queryObject._id = BSON.ObjectID.createFromHexString(queryObject._id);
}
}
return queryObject;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment