我怎么能从数组中删除重复的项目(复杂的对象)

时间:2015-06-03 07:31:00

标签: mongodb aggregation-framework

在每份文件中

records是一个包含许多重复对象的数组。

并且在buy_items中还包含许多重复的项目。

我如何清理重复的项目?

原始文件

{
  "_id": "0005d116qwwewdq82a1b84f148fa6027d429f3e",
  "records": [
    {
      "DATE": new Date("1996-02-08T08:00:00+0800"),
      "buy_items": [
        "5210 ",
        "5210 ",
        "5210 "
      ]
    },
    {
      "DATE": new Date("1996-02-08T08:00:00+0800"),
      "buy_items": [
        "5210 ",
        "5210 ",
        "5210 "
      ]
    }
    {
      "DATE": new Date("2012-12-08T08:00:00+0800"),
      "buy_items": [
        "5210 ",
        "1234 ",
        " "
      ]
    }        
    ]
}

预期输出

{
  "_id": "0005d116qwwewdq82a1b84f148fa6027d429f3e",
  "records": [
    {
      "DATE": new Date("1996-02-08T08:00:00+0800"),
      "buy_items": [
        "5210 "
      ]
    },
    {
      "DATE": new Date("2012-12-08T08:00:00+0800"),
      "buy_items": [
        "5210 ",
        "1234 ",
        " "
      ]
    }    
    ]
}

使用Michaels解决方案,输出可能如下所示

{
  "_id": "0005d116qwwewdq82a1b84f148fa6027d429f3e",
  "records": [
    "date": new Date("1996-02-08T08:00:00+0800"),
      "buy_items": [
        "5210 "
        "1234 ",
        " "
      ]
    ]
}

3 个答案:

答案 0 :(得分:3)

您可以使用aggregation framework

删除重复的对象
db.collection.aggregate(
    [
        { $unwind: "$records" }, 
        { $unwind: "$records.buy_items" }, 
        { $group: { "_id": {id: "$_id", date: "$records.DATE" }, buy_items: { $addToSet: "$records.buy_items" }}}, 
        { $group: {"_id": "$_id.id", records: { $push: {"date": "$_id.date", "buy_items": "$buy_items" }}}}, { $sort: { "records.0.date": 1 }} ,
        { $out: "collection" }
    ]
)

$out运算符允许您在指定集合中编写汇总结果,或替换您现有的集合。

使用"Bulk"操作

更好
var bulk = bulk = db.collection.initializeOrderedBulkOp(),
    count = 0;

db.collection.aggregate([
    { "$unwind": "$records" }, 
    { "$project": { 
        "date": "$records.DATE", 
        "buy_items": { "$setIntersection": "$records.buy_items" }
    }}, 
    { "$unwind": "$buy_items" }, 
    { "$group": { 
        "_id": { "id": "$_id", "date": "$date" }, 
        "buy_items": { "$addToSet": "$buy_items" }
    }},
    { "$group": { 
        "_id": "$_id.id", 
        "records": { "$push": { 
            "date": "$_id.date", 
            "buy_items": "$buy_items" 
        }}
    }}
]).forEach(function(doc) { 
       bulk.find({"_id": doc._id}).updateOne({
       "$set": { "records": doc.records }
       }); 
       count++; 
       if (count % 500 == 0) {   
           bulk.execute();    
           bulk = db.collection.initializeOrderedBulkOp(); 
       } 
})

if (count % 500 != 0)
    bulk.execute();

结果:

{
    "_id" : "0005d116qwwewdq82a1b84f148fa6027d429f3e",
    "records" : [
            {
                    "date" : ISODate("2012-12-08T00:00:00Z"),
                    "buy_items" : [
                            " ",
                            "1234 ",
                            "5210 "
                    ]
            },
            {
                    "date" : ISODate("1996-02-08T00:00:00Z"),
                    "buy_items" : [
                            "5210 "
                    ]
            }
    ]
}

答案 1 :(得分:1)

如果要更新当前集合而不创建新集合并删除以前的集合。我试过这个但是这样做你应该运行两个不同的更新命令。

首先使用records更新distinct,如下所示:

db.collectionName.update({},{"$set":{"records":db.collectionName.distinct('records')}})

buy_items的{​​{1}}的第二次更新,如下所示:

distinct

如果您想避免两次更新查询,请按照 Michael 回答。

答案 2 :(得分:1)

您可以尝试使用forEach()游标的find()方法迭代每个文档属性,检查唯一性并按如下方式过滤不同的值:

db.collection.find().forEach(function(doc){
    var records = [], seen = {};
    doc.records.forEach(function (item){
         var uniqueBuyItems = item["buy_items"].filter(function(i, pos) {
            return item["buy_items"].indexOf(i) == pos;
         });
         item["buy_items"] = uniqueBuyItems;
         if (JSON.stringify(item["buy_items"]) !== JSON.stringify(seen["buy_items"])) {
            records.push(item);
            seen["buy_items"] = item["buy_items"];
         }         
    }); 
    doc.records = records;
    db.collection.save(doc);
})