Solved thread

This post is marked as solved. If you think the information contained on this thread must be part of the official documentation, please contribute submitting a pull request to its repository.

How to use conditions in ODM (MongoDB)

Hi,

I would like to retrieve records which has enddate 'lesser' than "today". Below is what i come out with, but does not work. Any advice appreciated.

$records = Records::find(array(
        '$lte' => array(
            array( 'endDateTime' =>  date())
        ),
        "limit" => 10,
        "sort"  => array("_id" => -1)
    ));


4.3k
Accepted
answer
edited Jul '15

I haven't used collections in Phalcon however you should be able to do something like this:

    $records = Records::find([
        [
            'endDateTime' => [
                '$lte' => new Mongodate(strtotime("now"))
            ],
        ]
        'limit' => 10,
        'sort'  => [
            "_id" => -1
        ]
    ]);


13.0k

Thanks alot! I managed to get it working too!

I haven't used collections in Phalcon however you should be able to do something like this:

```php $records = Records::find([ [ 'endDateTime' => [ '$lte' => new Mongodate(strtotime("now")) ], ] 'limit' => 10, 'sort' => [ "_id" => -1 ] ]); ```



13.0k

For benefit of others, here is my working code. I have also include a OR condition.

 $products = Products::find(array(
        array(
            '$or' => array(
                        array('endDateTime' => ''),
                        array('endDateTime' => array('$gt' => new Mongodate(strtotime("now"))))
            )               
        ),
        "sort"  => array("_id" => -1)
    ));