gpt4 book ai didi

python - 在 Mongodb 中聚合的有效方法

转载 作者:可可西里 更新时间:2023-11-01 10:03:38 27 4
gpt4 key购买 nike

我有一个收藏

{
"name" : "foo"
"clicked" : {"0":6723,"1": 1415,"2":1122}
}
{
"name" : "bar"
"clicked" : {"8":1423,"9": 1415,"10":1122}
}
{
"name" : "xyz"
"clicked" : {"22":6723,"23": 1415,"2":1234}
}

clicked 基本上是 {"position of item-clicked in the list": "id of the item"}

我想要的最终输出是一个项目被点击的总次数,即上面示例的以下内容:

    {
6723:2,
1415:3,
1423:1,
1122:2,
1234:1
}

一种方法是在内存中维护一个字典(在 python 脚本中)并在每个文档中查找 “clicked” 字段以更新字典。我是 mongo 的新手,请帮忙!

最佳答案

使用collections.Counter

In [58]: import pymongo

In [59]: from collections import Counter

In [61]: conn = pymongo.MongoClient()

In [62]: db = conn.test

In [63]: col = db.collection

In [64]: result = col.aggregate([{"$group": {"_id": None, "clicked": {"$push": "$clicked"}}}]).next()['clicked']

In [65]: c = Counter()

In [66]: for el in [Counter(i.values()) for i in result]:
....: c += el
....:

In [67]: print(dict(c))
{1122: 2, 6723: 2, 1415: 3, 1234: 1, 1423: 1}

关于python - 在 Mongodb 中聚合的有效方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30665421/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com