Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

mongodb queries both with AND and OR

Tags:

mongodb

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

like image 297
user1171632 Avatar asked Jun 25 '12 19:06

user1171632


People also ask

How do I test multiple conditions in MongoDB?

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.

How do I join two queries in MongoDB?

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.

How do I query multiple values in MongoDB?

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.

What is the $or operator in MongoDB?

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.

How do I query for multiple criteria in MongoDB?

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.

How do I use $or clauses in a MongoDB query?

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.

How to combine and&or in MongoDB?

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 −


2 Answers

 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

    • first_name
    • last_name
  • Phone Number.

Note: Upgrade to latest version of MongoDB, if this doesn't work.

like image 82
its4zahoor Avatar answered Sep 23 '22 01:09

its4zahoor


Shorter to use an implicit AND operation:

db.things.find({     $or : [          {"first_name": "john"},         {"last_name": "john"}     ],     "phone": "12345678"          }) 
like image 36
TechWisdom Avatar answered Sep 19 '22 01:09

TechWisdom