我正在尝试迭代以下mongoDB(json),但它并没有完全遍历它,而只是遍历了有限的查询。
{
"_id" : ObjectId("5b7fb2a745d12b1e8bb20d84"),
"date_time" : "24, Aug 2018 12:52",
"original_query" : "harsh.shah.4949",
"posts" : [
{
"full_story" : "https://m.facebook.com//story.php?story_fbid=1875197722542135&id=100001557751449&refid=17",
"love" : {
"total" : "7",
"reactors" : [
{
"profile" : "https://m.facebook.com//AMITTIWARIITGN",
"name" : "Amit T",
"fb_id" : "100025139550829"
},
]
},
"like" : {
"total" : "93",
"reactors" : [
{
"profile" : "https://m.facebook.com//AMITTIWARIITGN",
"name" : "Amit T",
"fb_id" : "100025139550829"
},
]
},
"comments" : [
{
"comment" : "Super",
"commentator" : "Waseem Alamturki",
"fb_id" : "100027080688922"
}
],
"fb_id" : "100000107208292"
}
我的方法是-
result = db.post.find()
for doc in result:
for post in doc['posts']:
list_of_reactor_ids = []
dict_of_reactor_ids = Counter()
sorted_dict_of_reactor_ids = {}
我只是计算一个特定的fb_id喜欢/笑/爱的次数...该帖子,然后添加该数量并将其存储在字典中,但是我无法获得完整的列表,并且有很多条目最终结果中缺少
if 'like' in post:
value = 1
for reactor in post['like']['reactors']:
dict_of_reactor_ids.update({reactor['fb_id']: value})
if 'laugh' in post:
value = 2
for reactor in post['laugh']['reactors']:
dict_of_reactor_ids.update({reactor['fb_id']: value})
if 'love' in post:
value = 2
for reactor in post['love']['reactors']:
dict_of_reactor_ids.update({reactor['fb_id']: value})
在这里,我将字典递减排序,然后将字典存储在另一个字典“ okey”中,其关键字为fb_id,随后的反应堆都位于该字典中。
sorted_dict_of_reactor_ids = sorted(dict_of_reactor_ids.items(),
key=lambda dict_of_reactor_ids: dict_of_reactor_ids[1], reverse=True)
if 'fb_id' in doc:
okey.update({doc['fb_id']: sorted_dict_of_reactor_ids})
pprint.pprint(okey)
但是,我得到的输出几乎缺少许多值,并且不完整。 附言:我尝试在此处粘贴非常有限的代码,因此如果不清楚,请询问任何问题。