Created
April 22, 2015 16:08
-
-
Save Miniwe/18a62b00f3cd94dee993 to your computer and use it in GitHub Desktop.
Mongoose find by _id
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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