对 MongoDB 结果进行排序

发布于 2024-11-30 00:33:24 字数 1649 浏览 0 评论 0原文

我在 MongoDB 中有以下结构(是一个示例结构):

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

我想获取按created_at降序排序的最后 10 条评论,我写了以下内容:(

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

这是驱动 MongoDB 的 PHP 代码)

任何人都可以帮我做同样的事情,但是还显示按照created_at降序排列的最后3条评论?

这可以在单个查询中实现吗?你能帮忙写一下代码吗?

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

七分※倦醒 2024-12-07 00:33:24

我对 php 不太了解,无法理解您所描述的文档结构。因此,我假设 comment 文档“嵌入”在您的 usuario 文档中。

如果是这样,那么您就无法直接处理 comment 文档。您将首先获取一个 usuario 文档。这样,您将获得一组 comment 文档,您必须在客户端对这些文档进行排序和过滤。

或者,如果您的评论对象以特定顺序存储,您可以使用 $slice 来获取前 3 个或前 10 个或任何此类子集。更多信息:http://www.mongodb.org/display/DOCS/Retriving+a+Subset+of+Fields#RetrivingaSubsetofFields-RetrivingaSubrangeofArrayElements

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

箜明 2024-12-07 00:33:24

我想获取按created_at降序排序的最后10条评论,我写了以下内容:

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

该查询返回最后 10 个 usarios(用户)及其所有评论:

要获取最后 3 条评论,您必须按 ' 排序commentarios.created_at' 然后过滤这些结果。像这样:

$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'];
}

请注意,find 返回 3 个用户及其评论,因此您必须找到这些用户之间最近的 3 个用户。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文