【问题标题】:incorrect result with showing the number of occurrence for a specific field in mongodb python显示 mongodb python 中特定字段的出现次数的结果不正确
【发布时间】:2022-01-10 01:28:56
【问题描述】:

我希望显示每个 first_name 及其出现次数我该怎么做 这是我尝试过的代码

v=school.aggregate(
    [ {'$group' : { '_id': '$first_name', 'count' : {'$sum' : 1}}} ]
    )
    
for x in v:

    pprint(x) 

但结果不正确 它告诉我这个

{'_id': None, 'count': 955180}

【问题讨论】:

  • 你想知道名字出现了多少次?
  • 是的,每个不同的名字
  • 名字是名字列表还是字符串?
  • 不是一个列表只是一个字符串

标签: python mongodb pymongo


【解决方案1】:

您可以找到所有包含名字的 ID,然后获取列表的大小:

FirstNameCount = len(list(school.aggregate.find({"_id": first_name})))

【讨论】:

猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-09-13
  • 2021-10-07
  • 1970-01-01
  • 2019-07-25
  • 2015-10-30
相关资源
最近更新 更多