简体   繁体   中英

How to sort by nested object on mongodb via php?

I need help on how to sort a mongodb query based on a nested document

This is what i tried

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

This is the schema of the mongo object

{
    "_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
    }
}

I get this error message on Apache logs ConnectionException: bad sort specification

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

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

Try :

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM