Loading

mongodb 3.x 之实用新功能窥看[2] ——使用$lookup做多表关联处理

mongodb 3.x 之实用新功能窥看[2] ——使用$lookup做多表关联处理

e.g.

{
    "aggregate":"order",
    "pipeline":[
        {
            "$lookup":{
                "from":"order",
                "localField":"_id",
                "foreignField":"_id",
                "as":"order"
            }
        },
        {
            "$lookup":{
                "from":"slOrder",
                "localField":"_id",
                "foreignField":"orderId",
                "as":"slOrder"
            }
        },
        {
            "$lookup":{
                "from":"businessOrderAuditing",
                "localField":"_id",
                "foreignField":"orderId",
                "as":"businessOrderAuditing"
            }
        },
        {
            "$lookup":{
                "from":"businessOrderHandling",
                "localField":"_id",
                "foreignField":"orderId",
                "as":"businessOrderHandling"
            }
        },
        {
            "$match":{
                "_id":"1200-1200-AQX-201801180004",
                "slCategory":2,
                "businessOrderAuditing..businessConfirmInfo.guaranteeHeadBankId":"01005201706291222390007000000",
                "businessOrderAuditing..businessConfirmInfo.guaranteeBranchBankId":"01005201706291223350009000000",
                "businessOrderAuditing..businessConfirmInfo.guaranteeSubBankId":"01005201706291224170011000000",
                "$or":[
                    {
                        "buyers":{
                            "$elemMatch":{
                                "$and":[
                                    {
                                        "name":"张三"
                                    },
                                    {
                                        "isBorrowMan":1
                                    }
                                ]
                            }
                        }
                    },
                    {
                        "salers":{
                            "$elemMatch":{
                                "$and":[
                                    {
                                        "name":"张三"
                                    },
                                    {
                                        "isBorrowMan":1
                                    }
                                ]
                            }
                        }
                    }
                ],
                "slOrder.applyForDate":{
                    "$gte":{
                        "$date":"2017-12-31T16:00:00.000Z"
                    },
                    "$lte":{
                        "$date":"2018-01-24T16:00:00.000Z"
                    }
                },
                "businessOrderAuditing.redemptionCheckInfoPol.businessConfirmInfo.guaranteeStatus":{
                    "$exists":true
                },
                "businessOrderHandling.sateMentHandlingPol.relieveGuaranteeNodeInfo.relieveState":{
                    "$exists":false
                }
            }
        },
        {
            "$sort":{
                "applyForDate":-1
            }
        },
        {
            "$skip":0
        },
        {
            "$limit":10
        }
    ]
}

^_^

posted @ 2017-12-07 18:54  dai.sp  阅读(769)  评论(0编辑  收藏  举报