Mongo DB查询中的嵌套查找

时间:2019-10-30 17:06:24

标签: mongodb mongodb-query aggregation-framework mongodb-lookup

我正在制作航空公司虚拟网页,我需要获取所有航空公司每次航班的所有售票,我有3个集合:航空公司,航班和销售

我尝试在mongodb查询中使用嵌套查询,但无法将总票证相加,这是当前查询和结果:

class Media

结果是:

Airline.aggregate([{
  $lookup: {
    from: "flights",
    localField: "id_airline",
    foreignField: "id_airline",
    as: "flights"
  }
}, {
  $unwind: {
    path: "$flights",
    preserveNullAndEmptyArrays: true
  }
}, {
  $lookup: {
    from: "sales",
    localField: "flights.id_flight",
    foreignField: "id_flight",
    as: "flights.sales",
  }
}, {
  $group: {
    _id : "$_id",
    idAirline: { $first: "$id_airline" },
    flights: { $push: "$flights" }
  }
}, {
  $project: {
    _id: 1,
    idAirline: 1,
    "flights.id_flight": 1,
    "flights.price": 1,
    "flights.sold":{$sum:"$flights.sales.tickets"}
  }
}]

实际上,Clig-CPA-001售出了6张票,但我不能总结出结果。

以下是文件:

航空公司

... },
    {
        "_id": "5db381cb18518043c40e3ecd",
        "idAirline": "AVI-242",
        "flights": [
            {
                "id_flight": "CPA-001",
                "price": "125",
                "sold": 0
            },
            {
                "id_flight": "CGA-002",
                "price": "150",
                "sold": 0
            },
            {
                "id_flight": "CHA-003",
                "price": "135",
                "sold": 0
            }
        ]
    },
....

航班

{"_id":{"$oid":"5db3823718518043c40e3ece"},"country":["Panamá","El Salvador","Belice"],"id_airline":"VOL-643","name_airport":"Juan Santamaría","name_airline":"Volaris","createdAt":{"$date":{"$numberLong":"1572045367094"}},"updatedAt":{"$date":{"$numberLong":"1572045367094"}},"__v":{"$numberInt":"0"}}

销售

{"_id":{"$oid":"5db9a225154a1b1b08cc77c3"},"restrictions":["No Liquids"],"features":[],"id_flight":"CPA-001","id_airline":"AVI-242","date_departure":{"$date":{"$numberLong":"1546300800000"}},"date_arrival":{"$date":{"$numberLong":"1559779200000"}},"name":"CosPan","origin":"Costa Rica","destination":"Panamá","itinerary":"50 breakfasts","price":"125","status":"On Time","max_capacity":"50","createdAt":{"$date":{"$numberLong":"1572446757327"}},"updatedAt":{"$date":{"$numberLong":"1572447464367"}},"__v":{"$numberInt":"0"}}

1 个答案:

答案 0 :(得分:0)

我认为嵌套数组会引起一些困难。我在$ lookup和$ group之间添加了另一个$ unwind。让我知道你的想法...

db.airlines.aggregate([
  { $lookup: {
      from: "flights",
      localField: "id_airline",
      foreignField: "id_airline",
      as: "flights"
    }
  },
  { $unwind: {
      path: "$flights",
      preserveNullAndEmptyArrays: true
    }
  },
  { $lookup: {
      from: "sales",
      localField: "flights.id_flight",
      foreignField: "id_flight",
      as: "flights.sales",
    }
  },
  { $unwind: {
      path: "$flights.sales",
      preserveNullAndEmptyArrays: true
    }
  },
  { $group: {
      _id : "$_id",
      idAirline: { $first: "$id_airline" },
      flights: { $push: "$flights" }
    }
  },
  { $project: {
      _id: 1,
      idAirline: 1,
      "flights.id_flight": 1,
      "flights.price": 1,
      "flights.sold":{$sum: "$flights.sales.tickets"}
    }
  }
]).pretty()

用于测试的文档:

航班

{
    "_id" : ObjectId("5db9a225154a1b1b08cc77c3"),
    "restrictions" : [
        "No Liquids"
    ],
    "features" : [ ],
    "id_flight" : "CPA-001",
    "id_airline" : "VOL-643",
    "date_departure" : ISODate("2019-10-30T23:04:17.119Z"),
    "date_arrival" : ISODate("2019-10-30T23:04:17.119Z"),
    "name" : "CosPan",
    "origin" : "Costa Rica",
    "destination" : "Panamá",
    "itinerary" : "50 breakfasts",
    "price" : "125",
    "status" : "On Time",
    "max_capacity" : "50",
    "createdAt" : ISODate("2019-10-30T23:04:17.119Z"),
    "updatedAt" : ISODate("2019-10-30T23:04:17.119Z"),
    "__v" : {
        "$numberInt" : "0"
    }
}

航空公司

{
    "_id" : ObjectId("5db3823718518043c40e3ece"),
    "country" : [
        "Panamá",
        "El Salvador",
        "Belice"
    ],
    "id_airline" : "VOL-643",
    "name_airport" : "Juan Santamaría",
    "name_airline" : "Volaris",
    "createdAt" : ISODate("2019-10-30T23:04:17.119Z"),
    "updatedAt" : ISODate("2019-10-30T23:04:17.119Z"),
    "__v" : {
        "$numberInt" : "0"
    }
}

销售

[{
    "_id" : ObjectId("5db9b643b50c0d1540b8db89"),
    "id_user" : "304780391",
    "id_flight" : "CPA-001",
    "origin" : "Costa Rica",
    "destination" : "Panamá",
    "date_departure" : ISODate("2019-10-30T23:04:17.119Z"),
    "date_arrival" : ISODate("2019-10-30T23:04:17.119Z"),
    "tickets" : 4,
    "suitcases" : {
        "$numberInt" : "1"
    },
    "status" : "Bought",
    "seat" : -1,
    "createdAt" : ISODate("2019-10-30T23:04:17.119Z"),
    "updatedAt" : ISODate("2019-10-30T23:04:17.119Z"),
    "__v" : {
        "$numberInt" : "0"
    }
},
{
    "_id" : ObjectId("5db9b643b50c0d1540b8db8a"),
    "id_user" : "304780391",
    "id_flight" : "CPA-001",
    "origin" : "Costa Rica",
    "destination" : "Panamá",
    "date_departure" : ISODate("2019-10-30T23:04:17.119Z"),
    "date_arrival" : ISODate("2019-10-30T23:04:17.119Z"),
    "tickets" : 4,
    "suitcases" : {
        "$numberInt" : "1"
    },
    "status" : "Bought",
    "seat" : -1,
    "createdAt" : ISODate("2019-10-30T23:04:17.119Z"),
    "updatedAt" : ISODate("2019-10-30T23:04:17.119Z"),
    "__v" : {
        "$numberInt" : "0"
    }
}]