Home > database >  I Made Tow function in pymongo but the out but that i want is different from i get from the function
I Made Tow function in pymongo but the out but that i want is different from i get from the function

Time:05-24

Funtion that save Close,Symbol, Timeframe

    def Save_(self,collection,symbol,price,TF):
    db = self.get_db('MTF')[collection]
    B = {'ts':time.time(),"Symbol":symbol,
                    "Price":price,'TimeFrame':TF}
    data = db.insert_one(B)
    return data

Function to get data from mongodb

def find_all(self,collection):
    db = self.get_db('MTF')[collection]
    Symbols ={}
    data = db.find({})
    for i in data:
        Symbols[i['Symbol']] = [i['Price'],i['TimeFrame']]
    return Symbols

images from mongodb [2]: https://i.stack.imgur.com/RLtnz.png

images from B Function [1]: https://i.stack.imgur.com/AtwSy.png

if u see the image from Function B only gave me on timeframe but Function Save have 4 timeframe

CodePudding user response:

Looking at this loop:

for i in data:
    Symbols[i['Symbol']] = [i['Price'],i['TimeFrame']]

If you have the same Symbol coming from MongoDB, it will overwrite any previous value, so you will only get the final value for each Symbol which is what you are seeing.

To fix it you have a few options: you could check the key and either create or append the values to Symbols; or you could use $push in an aggregate query.

  • Related