MongoDb duplicate query

82 views
Skip to first unread message

Suchita Hota

unread,
Aug 7, 2018, 6:19:17 PM8/7/18
to mongodb-user
Hi!
I wrote a query to find duplicate records in mongodb 

 db.ls_census_user_mapping.aggregate([{$group : { _id: "$ref_ward_id" , count : { $sum: 1}}},{$match : { count : { $gt : 1 } }} ])

In this query,i wish to find duplicate records for column 'ref_ward_id' but this query only shows the count of no of duplicates of a particular ref_ward_id and I need help in getting all the data or columns related to the particular ref_ward_id.

lian....@mongodb.com

unread,
Aug 22, 2018, 4:45:32 AM8/22/18
to mongodb-user

Hi Suchita,


To output associated fields or columns related to ref_ward_id, you can use $lookup in the aggregation.

$lookup (aggregation) stage has the following syntax :

{

   $lookup:

     {

       from: <collection to join>,

       localField: <field from the input documents>,

       foreignField: <field from the documents of the "from" collection>,

       as: <output array field>

     }

}


For example, in your case:

db.ls_census_user_mapping.aggregate([

{ $group: { _id: "$ref_ward_id" , count : { $sum: 1 }}

},

{ $match: { count : { $gt : 1 }}

},

{ $lookup:

{ from: "census_user_mapping", localField: "_id", foreignField: "ref_ward_id", as: "new_dup" }

}

])

Where:

census_user_mapping is the collection you are finding duplicate records from.

“_id” is the local field to use and foreignField is “ref_ward_id”.

new_dup is the field which will display all data related to ref_ward_id in an array.


Regards,

Lian

Reply all
Reply to author
Forward
0 new messages