can I use combination of OR and AND in mongodb queries?
the code below doesn't work as expected
db.things.find({ $and:[ {$or:[ {"first_name" : "john"}, {"last_name" : "john"} ]}, {"phone": "12345678"} ]});
database content:
> db.things.find(); { "_id" : ObjectId("4fe8734ac27bc8be56947d60"), "first_name" : "john", "last_name" : "hersh", "phone" : "2222" } { "_id" : ObjectId("4fe8736dc27bc8be56947d61"), "first_name" : "john", "last_name" : "hersh", "phone" : "12345678" } { "_id" : ObjectId("4fe8737ec27bc8be56947d62"), "first_name" : "elton", "last_name" : "john", "phone" : "12345678" } { "_id" : ObjectId("4fe8738ac27bc8be56947d63"), "first_name" : "eltonush", "last_name" : "john", "phone" : "5555" }
when querying the above query - i get nothing!
> db.things.find({$and:[{$or:[{"first_name" : "john"}, {"last_name" : "john"}]},{"phone": "12345678"}]}); >
I'm using mongo 1.8.3
Find Multiple Conditions Using the $or Operator The or operator in MongoDB is used to select or retrieve only documents that match at least one of the provided phrases. You can also use this operator in a method like a find() , update() , etc., as per the requirements.
MongoDB is not a relational database, but you can perform a left outer join by using the $lookup stage. The $lookup stage lets you specify which collection you want to join with the current collection, and which fields that should match.
MongoDB provides the find() that is used to find multiple values or documents from the collection. The find() method returns a cursor of the result set and prints all the documents. To find the multiple values, we can use the aggregation operations that are provided by MongoDB itself.
MongoDB provides the user with different types of logical query operators, and the $or operator is one of them. This operator performs logical OR operations on the array of two or more expressions and selects or retrieves only those documents that match at least one of the given expressions in the array.
You can use the $and operator in MongoDB to query for documents that meet multiple criteria. This particular example finds all documents in the collection titled myCollection where field1 is equal to “hello” and field2 has a value greater than or equal to 10.
Consider the following query: To support this query, rather than a compound index, you would create one index on quantity and another index on price: MongoDB can use all but the geoHaystack index to support $or clauses. If $or includes a $text query, all clauses in the $or array must be supported by an index.
To combine AND & OR in MongoDB, let us first create a collection with documents − Following is the query to display all documents from a collection with the help of find () method −
db.things.find( { $and : [ { $or : [ {"first_name" : "john"}, {"last_name" : "john"} ] }, { "Phone":"12345678" } ] } )
AND takes an array of 2 expressions OR , phone.
OR takes an array of 2 expressions first_name , last_name.
AND
OR
Phone Number.
Note: Upgrade to latest version of MongoDB, if this doesn't work.
Shorter to use an implicit AND operation:
db.things.find({ $or : [ {"first_name": "john"}, {"last_name": "john"} ], "phone": "12345678" })
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With