Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

MongoDB multiple $and operators query in PHP

I’m having trouble with this MongoDB query using PHP array syntax. This is a direct version of the query I want to use.

db.collection.find({
        $or: [
            {$and : [{X:1}, {X: {$gt: 100}}]},
            {$and : [{X:2}, {X: {$lt: 100}}]}
        ]
});

Note: The real query is more complicated, this is just an example.

I wasn’t able to find some examples describing this kind of query in PHP. The best I’ve come up was this:

$query = array(
    '$or' => array(
        array(
            '$and' => array(
                array('X' => 1),
                array('X' => array('gt' => 100))
            )
        ),
        array(
            '$and' => array(
                array('X' => 2),
                array('X' => array('lt' => 100))
            )
        ),
    )
);

$this->db->collection->find($query);

But this query doesn't return any results. Obviously we can't remove $and from the array because we can't have duplicate keys in PHP array.

I don't want to use JavaScript expressions because the speed is critical.

UPDATE: As Alexander Azarov pointed out in comments, my original query can be written differently. I've updated the question with the properly used $and query.

like image 283
Christian P Avatar asked Jan 01 '12 17:01

Christian P


People also ask

How to query with multiple conditions using multiple documents in MongoDB?

Then, you can use the $and operator and retrieve the documents according to the condition. In this article, querying with multiple conditions using multiple documents in MongoDB is explained in detail using the $and and $or operator. DelftStack articles are written by software geeks like you.

What is the use of the or operator in MongoDB?

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. You can also use this operator with text queries and sort operations. You can also nest $or operation.

What is the query class in MongoDB?

The MongoDB\Driver\Query class is a value object that represents a database query. final public __construct ( array|object $filter, array $queryOptions = ?)

How do I use $or clauses in MongoDB?

Each clause of the $or query can utilize its index when employing indexes with $or queries. Look at the following example below. Rather than a compound index, you would establish one index for quantity and another for the price to handle this query. To enable the $or clauses, MongoDB can use any indexes except the geoHaystack index.


1 Answers

Here is an example of the code with or and and operators in PHP for a Mongo query.

It uses different field names from what you are going to find in your query, but it is straightforward to understand how to use it.

Right now I don't have time to change it, to match your query, but if you have problems with that, let me know and I will adjust it. This query is working and finding what it should find.

$arrFind = array(
    '$or' => array(
        array(
            '$and' => array(
                array(
                    UI_name => array(
                        '$regex' => 'andrew',
                        '$options' => 'i'
                    )
                ),
                array(
                    UI_surname => array(
                        '$regex' => 'mik',
                        '$options' => 'i'
                    )
                )
            )
        ),
        array(
            '$and' => array(
                array(
                    UI_surname => array(
                        '$regex' => 'andrew',
                        '$options' => 'i'
                    )
                ),
                array(
                    UI_name => array(
                        '$regex' => 'mik',
                        '$options' => 'i'
                    )
                )
            )
        ),
    )
);
like image 146
Salvador Dali Avatar answered Sep 18 '22 23:09

Salvador Dali