首页 > 解决方案 > 如何通过 php 按 mongodb 上的嵌套对象排序?

问题描述

我需要有关如何根据嵌套文档对 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: bad sort specification 上收到此错误消息

标签: phparraysmongodb

解决方案


尝试 :

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

推荐阅读