Removing duplicate records using MapReduce

后端 未结 4 1207
借酒劲吻你
借酒劲吻你 2020-12-10 06:09

I\'m using MongoDB and need to remove duplicate records. I have a listing collection that looks like so: (simplified)

[
  { \"MlsId\": \"12345\"\" },
  { \"M         


        
4条回答
  •  时光取名叫无心
    2020-12-10 06:43

    You can use aggregation operation to remove duplicates. Unwind, introduce a dummy $group and $sum stage and ignore the counts in your next stage. Something like this,

    db.myCollection.aggregate([
     {
         $unwind: '$list'
     },
     {
        $group:{
       '_id':
           {
             'listing_id':'$_id', 'MlsId':'$list.MlsId'
           },
              'count':
           {
              '$sum':1
           }
          }
    },
    {
          $group:
           {
            '_id':'$_id.listing_id',
            'list':
             {
              '$addToSet':
               {
                'MlsId':'$_id.MlsId'
               }
             }
           }
    }
    ]);
    

提交回复
热议问题