【问题标题】:MongoDB aggregation returning duplicated resultsMongoDB 聚合返回重复的结果
【发布时间】:2020-06-22 18:33:54
【问题描述】:

我第一次使用 MongoDB 聚合结果时遇到了一些麻烦。我正在尝试汇总一些销售总额,但它返回重复的,甚至是三次重复的结果,结果数据相同。有人能弄清楚我做错了什么吗?

这是一个示例数据行:

{
        "owner": "Anitta",
        "owner_id": ObjectId("5e0e25fc3ed15e27c5f5bb2b"),
        "units": NumberInt(19802),
        "percent": 0.35,
        "value": 30.35063004976345,
        "artist_name": "Warner",
        "release_title": "Carol",
        "upc": "3615937741130",
        "track_title": "Carol",
        "isrc": "BR9Z21900056",
        "store_name": "Spotify",
        "country": "Brazil",
        "label_name": "Warner Music",
        "distributor": "Believe",
        "sale_type": "audio",
        "sales_id": ObjectId("5e5e91ac238279bd2ba7e839"),
        "created_at": ISODate("2020-03-11T20:18:11.076-03:00"),
        "report_name": "202002-believe.csv",
        "sales_date": ISODate("2019-11-01T21:00:00.000-03:00"),
        "distributor_date": ISODate("2020-02-01T00:00:00.000-02:00"),
        "distributor_value": 5.355993538193549,
    }

这是聚合查询:

filters = { "$match": { owner: { $nin: [false, '', null] } } };
query.push(filters);

query.push({
                    $group: {
                        _id: {"owner": "$owner", "owner_id": "$owner_id"},
                        total_units: { $sum: "$units" },
                        total: { $sum: "$value" },
                    }
                })
                query.push({ $sort: { "total": -1 } })

这是我得到的结果:

{
    data: [
        {
            _id: {
                owner: "Anitta",
                owner_id: "5e0e25fc3ed15e27c5f5bb2b"
            },
            total_units: 8127500,
            total: 10163.241069212721
        },
      
        {
            _id: {
                owner: "Anitta",
                owner_id: "5e0e25fc3ed15e27c5f5bb2b"
            },
            total_units: 5851785,
            total: 2952.923583543785
        },
    ]
}

【问题讨论】:

  • 您似乎只提供了部分查询。
  • 查询的另一部分只是一个$match。无论如何,我已将其添加到问题中。谢谢!
  • 为什么不只在 owner_id 上分组?
  • 查询似乎没问题。在 mongo shell 中运行,是否返回预期结果?
  • @CodyGI 使用owner 将其显示在列表中。

标签: node.js mongodb


【解决方案1】:

我找到了问题和解决方案!

问题是我将owner_id 保存为stringObjectId。这会产生分裂的结果。

对于遇到同样问题的人,我建议将保存的引用从string 修改为ObjectId,因为它在数据库中占用的空间更少。您可以查看此线程以更好地理解:MongoDb: Benefit of using ObjectID vs a string containing an Id?

所以,如果你遇到这个问题,你可以这样做:

db.distribution.aggregate(
    // FILTER TO GET ONLY THE IDs SAVED AS STRING
    {$match: {owner_id : {$type : 2}}},
    
    // GET THE NECESSARY DATA
    {$group: {_id: {
        owner_id: "$owner_id",
        owner: "$owner",
    }}}
    )
.limit(1000)
.forEach(function(d) {
    // TRANSFORM THE STRING INTO OBJECTID
    var id = ObjectId(d._id.owner_id); 
    
    // THE STRING ID
    var oldId = d._id.owner_id; 
    
    // DISPLAY SOMETHING TO MONITOR
    console.log(d._id.owner, id, oldId)
    
    // UPDATE THE ENTRIES
    var ret = db.distribution.updateMany({
        owner_id: oldId,
    },
    {
        $set: {
            owner_id: id
        }
    });
    
    // JUST TO MONITOR THE RESULTS
    console.log(ret)
})

【讨论】:

    猜你喜欢
    • 2021-09-20
    • 2015-08-04
    • 2019-07-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-11-26
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多