开发者

Sorting MongoDB Results

I have the following structure in MongoDB (is an example structure):

Array
(
    [_id] => MongoId Object
        (
            [$id] => 4e465de048177e8105000003
        )

    [id_usuario] => MongoId Object
        (
            [$id] => 4e43b20648177e5305000000
        )

    [mensaje] => lero lero si?
    [created_at] => MongoDate Object
        (
            [sec] => 1313234400
            [usec] => 160000
        )
    [comentarios] => Array
        (
            [0] => Array
            (
                [comentario] => hola mundo
                [usuario] => [_id] => MongoId Object
                    (
                        [$id] => 4e465de048177e8105000003
                    )
                [created_at] => MongoDate Object
                (
                    [sec] => 1313234400
                    [usec] => 160000
                )
            )
            [1] => Array
            (
                [comentario] => hola mundo
                [usuario] => [_id] => MongoId Object
                    (
                        [$id] => 4e465de048177e8105000003
                    )
                [created_at] => MongoDate Object
                (
                    [sec] => 1313234400
                    [usec] => 160000
                )
            )
        )
)

I want to get the last 10 comments sorted by created_at in descending and I write the following:

$db->usuarios->find()->sort(array('created_at' => -1))->limit(10)->skip(0);

(this is PHP code driving MongoDB)

Could anyone help me to do the same but also display the last 3 comments ordered by created_at in descending?

Is开发者_开发问答 that possible in a single query? Can you help with code please?


I do not know php well enough to understand the document structure you have described. So I am assuming that the comment documents are "embedded" in your usuario documents.

If it is true, then you cannot address the comment documents directly. You will have first fetch a usuario document. With that you will get an array of comment documents which you will have to sort and filter on the client side.

Or if your comment objects are stored in a particular order you can use the $slice to fetch the first 3 or first 10 or any such subset. More info : http://www.mongodb.org/display/DOCS/Retrieving+a+Subset+of+Fields#RetrievingaSubsetofFields-RetrievingaSubrangeofArrayElements


I want to get the last 10 comments sorted by created_at in descending and I write the following:

$db->usuarios->find()->sort(array('created_at' => -1))->limit(10)->skip(0);

That query returns the last 10 usarios (users) and all of their comments:

To get the last 3 comments you have to sort by 'commentarios.created_at' and then filter those results. Like this:

$user = $db->usuarios->find()->sort(array('commentarios.created_at' => -1)->limit(3);
$comments = array_merge($user[0]['commentarios'], $user[1]['commentarios'], $user[2]['commentarios']
$ordered = uasort($comments, 'sorter')

function sorter($a, $b) {
  return $a['created_date'] < $b['created_date'];
}

Notice that find returns 3 users and their comments, so you have to find the 3 most recent between those users.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜