简体   繁体   English

在Yii2 mongodb中对文档进行排序

[英]Sorting inside a document in Yii2 mongodb

I am doing an application with Yii2 and mongodb.I want to retrieve records of a client based on id and sort the events of that client by date in descending order.But my code is not working.Please help me to find the issue. 我正在使用Yii2和mongodb进行应用程序。我想根据id检索客户端的记录,并按日期降序对该客户端的事件进行排序。但是我的代码无法正常工作,请帮助我找到问题。

The 'client' Collection's document is given below “客户”集合的文档如下

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

Here I want to get the document's 'event' array sorted with 'event_datetime' field in descending order .My controller code is given below. 在这里,我想获取文档的'event'数组,并以'event_datetime'字段降序排列。我的控制器代码如下。

       $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;
$query = Customer::find()->addOrderBy(['name'=> SORT_DESC, 'email' => SORT_DESC]);
$arrayDataProvider = new ArrayDataProvider([
        'allModels' => $query->all(),    
        'pagination' => ['pageSize' => 20],
    ]);

It works 有用

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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