[英]How to traverse the mongodb(JSON) document using queries
我的json文件是
{
"_id" : ObjectId("5b6049f845d12b6b62bf6fca"),
"original_query" : "",
}
我想遍歷每個在相同字段中的人,然后使用python將他們的fb_id存儲在列表中。
我是mnogodb和JSON的新手,任何領導和幫助建立必要的直覺將不勝感激。
編輯:代碼
import pymongo
from pymongo import MongoClient
import pprint
post=db.post
list_of_reactor_ids=[]
result=db.collection.find()
for doc in result:
list_of_reactor_ids=[]
for post in doc['posts']:
for reactor in like['reactors']:
list_of_reactor_ids.append(reactor[''])
print(list_of_reactor_ids)
你可以嘗試這樣:
client=MongoClient('mongodb://admin:Password1@localhost:27017/iitb')
db=client.iitb # this is you selecting the database 'iitb'
list_of_reactor_ids=[]
results = db.post.find() # 'post' is the name of collection
for doc in results:
list_of_reactor_ids = []
for post in doc['posts']:
if 'like' in post:
for reactor in post['like']['reactors']:
list_of_reactor_ids.append(reactor['fb_id'])
print(list_of_reactor_ids)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.