开发者

How to make MongoDB query with $or

开发者 https://www.devze.com 2023-02-09 16:43 出处:网络
I\'m trying to return a user from a MongoDB collection that either ma开发者_C百科tches the supplied username OR email:

I'm trying to return a user from a MongoDB collection that either ma开发者_C百科tches the supplied username OR email:

db.users.find({
    $or:[
        {username: "user1"},
        {email: "mail@mail.com"}
    ]
});

This query returns an empty result even though

db.users.find({username: "user1"});

or

db.users.find({email: "mail@mail.com"});

Returns the same object, how do I format the query to make it optional to supply the username or email?


Or operator become with 1.5.3 version of mongodb.

From documentation:

$or

The $or operator lets you use a boolean or expression to do queries. You give $or a list of expressions, any of which can satisfy the query. New in MongoDB 1.5.3

So problem was in old version of mongodb.


Version 1.2.2 does not seem to fully support $or, version 1.6.5 does.

0

精彩评论

暂无评论...
验证码 换一张
取 消