1

我想知道如何在 Mongodb 中对子数组进行排序。

我有以下

$cursor = $collection->find(array("_id"=>new MongoId($this->data['cid'])));
            $cursor->sort(array("replied"=>array("replyid"=>1)));
                if ($cursor->count() > 0)
                {
                    $test = array();
                    // iterate through the results
                    while( $cursor->hasNext() ) {   
                    $test[] = ($cursor->getNext());
                    }
                    return $test;
                }

但是我尝试过但$cursor->sort(array("replied"=>array("replyid"=>0)));没有$cursor->sort(array("replied"=>array("replyid"=>1)));成功

我想知道是否需要在 foreach 语句中执行此操作

<?php
            if($customersupport[0]['replies'] != NULL)
            {
            foreach($customersupport[0]['replies'] as $customer_reply)
            {
            ?>
            <div style="border-bottom:thin solid #666;">
                <span style="color:#666;float:left; width:40%;">
                <?php if($customer_reply['whoreplied'] == "user"){
                    $usertwo = $fetch->namesearch($customer_reply['uid']);
                }else{
                    $usertwo = $fetch->adminnamesearch($customer_reply['uid']);
                }
                    print $usertwo[0]['personalinfo']['firstname']." ".$usertwo[0]['personalinfo']['lastname'];
                ?></span><span style="float:right; width:40%; text-align:right;font-size:12px;"><?=$customer_reply['replydate'];?></span>
                <br>
                <?=$customer_reply['msg'];?>
            </div>
            <?php 
            }
            }
            ?>

这是我们的 mongodb 示例

{
   "_id": ObjectId("5XXXXXXXX000000"),
   "issue": "test",
   "replies": {
     "0": {
       "replyid"▼: ObjectId("5014cXXXXXXXXXX0000"),
       "replydate": "29\/07\/2012CDT00: 18: 50",
       "whoreplied": "user",
       "uid": ObjectId("4fXXXXXXXXXXXXX00"),
       "msg": "test" 
    } 
  },
   "status": "Issued",
   "subject": "test",
   "uid": ObjectId("4XXXXXXXX000000") 
}
4

0 回答 0