【问题标题】:Mongodb query for selecting embeded object用于选择嵌入对象的MongoDB查询
【发布时间】:2019-09-16 09:04:44
【问题描述】:

我有一个文档看起来像这样的集合

{'_id': ObjectId('5d7f4aa4d2394d86aacbfbe0'),
 'aeroplanes': [{'_id': ObjectId('5d7f4aa4d2394d86aacbfbd8'),
                 'capacity': 1442,
                 'flights': [{'_id': ObjectId('5d7f4aa4d2394d86aacbfbd7'),
                              'arrival_time': datetime.datetime(2010, 10, 8, 3, 26, 50),
                              'departure_time': datetime.datetime(1988, 6, 29, 14, 10, 52),
                              'gate_number': 6,
                              'seats': [{'_id': ObjectId('5d7f4aa4d2394d86aacbfbd6'),
                                         'price': 1779,
                                         'ticket': None,
                                         'type': 'A'}]}],

我想选择所有无(空)类型的座位。我想要的输出是这样的:

'seats': [
   {'_id': ObjectId('5d7f4aa4d2394d86aacbfbd6'),
     'price': 1779,
     'ticket': None,
     'type': 'A'},
   {'_id': ObjectId('5d7f4aa4d2394566acfgbgt'),
    'price': 3546,
    'ticket': None,
    'type': 'A'}
]

我尝试关注this answer 并写道:

airline_col.aggregate([
{"$match": {'aeroplanes.flights.seats.ticket': None}},
{"$project": {
    "seats" : { "$filter" : {
        "input": '$aeroplanes.flights.seats',
        "as": 'seat',
        "cond": {"$eq": ['$$seat.ticket', None]}
    }}
  }}
])

但它只是返回空数组。正确的查询是什么?

Mongo Playground 查询链接:https://mongoplayground.net/p/Wl2l4IdJLT2

【问题讨论】:

  • 如果您将收集的示例数据和您在查询中所做的工作放在此处mongoplayground.net 用于 mongo-query,这对其他人的回答将非常有帮助。
  • 添加了链接。
  • 您只需要查询后响应的座位数据,而不是飞机、航班等字段?
  • 其实这两种查询类型我都需要。在一种类型中,只需要座位信息,在另一种类型中,我也需要所有家长信息。

标签: mongodb mongodb-query aggregation-framework pymongo


【解决方案1】:

所以这是在 $map 和 $filter 阶段使用 $cond 后的最终查询

输入:

[
  {
    "_id": "5d7f4aa4d2394d86aacbfbe0",
    "aeroplanes": [
      {
        "_id": "5d7f4aa4d2394d86aacbfbd8",
        "capacity": 1442,
        "flights": [
          {
            "_id": "5d7f4aa4d2394d86aacbfbd7",
            "arrival_time": "2003-07-29",
            "departure_time": "1984-09-19",
            "gate_number": 6,
            "seats": [
              {
                "_id": "5d7f4aa4d2394d86aacbfbd6",
                "price": 1779,
                "ticket": null,
                "type": "A"
              }
            ]
          }
        ],
        "model": "Blue writer second all capital become."
      },
      {
        "_id": "5d7f4aa4d2394d86aacbfbdf",
        "capacity": 240,
        "flights": [
          {
            "_id": "5d7f4aa4d2394d86aacbfbde",
            "airport_id_as_dest": "5d7f49f1d2394d86aacb7d26",
            "airport_id_as_source": "5d7f49f1d2394d86aacb7cc9",
            "arrival_time": "2009-04-30",
            "departure_date": "2016-05-07",
            "gate_number": 2,
            "seats": [
              {
                "_id": "5d7f4aa4d2394d86aacbfbdd",
                "price": 1896,
                "ticket": {
                  "_id": "5d7f4aa4d2394d86aacbfbdc",
                  "agent_id": "5d7f49f1d2394d86aacb7cf2",
                  "boarding_pass": {
                    "_id": "5d7f4aa4d2394d86aacbfbdb"
                  },
                  "pnr": {
                    "_id": "5d7f4aa4d2394d86aacbfbda",
                    "name": "Breanna",
                    "passenger_id": "5d7f49f1d2394d86aacb7d3a",
                    "services": [
                      {
                        "_id": "5d7f4aa4d2394d86aacbfbd9",
                        "service": "Dawn "
                      }
                    ]
                  }
                },
                "type": "A"
              }
            ]
          }
        ],
        "model": "Stand system pattern write."
      }
    ],
    "name": "Raise billion order close.",
    "type": "Asd-1-3"
  }
]

聚合管道:

db.collection.aggregate([
  {
    $project: {
      type: 1,
      name: 1,
      aeroplanes: {
        $filter: {
          input: "$aeroplanes",
          as: "a1",
          cond: {
            $ne: [
              {
                $map: {
                  input: "$$a1.flights",
                  as: "a2",
                  in: {
                    $cond: [
                      {
                        $eq: [
                          {
                            $filter: {
                              input: "$$a2.seats",
                              as: "a3",
                              cond: {
                                $eq: [
                                  "$$a3.ticket",
                                  null
                                ]
                              }
                            }
                          },
                          []
                        ]
                      },
                      "NO_DATA",
                      {
                        _id: "$$a2._id",
                        arrival_time: "$$a2.arrival_time",
                        departure_time: "$$a2.departure_time",
                        gate_number: "$$a2.gate_number",
                        model: "$$a2.model",
                        seats: {
                          $filter: {
                            input: "$$a2.seats",
                            as: "a3",
                            cond: {
                              $eq: [
                                "$$a3.ticket",
                                null
                              ]
                            }
                          }
                        }
                      }
                    ]
                  }
                }
              },
              [
                "NO_DATA"
              ]
            ]
          }
        },

      }
    }
  }
])

输出:

[
  {
    "_id": "5d7f4aa4d2394d86aacbfbe0",
    "aeroplanes": [
      {
        "_id": "5d7f4aa4d2394d86aacbfbd8",
        "capacity": 1442,
        "flights": [
          {
            "_id": "5d7f4aa4d2394d86aacbfbd7",
            "arrival_time": "2003-07-29",
            "departure_time": "1984-09-19",
            "gate_number": 6,
            "seats": [
              {
                "_id": "5d7f4aa4d2394d86aacbfbd6",
                "price": 1779,
                "ticket": null,
                "type": "A"
              }
            ]
          }
        ],
        "model": "Blue writer second all capital become."
      }
    ],
    "name": "Raise billion order close.",
    "type": "Asd-1-3"
  }
]

链接:https://mongoplayground.net/p/UrR8TTuMgDF

【讨论】:

    【解决方案2】:

    试试这个:

    db.collection.aggregate(
    [
        {"$unwind": "$aeroplanes"},
        {"$unwind": "$aeroplanes.flights"},
        {"$unwind": "$aeroplanes.flights.seats"},
        {"$match": {"aeroplanes.flights.seats.ticket": null}},
        {"$project": {"aeroplanes.flights.seats": 1, "_id": 0}}
    ]
    )
    

    https://mongoplayground.net/p/d3X0pX2Zkrl

    【讨论】:

      猜你喜欢
      • 2022-11-03
      • 2020-05-29
      • 1970-01-01
      • 2023-03-11
      • 1970-01-01
      • 2021-01-24
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多