Untitled

 avatar
unknown
plain_text
9 months ago
4.9 kB
16
Indexable
const aggr: any = [
                {
                    $match: filter,
                },
                {
                    $lookup: {
                        from: AccountDatabaseName,
                        localField: "accountId",
                        foreignField: "_id",
                        as: "accountId",
                    },
                },
                {
                    $unwind: {
                        path: "$accountId",
                        preserveNullAndEmptyArrays: true,
                    },
                },
                { $match: accountFilters },
                // {
                //     $lookup: {
                //         from: AccountServiceDatabaseName,
                //         localField: "_id",
                //         foreignField: "entityId",
                //         as: "accountService",
                //     },
                // },

                // {
                //     $unwind: {
                //         path: "$accountService",
                //         preserveNullAndEmptyArrays: true,
                //     },
                // },
                // {
                //     $group: {
                //         _id: "$_id",
                //         doc: { $first: "$$ROOT" }, // keep only one merged document per entity
                //     },
                // },
                // {
                //     $replaceRoot: { newRoot: "$doc" },
                // },

                {
                    $lookup: {
                        from: AccountServiceDatabaseName,
                        let: { entityId: "$_id" },
                        pipeline: [
                            {
                                $match: {
                                    $expr: {
                                        $and: [
                                            {
                                                $eq: [
                                                    "$entityId",
                                                    "$$entityId",
                                                ],
                                            },
                                            // { $eq: ["$isActive", true] },
                                            // {
                                            //     $eq: [
                                            //         "$projectId", PROJECT_ID
                                            //     ],
                                            // },
                                            // {
                                            //     $eq: [
                                            //         "$partnerId", PARTNER_ID
                                            //     ],
                                            // },
                                        ],
                                    },
                                },
                            },
                        ],
                        as: "accountService",
                    },
                },
                {
                    $unwind: {
                        path: "$accountService",
                        preserveNullAndEmptyArrays: true,
                    },
                },
                {
                    $group: {
                        _id: "$_id",
                        doc: { $first: "$$ROOT" },
                    },
                },
                {
                    $replaceRoot: { newRoot: "$doc" },
                },

                // {
                //     $lookup: {
                //         from: AccountServiceBillingPlanDatabaseName,
                //         localField: "accountService._id",
                //         foreignField: "accountServiceId",
                //         as: "serviceBillPlan",
                //     }
                // },
                // {
                //     $unwind: {
                //         path: "$serviceBillPlan",
                //         preserveNullAndEmptyArrays: true,
                //     }
                // },

                // {
                //     $project: {
                //         _id: 1,
                //         name: 1,
                //         code: 1,
                //         uidNo: 1,
                //         updatedAt: 1,
                //         createdAt: 1,
                //         accountId: { _id: 1, name: 1, accountIdNo: 1 },
                //     },
                // },
                {
                    $facet: {
                        data: [{ $skip: skip }, { $limit: limit }],
                        totalCount: [{ $count: "count" }],
                    },
                },
            ];
Editor is loading...
Leave a Comment