首页 > 解决方案 > 用 mongodb 中的旧值更新子文档值?

问题描述

我想四舍五入保存在这个集合中的所有价格(有大量文档)我怎么能做到这一点而无需遍历所有文档并更新每个文档?

输入:

[{
    "_id" : ObjectId("5e05453d88ce103684d3b7e5"),//id is primary key for the root document
    "RoomTypes" : [
        {
            "Code" : "STDB",//(Code is ducument leve primary key for this subdocument)
            "RatePlans" : [
                {
                    "Code":"standard",//(Code is ducument leve primary key for this subdocument)
                    "Rates" : [
                        {
                            "_value" : {
                                "Occupancy" : NumberInt(1),
                                "Currency" : "IRR",//(Occupancy an Currency are ducument leve primary key for this subdocument)
                                "Amount" : 3560.5,
                            }
                        },
                        {
                            "_value" : {
                                "Occupancy" : NumberInt(1),
                                "Currency" : "EURO",//(Occupancy an Currency are ducument leve primary key for this subdocument)
                                "Amount" : 35.5,
                            }
                        }

                    ]
                }
            ]
        }
    ]
},
{
    "_id" : ObjectId("5e05453d88ce103684d3b7e5"),//id is primary key for the root document
    "RoomTypes" : [
        {
            "Code" : "STDB",//(Code is ducument leve primary key for this subdocument)
            "RatePlans" : [
                {
                    "Code":"standard",//(Code is ducument leve primary key for this subdocument)
                    "Rates" : [
                        {
                            "_value" : {
                                "Occupancy" : NumberInt(1),
                                "Currency" : "IRR",//(Occupancy an Currency are ducument leve primary key for this subdocument)
                                "Amount" : 3560.2,
                            }
                        }
                    ]
                }
            ]
        }
    ]
}]

预期结果:

[{
    "_id" : ObjectId("5e05453d88ce103684d3b7e5"),//id is primary key for the root document
    "RoomTypes" : [
        {
            "Code" : "STDB",//(Code is ducument leve primary key for this subdocument)
            "RatePlans" : [
                {
                    "Code":"standard",//(Code is ducument leve primary key for this subdocument)
                    "Rates" : [
                        {
                            "_value" : {
                                "Occupancy" : NumberInt(1),
                                "Currency" : "IRR",//(Occupancy an Currency are ducument leve primary key for this subdocument)
                                "Amount" : 3561,
                            }
                        },
                        {
                            "_value" : {
                                "Occupancy" : NumberInt(1),
                                "Currency" : "EURO",//(Occupancy an Currency are ducument leve primary key for this subdocument)
                                "Amount" : 36,
                            }
                        }

                    ]
                }
            ]
        }
    ]
},
{
    "_id" : ObjectId("5e05453d88ce103684d3b7e5"),//id is primary key for the root document
    "RoomTypes" : [
        {
            "Code" : "STDB",//(Code is ducument leve primary key for this subdocument)
            "RatePlans" : [
                {
                    "Code":"standard",//(Code is ducument leve primary key for this subdocument)
                    "Rates" : [
                        {
                            "_value" : {
                                "Occupancy" : NumberInt(1),
                                "Currency" : "IRR",//(Occupancy an Currency are ducument leve primary key for this subdocument)
                                "Amount" : 3560,
                            }
                        }
                    ]
                }
            ]
        }
    ]
}]

标签: mongodb

解决方案


推荐阅读