用于选择嵌入对象的Mongodb查询
我有一个收藏,它的文档是这样的用于选择嵌入对象的Mongodb查询,mongodb,mongodb-query,aggregation-framework,pymongo,Mongodb,Mongodb Query,Aggregation Framework,Pymongo,我有一个收藏,它的文档是这样的 {'_id': ObjectId('5d7f4aa4d2394d86aacbfbe0'), 'aeroplanes': [{'_id': ObjectId('5d7f4aa4d2394d86aacbfbd8'), 'capacity': 1442, 'flights': [{'_id': ObjectId('5d7f4aa4d2394d86aacbfbd7'),
{'_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'}
]
我试着跟随并写下:
airline_col.aggregate([
{"$match": {'aeroplanes.flights.seats.ticket': None}},
{"$project": {
"seats" : { "$filter" : {
"input": '$aeroplanes.flights.seats',
"as": 'seat',
"cond": {"$eq": ['$$seat.ticket', None]}
}}
}}
])
但它只是返回空数组。正确的查询是什么
Mongo游乐场查询链接:这里是在$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"
}
]
链接:试试这个:
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}}
]
)
如果您将收集的样本数据和您在查询中所做的工作放在这里,这将对其他人的回答非常有帮助。添加了链接。您只需要查询后的座位数据,而不需要飞机、航班等字段。实际上,我需要这两种查询类型。在一种类型中,只需要座位信息,而在另一种类型中,我也需要所有的家长信息。请重试