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 deal with embedded document of mongodb ?

I have an array in an embedded document and I want to sort this array by one of it's element, can I implement this in a way like the ODM in phalcon? for example, can I sort it in this way?

find(array(
    'conditions' => array(
        'time' => array($compare => $time)),
    'sort' => array('time' => -1),
    'limit' => $count
));

image below is my document in mongodb, and I want to sort the elements of array called document by $time, can phalcon implement this? I know mongodb itself can, but I don't know how to do this in phalcon. Thanks!



82.7k
Accepted
answer

Yes, it must work this way:

MyModel::find(array(
    'conditions' => array(
        'time' => array($compare => $time)),
    'sort' => array('time' => -1),
    'limit' => $count
));

http://docs.phalconphp.com/en/latest/reference/odm.html#finding-documents



1.4k

thank you for helping , and what I want is : sort the array in the red circle by time, and this array is in an inner document, so I think this way in phalcon's document can't work. anyway, I can't get my work done. So do you have any other suggestion or where I was wrong?



82.7k

Try:

MyModel::find(array(
    'conditions' => array(
        'time' => array($compare => $time)),
    'sort' => array('bookmark.document.time' => -1),
    'limit' => $count
));