Skip to content

Instantly share code, notes, and snippets.

@Miniwe
Created April 22, 2015 16:08
Show Gist options
  • Save Miniwe/18a62b00f3cd94dee993 to your computer and use it in GitHub Desktop.
Save Miniwe/18a62b00f3cd94dee993 to your computer and use it in GitHub Desktop.
Mongoose find by _id
var ObjectId = require('mongoose').Types.ObjectId;
var objId = new ObjectId( (param.length < 12) ? "123456789012" : param );
// You should make string 'param' as ObjectId type. To avoid exception,
// the 'param' must consist of more than 12 characters.
User.find( { $or:[ {'_id':objId}, {'name':param}, {'nickname':param} ]},
function(err,docs){
if(!err) res.send(docs);
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment