MongoDB: Combine data from multiple collections into one..how?

后端 未结 11 1158
余生分开走
余生分开走 2020-11-22 06:44

How can I (in MongoDB) combine data from multiple collections into one collection?

Can I use map-reduce and if so then how?

I would greatly appreciate some

11条回答
  •  -上瘾入骨i
    2020-11-22 07:44

    use multiple $lookup for multiple collections in aggregation

    query:

    db.getCollection('servicelocations').aggregate([
      {
        $match: {
          serviceLocationId: {
            $in: ["36728"]
          }
        }
      },
      {
        $lookup: {
          from: "orders",
          localField: "serviceLocationId",
          foreignField: "serviceLocationId",
          as: "orders"
        }
      },
      {
        $lookup: {
          from: "timewindowtypes",
          localField: "timeWindow.timeWindowTypeId",
          foreignField: "timeWindowTypeId",
          as: "timeWindow"
        }
      },
      {
        $lookup: {
          from: "servicetimetypes",
          localField: "serviceTimeTypeId",
          foreignField: "serviceTimeTypeId",
          as: "serviceTime"
        }
      },
      {
        $unwind: "$orders"
      },
      {
        $unwind: "$serviceTime"
      },
      {
        $limit: 14
      }
    ])
    

    result:

    {
        "_id" : ObjectId("59c3ac4bb7799c90ebb3279b"),
        "serviceLocationId" : "36728",
        "regionId" : 1.0,
        "zoneId" : "DXBZONE1",
        "description" : "AL HALLAB REST EMIRATES MALL",
        "locationPriority" : 1.0,
        "accountTypeId" : 1.0,
        "locationType" : "SERVICELOCATION",
        "location" : {
            "makani" : "",
            "lat" : 25.119035,
            "lng" : 55.198694
        },
        "deliveryDays" : "MTWRFSU",
        "timeWindow" : [ 
            {
                "_id" : ObjectId("59c3b0a3b7799c90ebb32cde"),
                "timeWindowTypeId" : "1",
                "Description" : "MORNING",
                "timeWindow" : {
                    "openTime" : "06:00",
                    "closeTime" : "08:00"
                },
                "accountId" : 1.0
            }, 
            {
                "_id" : ObjectId("59c3b0a3b7799c90ebb32cdf"),
                "timeWindowTypeId" : "1",
                "Description" : "MORNING",
                "timeWindow" : {
                    "openTime" : "09:00",
                    "closeTime" : "10:00"
                },
                "accountId" : 1.0
            }, 
            {
                "_id" : ObjectId("59c3b0a3b7799c90ebb32ce0"),
                "timeWindowTypeId" : "1",
                "Description" : "MORNING",
                "timeWindow" : {
                    "openTime" : "10:30",
                    "closeTime" : "11:30"
                },
                "accountId" : 1.0
            }
        ],
        "address1" : "",
        "address2" : "",
        "phone" : "",
        "city" : "",
        "county" : "",
        "state" : "",
        "country" : "",
        "zipcode" : "",
        "imageUrl" : "",
        "contact" : {
            "name" : "",
            "email" : ""
        },
        "status" : "ACTIVE",
        "createdBy" : "",
        "updatedBy" : "",
        "updateDate" : "",
        "accountId" : 1.0,
        "serviceTimeTypeId" : "1",
        "orders" : [ 
            {
                "_id" : ObjectId("59c3b291f251c77f15790f92"),
                "orderId" : "AQ18O1704264",
                "serviceLocationId" : "36728",
                "orderNo" : "AQ18O1704264",
                "orderDate" : "18-Sep-17",
                "description" : "AQ18O1704264",
                "serviceType" : "Delivery",
                "orderSource" : "Import",
                "takenBy" : "KARIM",
                "plannedDeliveryDate" : ISODate("2017-08-26T00:00:00.000Z"),
                "plannedDeliveryTime" : "",
                "actualDeliveryDate" : "",
                "actualDeliveryTime" : "",
                "deliveredBy" : "",
                "size1" : 296.0,
                "size2" : 3573.355,
                "size3" : 240.811,
                "jobPriority" : 1.0,
                "cancelReason" : "",
                "cancelDate" : "",
                "cancelBy" : "",
                "reasonCode" : "",
                "reasonText" : "",
                "status" : "",
                "lineItems" : [ 
                    {
                        "ItemId" : "BNWB020",
                        "size1" : 15.0,
                        "size2" : 78.6,
                        "size3" : 6.0
                    }, 
                    {
                        "ItemId" : "BNWB021",
                        "size1" : 20.0,
                        "size2" : 252.0,
                        "size3" : 11.538
                    }, 
                    {
                        "ItemId" : "BNWB023",
                        "size1" : 15.0,
                        "size2" : 285.0,
                        "size3" : 16.071
                    }, 
                    {
                        "ItemId" : "CPMW112",
                        "size1" : 3.0,
                        "size2" : 25.38,
                        "size3" : 1.731
                    }, 
                    {
                        "ItemId" : "MMGW001",
                        "size1" : 25.0,
                        "size2" : 464.375,
                        "size3" : 46.875
                    }, 
                    {
                        "ItemId" : "MMNB218",
                        "size1" : 50.0,
                        "size2" : 920.0,
                        "size3" : 60.0
                    }, 
                    {
                        "ItemId" : "MMNB219",
                        "size1" : 50.0,
                        "size2" : 630.0,
                        "size3" : 40.0
                    }, 
                    {
                        "ItemId" : "MMNB220",
                        "size1" : 50.0,
                        "size2" : 416.0,
                        "size3" : 28.846
                    }, 
                    {
                        "ItemId" : "MMNB270",
                        "size1" : 50.0,
                        "size2" : 262.0,
                        "size3" : 20.0
                    }, 
                    {
                        "ItemId" : "MMNB302",
                        "size1" : 15.0,
                        "size2" : 195.0,
                        "size3" : 6.0
                    }, 
                    {
                        "ItemId" : "MMNB373",
                        "size1" : 3.0,
                        "size2" : 45.0,
                        "size3" : 3.75
                    }
                ],
                "accountId" : 1.0
            }, 
            {
                "_id" : ObjectId("59c3b291f251c77f15790f9d"),
                "orderId" : "AQ137O1701240",
                "serviceLocationId" : "36728",
                "orderNo" : "AQ137O1701240",
                "orderDate" : "18-Sep-17",
                "description" : "AQ137O1701240",
                "serviceType" : "Delivery",
                "orderSource" : "Import",
                "takenBy" : "KARIM",
                "plannedDeliveryDate" : ISODate("2017-08-26T00:00:00.000Z"),
                "plannedDeliveryTime" : "",
                "actualDeliveryDate" : "",
                "actualDeliveryTime" : "",
                "deliveredBy" : "",
                "size1" : 28.0,
                "size2" : 520.11,
                "size3" : 52.5,
                "jobPriority" : 1.0,
                "cancelReason" : "",
                "cancelDate" : "",
                "cancelBy" : "",
                "reasonCode" : "",
                "reasonText" : "",
                "status" : "",
                "lineItems" : [ 
                    {
                        "ItemId" : "MMGW001",
                        "size1" : 25.0,
                        "size2" : 464.38,
                        "size3" : 46.875
                    }, 
                    {
                        "ItemId" : "MMGW001-F1",
                        "size1" : 3.0,
                        "size2" : 55.73,
                        "size3" : 5.625
                    }
                ],
                "accountId" : 1.0
            }, 
            {
                "_id" : ObjectId("59c3b291f251c77f15790fd8"),
                "orderId" : "AQ110O1705036",
                "serviceLocationId" : "36728",
                "orderNo" : "AQ110O1705036",
                "orderDate" : "18-Sep-17",
                "description" : "AQ110O1705036",
                "serviceType" : "Delivery",
                "orderSource" : "Import",
                "takenBy" : "KARIM",
                "plannedDeliveryDate" : ISODate("2017-08-26T00:00:00.000Z"),
                "plannedDeliveryTime" : "",
                "actualDeliveryDate" : "",
                "actualDeliveryTime" : "",
                "deliveredBy" : "",
                "size1" : 60.0,
                "size2" : 1046.0,
                "size3" : 68.0,
                "jobPriority" : 1.0,
                "cancelReason" : "",
                "cancelDate" : "",
                "cancelBy" : "",
                "reasonCode" : "",
                "reasonText" : "",
                "status" : "",
                "lineItems" : [ 
                    {
                        "ItemId" : "MMNB218",
                        "size1" : 50.0,
                        "size2" : 920.0,
                        "size3" : 60.0
                    }, 
                    {
                        "ItemId" : "MMNB219",
                        "size1" : 10.0,
                        "size2" : 126.0,
                        "size3" : 8.0
                    }
                ],
                "accountId" : 1.0
            }
        ],
        "serviceTime" : {
            "_id" : ObjectId("59c3b07cb7799c90ebb32cdc"),
            "serviceTimeTypeId" : "1",
            "serviceTimeType" : "nohelper",
            "description" : "",
            "fixedTime" : 30.0,
            "variableTime" : 0.0,
            "accountId" : 1.0
        }
    }
    

提交回复
热议问题