Dumb
Dumb

Reputation: 43

How to traverse the mongodb(JSON) document using queries

My json document is

{
"_id" : ObjectId("5b6049f845d12b6b62bf6fca"),
"original_query" : "",
}

I want to traverse each person who are in like field and then store their fb_id in a list using python.

I am new to mnogodb and JSON and any leads and help in building the necessary intution would be appreciated.

Edit:Code

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)

Upvotes: 1

Views: 301

Answers (1)

tonysepia
tonysepia

Reputation: 3510

you can try it like so:

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)

Upvotes: 3

Related Questions