Search code examples
mongodbpython-2.7yahoo-finance

iterating over a list of values of single key in a collection in mongodb


i have a collection of financial data stored in mongodb. each company symbol has its data. the question is how to iterate over the collection and change the value of the key which is the symbol company to print out the whole collection and this is my list of companies ['TSLA','TYO','C','LULU','DTV','SHS',' ZNGA'] and this is my cod which return the data of one company:

   from pymongo import MongoClient
   import csv
   host = "localhost"
   port = 27017
   databaseName = "finance000"
   collection_name = "income"
   client = MongoClient(host, port)
   database = client[databaseName]
   collection = database[collection_name]
   def finance_data(symbol):
        Earnings_BeforeInterestAndTaxes = symbol['statement'[0]EarningsBeforeInterestAndTaxes']['content']                                               
       Total_Revenue = symbol['statement'][0]['TotalRevenue']['content']
       return Earnings_BeforeInterestAndTaxes,Total_Revenue
i =collection.find({'symbol':'C'})

with open('D:/INCOMEdata.csv', "w") as output:
writer = csv.writer(output, lineterminator='\n')
for key  in i :
    print finance_data(key)

    writer.writerow(finance_data(key)) 

Solution

  • If I understood you correctly. You want to retrieve the document/data for a given company. The company is denoted by a symbol example 'TYSLA'.

    If that is what you need. Here is how you do it (assuming each symbol is unique)

    company_data = collection.find({'symbol':'TYSLA'})
    

    The above will return a dictionary. To access an element within the document you just use:

    company_data['profit'] #profit is an example of an attribute name. You can use any attribute you like.
    

    Assuming you have multiple companies with the same symbol. If you used the above command. you will get a cursor. to get each company just do a for loop example:

    company_data = collection.find({'symbol':'TYSLA'})
    

    Now to loop:

    for one in company_date:
         print one['profit'] #python 2.7
    

    Now to edit the say for example the profit attribute use $set

    collection.update_one({'symbol':'TYSLA'},{'profit':100})
    

    the above will change TYSLA's company profit to 100

    Update

    Assuming you have a collection with the symbol being any value of ['TSLA','TYO','C','LULU','DTV','SHS',' ZNGA']. to get the data for any of the symbols you use (assuming symbol contains any of the names (only one name)):

    you use the $or as:

    collection.find({"$or":[ {'symbol':'TSLA},{'symbol':'TYO},... ]},{}) #the ... are the rest of the names you need 
    

    The above returns the whole data for a given symbol. To return the specifics Total_Revenue and Earnings_BeforeInterestAndTaxes you use the following:

    collection.find({"$or":[ {'symbol':'TSLA},{'symbol':'TYO},... ]},{'Total_Revenue ':1,'Earnings_BeforeInterestAndTaxes ':1, _id:0 }) #if you remove _id it will always be returned
    

    I hope that helps.