Kane
Kane

Reputation: 615

Aggregation if local field exist in foreign field

I am using MeteorJS. Now I am trying to fetch data by using meteor call method. It's working well. But I have $lookup for aggregation it's also working fine. Now I am trying to only fetch data by unique, no need duplicate.

[![Meteor.methods({
        allIndications(someId) {
            const indications = Promise.await(
                Medicines.aggregate(\[
                    {
                        $lookup: {
                            from: "indications",
                            localField: "medicine_indications",
                            foreignField: "_id",
                            as: "AllIndications"
                        }
                    },
                    {
                        $unwind: {
                            path: "$AllIndications",
                            preserveNullAndEmptyArrays: true
                        }
                    },

                    { $project: { _id: 1, AllIndications: 1 } }
                \]).toArray()
            );
            return indications;
        }
    });][1]][1]

enter image description here

Upvotes: 0

Views: 47

Answers (1)

Jits
Jits

Reputation: 545

You can try this

[![Meteor.methods({
    allIndications(someId) {
        const indications = Promise.await(
            Medicines.aggregate(\[
                {
                    $lookup: {
                        from: "indications",
                        localField: "medicine_indications",
                        foreignField: "_id",
                        as: "AllIndications"
                    }
                },
                {
                    $unwind: {
                        path: "$AllIndications",
                        preserveNullAndEmptyArrays: true
                    }
                },
                {
                     $group:{
                         _id:null,
                         AllIndications:{$addToSet: "$AllIndications"}
                     }
                },
                { $project: { _id: 1, AllIndications: 1 } }
            \]).toArray()
        );
        return indications;
    }
});][1]][1]

Upvotes: 1

Related Questions