如何按Mongodb中的子阵列ID排序?

时间:2012-07-29 05:25:28

标签: php mongodb sorting

我想知道如何在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") 
}

0 个答案:

没有答案