Skip to content

Instantly share code, notes, and snippets.

@ofirski
Last active September 15, 2016 20:36
Show Gist options
  • Save ofirski/c20a92254409780472f4 to your computer and use it in GitHub Desktop.
Save ofirski/c20a92254409780472f4 to your computer and use it in GitHub Desktop.
Add findOneById to MongoShell's DB collection
/****************************************************************************
* Add this snippet to ~/.mongorc in order to load it to any shell initiated
* Usage:
* db.myCollection.findOneById("5149cdbb62d5cbb22c000024")
* db.myCollection.findOneById("5149cdbb62d5cbb22c000024",{"field":1})
*****************************************************************************/
DBCollection.prototype.findOneById = function(objId ,fields, options ){
return this.findOne({"_id":ObjectId(objId)}, fields, options);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment