In my MongoDb I've got documents into my collection as such:
array (
'_id' => new MongoId("561f925017e74f6a08012c65"),
'timestamp' => new MongoDate(1444877100, 0),
'value' => 0,
)
Of course all documents with their own timestamp.
How can I find the documents with multiple OR-conditions on the timestamp? For example find all records that have a timestamp between 1444877100 and 1444877105 OR timestamp between 1444877120 and 1444877150.
For example, in Mysql it'd be:
WHERE (
(timestamp >= 144877100 AND timestamp <= 1444877105) OR
(timestamp >= 1444877120 AND timestamp <= 1444877150)
)
I can't seem to get it right or find any help in the right direction. I have checked this question as well (MongoDB - Multiple $or operations) but that uses set values and I can't figure out how to do it with $gte and $lte (at least I think that's the way to go).
Can anybody help me? Thanks!
What you want is the $or
operator which perform a logical OR operation of two or more expressions. Of cause the $gte
and $lte
respectively selects the documents where the value of the field is greater than or equal to (i.e. >=) a specified value and less than or equal to (i.e. <=) the specified value.
$result = $collection->find(['$or' => [
['timestamp' => ['$gte' => new MongoDate(1444877100), '$lte' => new MongoDate(1444877105)]],
['timestamp' => ['$gte' => new MongoDate(1444877120), '$lte' => new MongoDate(1444877150)]]
]]);
Since the find()
returns a cursor you will need to loop through each element using the foreach loop. Something like this:
foreach ( $result as $doc ) { /* dosomething(); */ }
Collected from the Internet
Please contact [email protected] to delete if infringement.
Comments