2014-09-13 40 views

回答

4
User.findOne({ 
    $or: [ 
     {first_name: name}, 
     {last_name: name}, 
    ], 
}, function(err, user) { 
    ... 
}) 

使用find如果你认为会有不止一个文件名匹配为等于或FIRST_NAME LAST_NAME。

+0

@Ranjith做了这项工作? – 2014-09-14 11:02:51

+0

文本索引和相应的搜索也可能起作用。 – 2014-09-14 21:32:21

+0

不知道它是否适用于Ranjith,但这种方法确实有效(它对我来说)。注意是否需要搜索多种类型(如String和ObjectId),因为如果您将ObjectId传递给不符合的字符串,您将会得到Cast Error。 – cjn 2015-08-17 17:46:07

相关问题