We are moving our forum in GitHub Discussions. For questions about Phalcon v3/v4 you can visit here and for Phalcon v5 here.

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)
    ));


5.7k
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
        ]
    ]);


27.7k

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:

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


27.7k

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)
    ));