MongoDB equivalent of SQL “OR”

2019-03-31 05:44发布

问题:

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?

回答1:

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



回答2:

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

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



回答3:

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/