按总费用对子阵列进行排序

时间:2014-04-14 13:13:42

标签: php mongodb smarty

我想按total_charges进行排序。 我的mongodb数据如下:

 {
    "_id" : ObjectId("534bceb303ea2ecd1de86944"),
    "Hotel" : [ 
        {
            "0" : 0,
            "hotelId" : "123",
            "name" : "HOTEL - DEMO1",
            "city" : "Paris",
            "address1" : "Demo -Paris ",
            "total_charges" : 1653.76,
            "propertyAvailable" : 1
        }, 
        {
            "0" : 0,
            "hotelId" : "134",
            "name" : "HOTEL - DEMO2",
            "city" : "Paris",
            "address1" : "Demo Paris ",
            "total_charges" : 1875.71,
            "propertyAvailable" : 1
        }, 
        {
            "0" : 0,
            "hotelId" : "145",
            "name" : "HOTEL - DEMO3",
            "city" : "Paris",    
            "address1" : "Demo Paris ",
            "total_charges" : 1143.18,
            "propertyAvailable" : 1
        }
    ],
    "size" : 3
}

我在PHP中尝试过类似的东西。查找工作正常但排序无效

        $dcnew = $dbnew->selectCollection($a);
        $results = $dcnew->find()->sort(array("Hotel.total_charges" => 1));
        echo "<pre>";
        foreach($results as $test) {
               $test1=$test;
        }
        echo "</pre>";
    $smarty->assign("result",$test1['Hotel']);
    $smarty->assign('template',"mongo_hotel_search_listing.tpl");

知道它为什么不起作用?
谢谢

2 个答案:

答案 0 :(得分:1)

以下答案适用于此问题

db.collection.aggregate([{ "$unwind": "$Hotel" },{ $sort : { 'Hotel.total_charges' : 1 } },{$group:{_id:'$Hotel.name',Hotel:{$push:{'total_charges':'$Hotel.total_charges'}}}}])

答案 1 :(得分:0)

这里的问题是你实际上正在排序&#34; top&#34;级别文档,而不是您需要的数组中的子文档。

所以基本的查询表单(不直接作为PHP,所以更多的人得到它)实际上是使用聚合完成的,你无法对数组中元素的插入或修改进行预排序:

db.collection.aggregate([
    // De-normalize the array into individual documents
    { "$unwind": "$Hotel" },

    // Actually sort the array documents
    { "$sort": { "Hotel.total_charges": 1 } },

    // Put everything back into original form, now sorted
    { "$group": {
        "_id": "$_id",
        "Hotel": { "$push": { 
            "hotelId" : "$Hotel.hotelId",
            "name" : "$Hotel.name",
            "city" : "$Hotel.city",
            "rateCurrencyCode" : "$Hotel.rateCurrencyCode",
            "address1" : "$Hotel.address1",
            "total_charges" : "$Hotel.total_charges"
        }}
    }}
])

或以其他方式将排序顺序反转为-1以获得最高至最低。

结果:

{
    "_id" : ObjectId("534bceb303ea2ecd1de86944"),
    "Hotel" : [
            {
                    "hotelId" : "1033132",
                    "name" : "VICTORIA PALACE HOTEL - DEMO: T2",
                    "city" : "Paris,FR",
                    "rateCurrencyCode" : "SAR",
                    "address1" : "Demo Paris ",
                    "total_charges" : 1143.18
            },
            {
                    "hotelId" : "11711",
                    "name" : "HOTEL DU TRIOMPHE - DEMO: T2",
                    "city" : "Paris,FR",
                    "rateCurrencyCode" : "SAR",
                    "address1" : "Demo -Paris ",
                    "total_charges" : 1653.76
            },
            {
                    "hotelId" : "17421",
                    "name" : "RAPHAEL HOTEL - DEMO: T2",
                    "city" : "Trocadero,Paris,FR",
                    "rateCurrencyCode" : "SAR",
                    "address1" : "Demo Paris ",
                    "total_charges" : 1875.71
            }
    ]
}

但很容易转换为PHP代码。