Как объединить несколько таблиц в mongodb - PullRequest
0 голосов
/ 03 января 2019

В настоящее время у меня есть 5 таблиц, которые необходимо объединить из-за их связи.Используя $ lookup, я могу соединить таблицу Order с таблицей Plan и получить то, что мне нужно, но как мне поступить с другими таблицами?

Вот каждая таблица и Id / таблица, с которой она должна соединиться

Plan       - _id, unassignedOrderIds(array)
DriverPlan - _id, planId, orderIds(array), driverId, vehicleId
Driver     - _id, vehicleId
Vehicle    - _id
Orders     - _id

В конце я ищу mongoDb для возврата объекта Plan с загруженными UnassignedOrders и DriverPlans.За ним следуют DriverPlans с загруженными заказами, водителем и транспортным средством.

Вот что у меня есть:

db.Plan.aggregate([
    // Unwind the source
    { "$unwind": "$UnassignedOrderIds" },
    // Do the lookup matching
    { "$lookup": {
       "from": "Order",
       "localField": "UnassignedOrderIds",
       "foreignField": "_id",
       "as": "UnassignedOrders"
    }},
    // Unwind the result arrays ( likely one or none )
    { "$unwind": "$UnassignedOrders" },
    // Group back to arrays
    { "$group": {
        "_id": "$_id",
        "Order": { "$push": "$Order" },
        "UnassignedOrders": { "$push": "$UnassignedOrders" }
    }}
])

Образец документа:

//Plan
{
    "_id" : ObjectId("5c1d244de707b20cece645f1"),
    "UnassignedOrderIds" : [ 
        ObjectId("5c1d247fe707b20cece6462e"), 
        ObjectId("5c1d035de707b20cece63104")
    ]
}

//DriverPlan
[{
    "_id" : ObjectId("123d247fe707b20cece6462e"),
    "PlanId" : ObjectId("5c1d244de707b20cece645f1"),
    "DriverId" : ObjectId("1c1d247fe707b20cece64622"),
    "VehicleId" : ObjectId("3c1d247fe707b20cece64633"),
    "OrderIds": [ 
        ObjectId("5c1d247fe707b20cece64621"), 
        ObjectId("5c1d247fe707b20cece64624")
    ]
},{
    "_id" : ObjectId("123d247fe707b20cece64655"),
    "PlanId" : ObjectId("5c1d244de707b20cece645f1"),
    "DriverId" : ObjectId("2c1d035de707b20cece63104"),
    "VehicleId" : null,
    "OrderIds": [ 
        ObjectId("5c1d247fe707b20cece64625")
    ]
}]

//Orders
[{
    "_id" : ObjectId("5c1d247fe707b20cece6462e"),
    "name" "Order1"
},{
    "_id" : ObjectId("5c1d035de707b20cece63104"),
    "name" "Order2"
},{
    "_id" : ObjectId("5c1d247fe707b20cece64621"),
    "name" "Order3"
},{
    "_id" : ObjectId("5c1d247fe707b20cece64624"),
    "name" "Order4"
},{
    "_id" : ObjectId("5c1d247fe707b20cece64625"),
    "name" "Order5"
}]

//Driver
[{
    "_id" : ObjectId("1c1d247fe707b20cece64622"),
    "vehicleId" : ObjectId("3c1d247fe707b20cece6462e"),
    "name" "Driver1"
},{
    "_id" : ObjectId("2c1d035de707b20cece63104"),
    "vehicleId" : null,
    "name" "Driver2"
},{
    "_id" : ObjectId("3c1d247fe707b20cece64621"),
    "vehicleId" : ObjectId("3c1d035de707b20cece63104"),
    "name" "Driver3"
}]

//Vehicle
[{
    "_id" : ObjectId("3c1d247fe707b20cece6462e"),
    "name" "Vehicle1"
},{
    "_id" : ObjectId("3c1d035de707b20cece63104"),
    "name" "Vehicle2"
},{
    "_id" : ObjectId("3c1d247fe707b20cece64633"),
    "name" "Vehicle3"
}]

Ожидаемыйвыходной файл json выглядит следующим образом

//Plan with children loaded
{
"_id" : ObjectId("5c1d244de707b20cece645f1"),
"UnassignedOrderIds" : [ 
    ObjectId("5c1d247fe707b20cece6462e"), 
    ObjectId("5c1d035de707b20cece63104")
],
"UnassignedOrders": [{
    "_id" : ObjectId("5c1d247fe707b20cece6462e"),
    "name" "Order1"
},{
    "_id" : ObjectId("5c1d035de707b20cece63104"),
    "name" "Order2"
}],
"DriverPlans" : 
[{
    "_id" : ObjectId("123d247fe707b20cece6462e"),
    "PlanId" : ObjectId("5c1d244de707b20cece645f1"),
    "DriverId" : ObjectId("1c1d247fe707b20cece64622"),
    "Driver": {
        "_id" : ObjectId("1c1d247fe707b20cece64622"),
        "vehicleId" : ObjectId("3c1d247fe707b20cece6462e"),
        "name" "Driver1"
    },
    "VehicleId" : ObjectId("3c1d247fe707b20cece64633"),
    "Vehicle" : {
        "_id" : ObjectId("3c1d247fe707b20cece64633"),
        "name" "Vehicle3"
    },
    "OrderIds": [ 
        ObjectId("5c1d247fe707b20cece64621"), 
        ObjectId("5c1d247fe707b20cece64624")
    ],
    "Orders" : [ 
        {
            "_id" : ObjectId("5c1d247fe707b20cece64621"),
            "name" "Order3"
        },{
            "_id" : ObjectId("5c1d247fe707b20cece64624"),
            "name" "Order4"
        }]
},{
    "_id" : ObjectId("123d247fe707b20cece64655"),
    "PlanId" : ObjectId("5c1d244de707b20cece645f1"),
    "DriverId" : ObjectId("2c1d035de707b20cece63104"),
    "Driver" : {
        "_id" : ObjectId("2c1d035de707b20cece63104"),
        "vehicleId" : null,
        "name" "Driver2"
    }
    "VehicleId" : null,
    "Vehicle" : null,
    "OrderIds": [ 
        ObjectId("5c1d247fe707b20cece64625")
    ],
    "Orders": [{
            "_id" : ObjectId("5c1d247fe707b20cece64625"),
            "name" "Order5"
        }
    ]
}]

}

1 Ответ

0 голосов
/ 03 января 2019

Вы можете использовать ниже агрегации

db.Plan.aggregate([
  { "$lookup": {
    "from": Order.collection.name,
    "let": { "unassignedOrderIds": "$UnassignedOrderIds" },
    "pipeline": [
      { "$match": { "$expr": { "$in": ["$_id", "$$unassignedOrderIds"] } } }
    ],
    "as": "UnassignedOrderIds"
  }},
  { "$lookup": {
    "from": DriverPlan.collection.name,
    "let": { "planId": "$_id" },
    "pipeline": [
      { "$match": { "$expr": { "$eq": ["$PlanId", "$$planId"] } } },
      { "$lookup": {
        "from": Driver.collection.name,
        "let": { "driveId": "$DriverId" },
        "pipeline": [
          { "$match": { "$expr": { "$eq": ["$_id", "$$driveId"] } } }
        ],
        "as": "Driver"
      }},
      { "$lookup": {
        "from": Vehicle.collection.name,
        "let": { "vehicleId": "$VehicleId" },
        "pipeline": [
          { "$match": { "$expr": { "$eq": ["$_id", "$$vehicleId"] } } }
        ],
        "as": "Vehicle"
      }},
      { "$lookup": {
        "from": Order.collection.name,
        "let": { "orderIds": "$OrderIds" },
        "pipeline": [
          { "$match": { "$expr": { "$in": ["$_id", "$$orderIds"] } } }
        ],
        "as": "Orders"
      }}
    ],
    "as": "DriverPlans"
  }}
])

С mongodb 3.4 и ниже $lookup синтаксис

db.Plan.aggregate([
  { "$lookup": {
    "from": Order.collection.name,
    "localField": "UnassignedOrderIds",
    "foreignField": "_id",
    "as": "UnassignedOrderIds"
  }},
  { "$lookup": {
    "from": DriverPlan.collection.name,
    "localField": "_id",
    "foreignField": "PlanId",
    "as": "DriverPlans"
  }},
  { "$unwind": "$DriverPlans" },
  { "$lookup": {
    "from": Driver.collection.name,
    "localField": "DriverPlans.DriverId",
    "foreignField": "_id",
    "as": "DriverPlans.Driver"
  }},
  { "$unwind": "$DriverPlans.Driver" },
  { "$lookup": {
    "from": Vehicle.collection.name,
    "localField": "DriverPlans.VehicleId",
    "foreignField": "_id",
    "as": "DriverPlans.Vehicle"
  }},
  { "$unwind": "$DriverPlans.Vehicle" },
  { "$lookup": {
    "from": Order.collection.name,
    "localField": "DriverPlans.OrderIds",
    "foreignField": "_id",
    "as": "DriverPlans.Orders"
  }},
  { "$group": {
    "_id": "$_id",
    "DriverPlans": { "$push": "$DriverPlans" }
  }}
])
...