对于聚合查询,我需要您的帮助。 发布前我进行了一些搜索,但是没有找到结果对我有用。
我有那些文件:
订单:
{
"_id" : MongoId,
"customId" : integer,
... other fields
"circuit" : MongoId, // Ref to Circuit Document
"agency" : MongoId, // Ref to Agency document
"customer" : MongoId, // Ref to Customer document
"places" : [ // Array of MongoId ref to place document
MongoId,
MongoId,
...
],
"materials" : [ // Array of MongoId ref to material document
MongoId,
MongoId,
...
],
... other fields
}
电路:
{
"_id" : MongoId,
"customId" : Integer,
"name" : String,
"state" : String,
"type" : String,
"agency" : MongoId,
"trucks" : [ // Array of MongoId ref to truck document
MongoId,
MongoId,
...
],
"drivers" : [ // Array of MongoId ref to truck document
MongoId,
MongoId,
...
],
"orders" : [ // Array of MongoId ref to order document
MongoId,
MongoId,
...
]
}
所以我需要为每个赛道获得司机,卡车和订单。这部分没问题,查询有效(请参见下文)。
但是我没有从每个电路的每个订单中获得所有的位置和材料。
所以,这是我当前的查询:
db.circuit.aggregate([
{$match: {"date": {$lte: ISODate("2019-04-01 23:59:59")}}},
{$match: {"date": {$gte: ISODate("2019-03-31 00:00:59")}}},
{
$lookup: {
from: "agency",
localField: "agency",
foreignField: "_id",
as: "agency"
}
},
{$unwind: "$agency"},
{
$lookup: {
from: "order",
localField: "orders",
foreignField: "_id",
as: "orders"
}
},
{
$lookup: {
from: "driver",
localField: "drivers",
foreignField: "_id",
as: "drivers"
}
},
{
$group:
{
"_id": "$_id",
"agency": {$addToSet: "$agency.name"},
"date": {$addToSet: "$date"},
"orders": {$addToSet: "$orders"},
"drivers": {$addToSet: "$drivers"},
}
},
{$unwind: "$agency"},
{$unwind: "$date"},
{$unwind: "$orders"},
{$unwind: "$drivers"},
{
$project: {
"agency": "$agency",
"date": "$date",
"closedOrders": {
$map: {
input: {
$filter: {
input: "$orders",
as: "o",
cond: {
$in: [
"$$o.state", ["closed"]
]
}
}
},
as: "order",
in: {
"id": "$$order.customId"
}
}
},
"startedOrders": {
$map: {
input: {
$filter: {
input: "$orders",
as: "o",
cond: {
$in: [
"$$o.state", ["started"]
]
}
}
},
as: "order",
in: {
"id": "$$order.customId"
}
}
},
"plannedOrders": {
$map: {
input: {
$filter: {
input: "$orders",
as: "o",
cond: {
$in: [
"$$o.state", ["started", "added"]
]
}
}
},
as: "order",
in: {
"id": "$$order.customId"
}
}
},
"orders": {
$map: {
input: "$orders",
as: "o",
in: {
... Orders fields
}
}
},
"drivers": {
$map: {
input: "$drivers",
as: "driver",
in: {
... Drivers fields
}
}
}
}
}
]);
我尝试了几种徒劳地从电路(两层嵌套文档)的订单中获取位置和材料的方法。
为了说明我的需求,输出必须查看此内容:
{
"_id" : "5c6e6f7be05bd900a1682582",
"customId" : 4966075,
"name" : "Circuit name",
"date" : ISODate("2019-04-01T23:00:00.000Z"),
"type" : "TYPE",
"agency" : {
"_id" : "5c6cfe7ae05bd9007477f7ee",
"customId" : 0,
"name" : "AGENCY NAME"
},
"trucks" : [
{
"_id" : "5c6e6f7be05bd900a1682583",
"code" : "CODE",
},
{
"_id" : "5c6cfe7ee05bd9007478018f",
"code" : "CODE NEXT",
}
],
"drivers" : [
{
"_id" : "5c7e6f7be05bd900a168264a",
"code" : "CODE D",
},
{
"_id" : "5c89fe7ee05bd90074782549",
"code" : "CODE D NEXT",
}
],
"orders" : [
{
"_id" : "5c6e7125e05bd900a1683596",
"customId" : 122116867,
"bonPassage" : "ELEC",
"date" : ISODate("2019-04-01T23:00:15.000Z"),
"state" : "closed",
"type" : "TYPE",
"circuit" : "5c6e6f7be05bd900a1682582",
"agency" : {
"_id" : "5c6cfe7ae05bd9007477f7ee",
"customId" : 0,
"name" : "AGENCY NAME"
},
"customer" : {
"_id" : "5c6cfed9e05bd90074787634",
"customId" : 0,
"nom" : "CUSTOMER NAME",
},
"places" : [
{
"_id" : "5c6d0181e05bd900747a3a71",
"customId" : 0,
"name" : "Unknown",
"agencies" : []
},
...
],
"materials" : [
{
"_id" : "5c6d024be05bd900747a542e",
"customId" : 0,
"code" : "Unknown",
"agencies" : []
},
...
],
},
...
]
}
您能否帮助我为查询实现最后一部分,并且如果可能的话,还可以提供良好的性能:)
谢谢!