MongoDB equivalent of SQL “OR”

2019-03-31 05:55发布

So, MongoDB defaults to "AND" when finding records. For example:

db.users.find({age: {'$gte': 30}, {'$lte': 40}});

The above query finds users >= 30 AND <= 40 years old.

How would I find users <= 30 OR >= 40 years old?

3条回答
迷人小祖宗
2楼-- · 2019-03-31 06:25

There is no OR operator, but they say you can still do it:

http://www.mongodb.org/display/DOCS/OR+operations+in+query+expressions

查看更多
Emotional °昔
3楼-- · 2019-03-31 06:27

matt -i too researched the $or business and hit a brick wall. i had saved this little link on googlegroups and was going to follow up on it at some point:

http://groups.google.com/group/mongodb-user/browse_thread/thread/4ac6f67622b3b951

sorry to not have answered the question but just wanted to make you aware that others were looking for the same functionailty at the time i was tentatively looking at this.

you could of course (depending on the language implementation) use the linq provider for mongo.

jim

[edit] for linq stuff see - http://schotime.net/blog/index.php/2010/04/29/nosql-norm-mongodb-and-regular-expressions/

查看更多
Ridiculous、
4楼-- · 2019-03-31 06:44

There will be an or operator in the near future. The nightly Mongodb build has already an $or. You can also use $where and use JavaScript to express the or.

See http://groups.google.com/group/mongodb-user/browse_thread/thread/a9a4d8b863d84601

查看更多
登录 后发表回答