【问题标题】:How to sort by nested object on mongodb via php?如何通过 php 按 mongodb 上的嵌套对象排序?
【发布时间】:2019-11-14 04:33:38
【问题描述】:

我需要有关如何根据嵌套文档对 mongodb 查询进行排序的帮助

这是我尝试过的

$options = ['sort' => ['stats' => ['points' => 1]]];
$query = new MongoDB\Driver\Query([],$options);

这是 mongo 对象的架构

{
    "_id" : ObjectId("5d0cd19c811d53277225fc33"),
    "uuid" : "a2277fa1-07df-4c19-902a-93ae7e2795a1",
    "name" : "ImAleex_",
    "stats" : {
        "points" : 1000,
        "kills" : 1,
        "losses" : 1,
        "wins" : 1
    },
    "info" : {
        "lastRankeds" : NumberLong(1561128665478),
        "lastConnection" : NumberLong(1561128834286),
        "lastModifiedKit" : NumberLong(1561215124553),
        "rankeds" : 5,
        "visibility" : true,
        "chat" : true,
        "fly" : false,
        "requests" : true,
        "menuRequests" : true
    }
}

我在 Apache 日志中收到此错误消息 ConnectionException:错误的排序规范

【问题讨论】:

    标签: php arrays mongodb


    【解决方案1】:

    我终于解决了,这是正确的排序语法

    $options = ['sort' => ['stats.points' => 1]];

    【讨论】:

      【解决方案2】:

      试试:

      $options = ['sort' => ['stats.points' => 1]];
      $query = new MongoDB\Driver\Query([],$options);
      

      【讨论】:

        猜你喜欢
        • 2019-05-13
        • 2021-10-03
        • 2015-11-22
        • 1970-01-01
        • 2017-03-07
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2015-03-31
        相关资源
        最近更新 更多