如何在mongodb + php中更新嵌套数组?

时间:2016-01-08 05:15:17

标签: php mongodb

[Details] => Array
        (
            [0] => Array
                (
                    [Items] => Array
                        (
                            [0] => Array
                                (
                                    [Description] => Discriptionqwe
                                    [Quantity] => qwewe
                                    [ItemName] => not_available
                                    [CategoryName] => health
                                    [ItemsId] => 568e70d66734c482495fb085
                                )

                        )

                    [Distance] => 56.8KM
                    [VendorId] => 5683ad876734c4e2158b4570
                    [Rating] => 0
                    [OpeningTime] => 10AM to 11PM
                    [VendorName] => bombay ji
                )

        )

1 个答案:

答案 0 :(得分:0)

试试这样:

var update =  db.collection.update(
                                    { _id:ObjectId("id_of_record")},
                                    { $set: { "Details.$.Items.$.Description":"Discriptionqwe" } }
                            );

    var is_row_affected = db.runCommand({getLastError : 1});                                

    if(is_row_affected['updatedExisting'] == true){  
         result = {msg: '1', data: []};
    }else {
         result = { msg: '0', data: []};
    }
    return result;