как объединить результаты в агрегирование mon go db? - PullRequest
0 голосов
/ 01 апреля 2020

У меня есть 4 коллекции в базе данных. Мне нужно присоединиться к этим коллекциям при различных условиях. Для этого я использую совокупную структуру. Я получаю следующий результат

 [
        {
            "_id": "5e8469bd48b0853468db6836",
            "Goods": {
                "no": "6 - 20/21",
                "grnNo": "17 - 20/21",
                "id": "28a82510-6f26-11ea-86ba-8fc4c02180c6"
            },
            "ProgressArray": {
                "LabTests_Status": "In-Progress",
                "Inwards_Status": "In-Progress",
                "Goods_Status": "In-Progress",
                "Productions_Status": "Completed"
            },
            "Inwards": [
                {
                    "grnNo": "17 - 20/21",
                    "id": "f358f0b0-70e7-11ea-9d6d-e31c8d952ef7"
                }
            ],
            "Production": {
                "no": "48 - 20/21",
                "grnNo": "17 - 20/21",
                "id": "68b93e70-7267-11ea-9a01-c7602a734a9a"
            },
            "LabTest": [
                {
                    "no": "68 - 20/21",
                    "grnNo": "17 - 20/21",
                    "id": "32740cf0-6f29-11ea-90b2-c18de169456e"
                }
            ],
            "currentLocation": "LabTest"
        },
        {
            "_id": "5e8469bd48b0853468db6836",
            "Goods": {
                "no": "6 - 20/21",
                "grnNo": "17 - 20/21",
                "id": "28a82510-6f26-11ea-86ba-8fc4c02180c6"
            },
            "ProgressArray": {
                "LabTests_Status": "In-Progress",
                "Inwards_Status": "In-Progress",
                "Goods_Status": "In-Progress",
                "Productions_Status": "Completed"
            },
            "Inwards": [
                {
                    "grnNo": "17 - 20/21",
                    "id": "f358f0b0-70e7-11ea-9d6d-e31c8d952ef7"
                }
            ],
            "Production": {
                "no": "49 - 20/21",
                "grnNo": "17 - 20/21",
                "id": "68b93e70-7267-11ea-9a01-c7602a734a9a"
            }
            "LabTest": [
                {
                    "no": "68 - 20/21",
                    "grnNo": "17 - 20/21",
                    "id": "32740cf0-6f29-11ea-90b2-c18de169456e"
                }
            ],
            "currentLocation": "LabTest"
        }
    ]

Мне нужно объединить производственные значения в один массив результатов. Вот мой ожидаемый результат

[
    {
        "_id": "5e8469bd48b0853468db6836",
        "Goods": {
            "no": "6 - 20/21",
            "grnNo": "17 - 20/21",
            "id": "28a82510-6f26-11ea-86ba-8fc4c02180c6"
        },
        "ProgressArray": {
            "LabTests_Status": "In-Progress",
            "Inwards_Status": "In-Progress",
            "Goods_Status": "In-Progress",
            "Productions_Status": "Completed"
        },
        "Inwards": [
            {
                "grnNo": "17 - 20/21",
                "id": "f358f0b0-70e7-11ea-9d6d-e31c8d952ef7"
            }
        ],
        "Production": [{
            "no": "48 - 20/21",
            "grnNo": "17 - 20/21",
            "id": "68b93e70-7267-11ea-9a01-c7602a734a9a"
        },
         {
            "no": "49 - 20/21",
            "grnNo": "17 - 20/21",
            "id": "68b93e70-7267-11ea-9a01-c7602a734a9b"}],
        "LabTest": [
            {
                "no": "68 - 20/21",
                "grnNo": "17 - 20/21",
                "id": "32740cf0-6f29-11ea-90b2-c18de169456e"
            }
        ],
        "currentLocation": "LabTest"
    }]

Вот мой код

async function getGrnDetails(userParam) {

    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",
            }
        },
        {
            $unwind: {
                "path": "$Productions",
                preserveNullAndEmptyArrays: true
            }
        }, {
            $unwind: {
                "path": "$status",
                preserveNullAndEmptyArrays: true
            }
        }, {
            $unwind: {
                "path": "$Goods",
                preserveNullAndEmptyArrays: true
            }
        },
        {
            $unwind: {
                "path": "$Inwards",
                preserveNullAndEmptyArrays: true
            }
        }, {

            $project: {
                ProgressArray: {
                    LabTests_Status: "$status",
                    Inwards_Status: "$Inwards.status",
                    Goods_Status: "$Goods.status",
                    Productions_Status: "$Productions.status",
                },



                Goods: { no: '$Goods.no', grnNo: '$Goods.grnNo', id: '$Goods.id' },
                Inwards: [{ no: '$Inwards.no', grnNo: '$Inwards.grnNo', id: '$Inwards.id' }],
                Production: { no: '$Productions.no', grnNo: '$Productions.grnNo', id: '$Productions.id' },
                LabTest: [{
                    no: '$no',
                    grnNo: '$grnNo',
                    id: '$id'
                }],

                currentLocation: {

                    $cond: {
                        if: {
                            $eq: ["$status", "Completed"]
                        },
                        then: {
                            $cond: {
                                if: {
                                    $eq: ["$Inwards.status", "Completed"]
                                },
                                then: {
                                    $cond: {
                                        if: {
                                            $eq: ["$Goods.status", "Completed"]
                                        },

                                        then: {
                                            $cond: {
                                                if: {
                                                    $eq: ["$Productions.status", "Completed"]
                                                },

                                                then: "Finished",
                                                else: "Productions"
                                            }
                                        },
                                        else: "Goods"
                                    }
                                },
                                else: "Inwards"
                            }
                        },
                        else: "LabTest"

                    }
                },
            }
        },


        { $sort: { _id: 1 } },
        { $limit: 2 }

    ]);


    return await userss
}

Я только новичок. Я не знаю как это сделать. Пожалуйста, помогите мне с этим вопросом

1 Ответ

0 голосов
/ 09 апреля 2020

Прежде чем я пошел в производство, мне просто нужно было сохранить в определенном массиве и показать все, поэтому я использовал метод 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);;
...