通过PHP合并两个mongodb文档?

时间:2015-11-27 10:45:01

标签: php mongodb merge documents

我希望能够将两个文档合并在一起,这些文档位于同一个集合中。例如,这里是' Master'文档。

第一个文件

{
    "_id" : ObjectId("5640965bb23eec8d218b4a92"),
    "agencyID" : "111111",
    "paxID" : "22222",
    "bookings" : [
        {
            "bookingID": "213123",
            "transactions": [
                {
                    "tranID": "001",
                    "invoices": [
                        {
                            "invNum": "1234145",
                            "type": "i",
                            "inv_date": "20140318",
                            "inv_time": "1642",
                            "inv_val": NumberLong(81383)
                        }
                    ],
                    "tranType": "Car",
                    "tranDate": "20140318",
                    "tranTime": "2312",
                    "opCode": "ge2123",
                    "pupDate": "20140104",
                    "advPur": "13"
                }
            ]
        }
    ]
}

我想将它与这样的文档合并

文档二

{
    "_id" : ObjectId("5640966ab23eec8d218b4c5a"),
    "agencyID" : "22122",
    "paxID" : "-14141",
    "bookings" : [
        {
            "bookingID": "452452",
            "transactions": [
                {
                    "tranID": "002",
                    "invoices": [
                        {
                            "invNum": "2432422",
                            "type": "i",
                            "inv_date": "20141119",
                            "inv_time": "1407",
                            "inv_val": NumberLong(20840)
                        }
                    ]
                }
            ]
        }
    ]
}

因此,例如,一个PHP文件将合并文档' 两个'进入文档' 一个',而不是覆盖文档' 一个'它增加了另一个预订'到文件' 一个'当前预订,所以当合并完成时,文件' 一个'会预订 - 213123& 452452以及其他预订信息。

我希望我能够很好地解释它。

编辑 -

合并应该如下所示。

    {
    "_id" : ObjectId("5640965bb23eec8d218b4a92"),
    "agencyID" : "111111",
    "paxID" : "22222",
    "bookings" : [
        {
            "bookingID": "213123",
            "transactions": [
                {
                    "tranID": "001",
                    "invoices": [
                        {
                            "invNum": "1234145",
                            "type": "i",
                            "inv_date": "20140318",
                            "inv_time": "1642",
                            "inv_val": NumberLong(81383)
                        }
                    ],
                    "tranType": "Car",
                    "tranDate": "20140318",
                    "tranTime": "2312",
                    "opCode": "ge2123",
                    "pupDate": "20140104",
                    "advPur": "13"
                }
            ]
        },
        {
        "bookingID": "452452",
            "transactions": [
                {
                    "tranID": "002",
                    "invoices": [
                        {
                            "invNum": "2432422",
                            "type": "i",
                            "inv_date": "20141119",
                            "inv_time": "1407",
                            "inv_val": NumberLong(20840)
                        }
                    ]
                }
            ]
        }
    ]
}

0 个答案:

没有答案