Php 在Yii2 mongodb中对文档内部进行排序

Php 在Yii2 mongodb中对文档内部进行排序,php,mongodb,sorting,yii2,Php,Mongodb,Sorting,Yii2,我正在使用Yii2和mongodb执行一个应用程序。我想根据id检索客户端的记录,并按日期降序排序该客户端的事件。但我的代码不起作用。请帮助我查找问题 “客户”集合的文档如下所示 { "_id" : ObjectId("55c397646ac8e41812000029"), "name" : "Krishnapriya", "location" : "Karunagapalli", "address" : "", "phoneno" : [

我正在使用Yii2和mongodb执行一个应用程序。我想根据id检索客户端的记录,并按日期降序排序该客户端的事件。但我的代码不起作用。请帮助我查找问题

“客户”集合的文档如下所示

{
    "_id" : ObjectId("55c397646ac8e41812000029"),
    "name" : "Krishnapriya",
    "location" : "Karunagapalli",
    "address" : "",
    "phoneno" : [ 
        "8765456789"
    ],
    "email" : [ 
        "kp@gmail.com"
    ],
    "category" : "0",
    "notes_on_user" : "",
    "event" : [ 
        {
            "event_title" : "Sony Home Theater",
            "event_call_or_visit_purpose" : "2",
            "event_notes" : "She enquired about new Sony XXX 5.1 Channel Home theater",
            "event_call_or_visit" : "1",
            "event_datetime" : ISODate("2015-08-06T17:23:46.000Z")
        }, 
        {
            "event_title" : "San Disk Pendrive",
            "event_call_or_visit_purpose" : "1",
            "event_notes" : "She wants the pendrive to be repaired by 2 days",
            "event_call_or_visit" : "2",
            "event_datetime" : ISODate("2015-08-06T17:31:07.000Z"),
            "item" : {
                "item_name" : "San Disk 4gb",
                "item_model" : "San disk",
                "item_description" : " Pendrive not in reading condition",
                "item_accessories" : []
            }
        }
    ]
}
在这里,我想用“event\u datetime”字段按降序对文档的“event”数组进行排序

       $clientdetail = Yii::$app->mongodb->getCollection('client');
       $result = $clientdetail->find(["_id" =>$id])->sort(["event.event_datetime"=> -1]);

       Yii::$app->response->format = \yii\web\Response::FORMAT_JSON;
       return $result;

它起作用了

嗨,kiran,你找到这个问题的解决方案了吗?如果你解决了这个问题,请与我分享解决方案
$query = Customer::find()->addOrderBy(['name'=> SORT_DESC, 'email' => SORT_DESC]);
$arrayDataProvider = new ArrayDataProvider([
        'allModels' => $query->all(),    
        'pagination' => ['pageSize' => 20],
    ]);