我正在遍历mongo文档
{
"_id" : ObjectId("5b39cbd94a6"),
"original_query" : "az",
"posts" : [
"comments" : [
{
"comment" : "",
"commentator" : "Abd",
"fb_id" : "1000"
},
{
"comment" : "Sharjeel Usmani Alig Haider Ali Haider Saifullah Naseem Akhtar Alig Haider Ali Tanveer Khan Alig Imran Choudhary Nizampur Tarique Anwar Champarni",
"commentator" : "Hi",
"fb_id" : "1000"
}],
"like" : {
"total" : "289",
"reactors" : [
{
"profile" : "",
"name" : "Ad ",
"fb_id" : "1000"
},
{
"profile" : "https://m.facebook.com",
"name" : "She ",
"fb_id" : "100"
}
]
]
}
我想计算注释部分的单词数,但我什至无法遍历注释部分。以下是我的方法。
result=db.post.find().limit(3)
for post in doc['posts']:
for com in post['comments'] :
pprint.pprint(com['comment'])
但是我在第二个for循环中遇到关键错误。
编辑: 所需的输出:
Sharjeel Usmani Alig Haider Ali Haider Saifullah Naseem Akhtar Alig Haider Ali Tanveer Khan Alig Imran Choudhary Nizampur Tarique Anwar Champarni"
编辑: 我已经使用
访问了喜欢字段的IDfor post in doc['posts']:
if 'like' in post:
for reactor in post['like']['reactors']:
list_of_reactor_ids.append(reactor['id'])