sachin
sachin

Reputation: 103

How can we retain only unique documents in mongodb aggregation

How can we retain all values even after using $group stage in case we need to use lookup in two collections. i.e suppose i have two sample collections col1

name: 'abc',
phoneNo:888######4,
vCode:20166,
dcode:30166,
city:'city1',
profession:'profession1'


name: 'name1',
phoneNo:999######1,
vCode:20167,
dcode:30166,
city:'city2',
profession:'profession2'


name: 'abc',
phoneNo:888######4,
vCode:20166,
dCode:30166
city:'city1'
profession:'profession1'

col2

name:'name1'
phoneNo:'999######1'
dCode:30166,
vCode:20167
profession:'profession2'

I have 3 docs in col1 out which 2 docs are duplicate.Now i needed to find out the docs which are not present in col2.Below is the query i have constructed.

db.col1.aggregate([
   { 
        $lookup: {
            from: "col2",
            let: {
                vCode: '$vCode',
                dCode: '$dCode',
            },
            pipeline: [
                {
                    $match: {
                        $expr: {
                            $and: [
                                { $eq: ["$dCode", "$$dCode"] },

                                { $eq: ["$vCode", "$$vCode"] }

                            ]
                        }
                    }
                },

            ],
            as: "col2"
        }
    },
    {
        $match: { col2: [] }
    }, {
        $project: {
           col2: 0
        }
    }
])

Now the above query does returns all the documents which are not present in col2. But as there are two duplicate documents in col1 hence the query returns both the documents too.Any suggestions how can i filter out duplicate documents and retain only unmatched and unique documents.

Upvotes: 1

Views: 86

Answers (1)

mickl
mickl

Reputation: 49945

You can $group by properties you consider as duplicated before you run your $lookup:

db.collection.aggregate([
    {
        $group: {
            _id: {
                name: "$name",
                phoneNo: "$phoneNo",
                vCode: "$vCode",
                dCode: "$dCode",
                city: "$city",
                profession: "$profession",
            }
        }
    },
    {
        $replaceRoot: {
            newRoot: "$_id"
        }
    }
    // your aggregation pipeline stages
])

Mongo Playground

Upvotes: 1

Related Questions