Mongodb multiple OR condition on same key

Jelmer Keij

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!

styvane

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.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

MongoDB - Join on multiple condition

From Dev

Multiple limit condition in mongodb

From Dev

MongoDB multiple condition in WHERE clause

From Dev

MongoDB query with condition on multiple records

From Dev

MongoDB multiple condition in WHERE clause

From Dev

Multiple values against same condition

From Dev

Perl 'or' condition to get hash value in the same key

From Dev

mongoDB `upsert` with multiple key values

From Dev

multiple key with same DKIM selector?

From Dev

Use same key for multiple computers

From Dev

Multiple objects listening for the same key

From Dev

SELECT multiple values to the same key in multiple tables

From Dev

Using "on duplicate key" with multiple duplicate condition

From Dev

distinct with multiple fields and with where condition in mongodb

From Dev

joint result for multiple condition on same column

From Dev

where clause with multiple condition on same table

From Dev

UPDATE multiple fields under same condition (IF or CASE)

From Dev

Slice string in multiple column with same condition

From Dev

How filter multiple column with the same condition?

From Dev

Slice string in multiple column with same condition

From Dev

How to apply multiple condition in same column in excel

From Dev

Multiple cells testing the same condition in an or statement

From Dev

If Condition is same then how to update multiple query in php

From Dev

Multiple ordering on same column with different condition

From Dev

Multiple ng-if on the same condition - performance wise

From Dev

MongoDB indexing for different datatypes of same key

From Dev

MongoDB MapReduce same key several times?

From Dev

MongoDB update with matching shard key and multiple=true

From Dev

Is it possible to key a single sort on multiple fields in MongoDB?

Related Related

  1. 1

    MongoDB - Join on multiple condition

  2. 2

    Multiple limit condition in mongodb

  3. 3

    MongoDB multiple condition in WHERE clause

  4. 4

    MongoDB query with condition on multiple records

  5. 5

    MongoDB multiple condition in WHERE clause

  6. 6

    Multiple values against same condition

  7. 7

    Perl 'or' condition to get hash value in the same key

  8. 8

    mongoDB `upsert` with multiple key values

  9. 9

    multiple key with same DKIM selector?

  10. 10

    Use same key for multiple computers

  11. 11

    Multiple objects listening for the same key

  12. 12

    SELECT multiple values to the same key in multiple tables

  13. 13

    Using "on duplicate key" with multiple duplicate condition

  14. 14

    distinct with multiple fields and with where condition in mongodb

  15. 15

    joint result for multiple condition on same column

  16. 16

    where clause with multiple condition on same table

  17. 17

    UPDATE multiple fields under same condition (IF or CASE)

  18. 18

    Slice string in multiple column with same condition

  19. 19

    How filter multiple column with the same condition?

  20. 20

    Slice string in multiple column with same condition

  21. 21

    How to apply multiple condition in same column in excel

  22. 22

    Multiple cells testing the same condition in an or statement

  23. 23

    If Condition is same then how to update multiple query in php

  24. 24

    Multiple ordering on same column with different condition

  25. 25

    Multiple ng-if on the same condition - performance wise

  26. 26

    MongoDB indexing for different datatypes of same key

  27. 27

    MongoDB MapReduce same key several times?

  28. 28

    MongoDB update with matching shard key and multiple=true

  29. 29

    Is it possible to key a single sort on multiple fields in MongoDB?

HotTag

Archive