Прежде чем я пошел в производство, мне просто нужно было сохранить в определенном массиве и показать все, поэтому я использовал метод SET, чтобы сделать это.
var userss = User.aggregate([{
$match: {
grnNo: userParam.grnNo
}
}, {
$lookup: {
from: "goods",
let: { grnNo: "$grnNo", defaultAccountId: "$defaultAccountId" },
pipeline: [{
$match: {
$expr: {
$and: [{
$eq: ["$defaultAccountId", '$$defaultAccountId'],
$eq: ["$grnNo", '$$grnNo']
}]
}
}
}],
as: "Goods",
},
},
{
$lookup: {
from: "inwards",
let: { grnNo: "$grnNo", defaultAccountId: "$defaultAccountId" },
pipeline: [
{
$match: {
$expr: {
$and: [{
$eq: ["$defaultAccountId", '$$defaultAccountId'],
$eq: ["$grnNo", '$$grnNo']
}]
}
}
}
],
as: "Inwards",
}
},
{
$lookup: {
from: "productions",
let: { grnNo: "$grnNo", defaultAccountId: "$defaultAccountId" },
pipeline: [{
$match: {
$expr: {
$and: [{
$eq: ["$defaultAccountId", '$$defaultAccountId'],
$eq: ["$grnNo", '$$grnNo']
}]
}
}
}, ],
as: "Productions",
}
},
{
$lookup: {
from: "despatches",
let: { grnNo: "$grnNo", defaultAccountId: "$defaultAccountId" },
pipeline: [{
$match: {
$expr: {
$and: [{
$eq: ["$defaultAccountId", '$$defaultAccountId'],
$eq: ["$grnNo", '$$grnNo']
}]
}
}
}, ],
as: "Despatch",
}
},
{
$lookup: {
from: "deliveries",
let: { grnNo: "$grnNo", defaultAccountId: "$defaultAccountId" },
pipeline: [{
$match: {
$expr: {
$and: [{
$eq: ["$defaultAccountId", '$$defaultAccountId'],
$eq: ["$grnNo", '$$grnNo']
}]
}
}
}, ],
as: "Delivery",
}
},
{
$unwind: {
"path": "$Productions",
preserveNullAndEmptyArrays: true
}
}, {
$unwind: {
"path": "$status",
preserveNullAndEmptyArrays: true
}
}, {
$unwind: {
"path": "$Goods",
preserveNullAndEmptyArrays: true
}
},
{
$unwind: {
"path": "$Inwards",
preserveNullAndEmptyArrays: true
}
},
{
$unwind: {
"path": "$Delivery",
preserveNullAndEmptyArrays: true
}
},
{
$unwind: {
"path": "$Despatch",
preserveNullAndEmptyArrays: true
}
},
{
$group: {
_id: null,
Inwards_Details: { $addToSet: { no: '$Inwards.no', grnNo: '$Inwards.grnNo', id: '$Inwards.id' } },
LabTest_Details: { $addToSet: { no: '$no', grnNo: '$grnNo', id: '$id' } },
Goods_Scheduler_Details: { $addToSet: { no: '$Goods.no', grnNo: '$Goods.grnNo', id: '$Goods.id' } },
Production_Details: { $addToSet: { no: '$Productions.no', grnNo: '$Productions.grnNo', id: '$Productions.id' } },
Despatch_Details: { $addToSet: { no: '$Despatch.no', grnNo: '$Despatch.grnNo', id: '$Despatch.id' } },
Delivery_Details: { $addToSet: { no: '$Delivery.no', grnNo: '$Delivery.grnNo', id: '$Delivery.id' } },
ProgressArray: {
$addToSet: {
Inwards_Status: "$Inwards_Details.status",
LabTest_Status: "$status",
Goodscheduler_Status: "$Goods.status",
Productions_Status: "$Productions.status",
Delivery_Status: "$Delivery.status",
Despatch_Status: "$Despatch.status"
}
},
},
},
{ $sort: { _id: 1 } },
{
$set: {
Inwards_Statuss: { $indexOfArray: ["$ProgressArray.Inwards_Status", "In-Progress"] }
}
},
{
$set: {
LabTest_Statuss: { $indexOfArray: ["$ProgressArray.LabTest_Status", "In-Progress"] }
}
},
{
$set: {
Goodscheduler_Statuss: { $indexOfArray: ["$ProgressArray.Goodscheduler_Status", "In-Progress"] }
}
},
{
$set: {
Productions_Statuss: { $indexOfArray: ["$ProgressArray.Productions_Status", "In-Progress"] }
}
},
{
$set: {
Deliveriess: { $indexOfArray: ["$ProgressArray.Delivery_Status", "In-Progress"] }
}
},
{
$set: {
Despatch_Statuss: { $indexOfArray: ["$ProgressArray.Despatch_Status", "In-Progress"] }
}
},
{
$set: {
ProgressArrays: {
Inwards_Status: {
$cond: {
if: {
$eq: ["$Inwards_Statuss", -1]
},
then: "Completed",
else: "In-Progress"
}
},
LabTest_Status: {
$cond: {
if: {
$eq: ["$LabTest_Statuss", -1]
},
then: "Completed",
else: "In-Progress"
}
},
Goods_Scheduler_Status: {
$cond: {
if: {
$eq: ["$Goodscheduler_Statuss", -1]
},
then: "Completed",
else: "In-Progress"
}
},
Productions_Status: {
$cond: {
if: {
$eq: ["$Productions_Statuss", -1]
},
then: "Completed",
else: "In-Progress"
}
},
Deliveries_Status: {
$cond: {
if: {
$eq: ["$Deliveriess", -1]
},
then: "Completed",
else: "Yet to be Started"
}
},
Despatch_Status: {
$cond: {
if: {
$eq: ["$Despatch_Statuss", -1]
},
then: "Completed",
else: "Yet to be Started"
}
},
}
}
},
{
$project: {
currentLocation: {
$cond: {
if: {
$eq: ["$ProgressArrays.Inwards_Status", "Completed"]
},
then: {
$cond: {
if: {
$eq: ["$ProgressArrays.LabTest_Status", "Completed"]
},
then: {
$cond: {
if: {
$eq: ["$ProgressArrays. Goods_Scheduler_Status", "Completed"]
},
then: {
$cond: {
if: {
$eq: ["$ProgressArrays.Productions_Status", "Completed"]
},
then: {
$cond: {
if: {
$eq: ["$ProgressArrays.Despatch_Status", "Completed"]
},
then: {
$cond: {
if: {
$eq: ["$ProgressArrays.Deliveries_Status", "Completed"]
},
then: "Finished",
else: "Delivery"
}
},
else: "Despatch"
}
},
else: "Production"
}
},
else: "Goods_Scheduler"
},
},
else: "LabTest"
},
},
else: "Inwards"
}
},
ProgressArray: "$ProgressArrays",
Inwards_Details: {
$cond: {
if: {
$eq: ["$Inwards_Details", [{}]]
},
then: [],
else: "$Inwards_Details"
}
},
LabTest_Details: {
$cond: {
if: {
$eq: ["$LabTest_Details", [{}]]
},
then: [],
else: "$LabTest_Details"
}
},
Goods_Scheduler_Details: {
$cond: {
if: {
$eq: ["$Goods_Scheduler_Details", [{}]]
},
then: [],
else: "$Goods_Scheduler_Details"
}
},
Production_Details: {
$cond: {
if: {
$eq: ["$Production_Details", [{}]]
},
then: [],
else: "$Production_Details"
}
},
Despatch_Details: {
$cond: {
if: {
$eq: ["$Despatch_Details", [{}]]
},
then: [],
else: "$Despatch_Details"
}
},
Delivery_Details: {
$cond: {
if: {
$eq: ["$Delivery_Details", [{}]]
},
then: [],
else: "$Delivery_Details"
}
}
}
}
]).allowDiskUse(true);;