Search code examples
pandasmongodbtime-seriespymongoccxt

How can I query unknown values from arrays in mongodb


I am stuck and don't know exactly what to do I have a mongodb server that stores open high low close volume from a pandas dataframe I am trying to figure out how I can query every single document and get just the values without specifying the datetime stamp. I am new to mongodb and not entirely sure what to do



    "_id" : ObjectId("5d7d5aa984323fa67c2e9002"),
    "exchange" : "binance",
    "instrument" : "XRPUSDT",
    "timeframe" : "1d",
    "candles" : {
        "2019-09-06:0000" : {
            "open" : 0.25616,
            "high" : 0.25868,
            "low" : 0.24692,
            "close" : 0.2511,
            "volume" : 63377736.0
        },
        "2019-09-07:0000" : {
            "open" : 0.25115,
            "high" : 0.26285,
            "low" : 0.25009,
            "close" : 0.25993,
            "volume" : 53971229.0
        },
        "2019-09-08:0000" : {
            "open" : 0.25989,
            "high" : 0.26591,
            "low" : 0.2555,
            "close" : 0.26205,
            "volume" : 65033003.0
        }

 "_id" : ObjectId("5d7d74925bff7734c6c348a0"),
    "exchange" : "binance",
    "instrument" : "XRPUSDT",
    "timeframe" : "1d",
    "candles" : {
        "2019-09-06:0000" : {
            "open" : 0.25616,
            "high" : 0.25868,
            "low" : 0.24692,
            "close" : 0.2511,
            "volume" : 63377736.0
        },
        "2019-09-07:0000" : {
            "open" : 0.25115,
            "high" : 0.26285,
            "low" : 0.25009,
            "close" : 0.25993,
            "volume" : 53971229.0
        },
        "2019-09-08:0000" : {
            "open" : 0.25989,
            "high" : 0.26591,
            "low" : 0.2555,
            "close" : 0.26205,
            "volume" : 65033003.0
        }

for example i want the value for close in every document how can i query mongodb in python3 to just return something like ["close": 0.2511, 0.25993, 0.26205, 0.2511, 0.25993, 0.26205]

and also get all timestamps from every document like [2019-09-06:0000, 2019-09-07:0000, 2019-09-08:0000, 2019-09-06:0000,2019-09-06:0000, 2019-09-07:0000, 2019-09-08:0000]


Solution

  • The key (if you excuse the pun) to this is .items() which allows you to get the key, value pairs . After this, everything else is just dictionary operators which you can manipulate as needed.

    import pymongo
    
    db = pymongo.MongoClient()['mydatabase']
    
    db.pricedata.insert_one({
        "exchange": "binance",
        "instrument": "XRPUSDT",
        "timeframe": "1d",
        "candles": {
            "2019-09-06:0000": {
                "open": 0.25616,
                "high": 0.25868,
                "low": 0.24692,
                "close": 0.2511,
                "volume": 63377736.0
            },
            "2019-09-07:0000": {
                "open": 0.25115,
                "high": 0.26285,
                "low": 0.25009,
                "close": 0.25993,
                "volume": 53971229.0
            },
            "2019-09-08:0000": {
                "open": 0.25989,
                "high": 0.26591,
                "low": 0.2555,
                "close": 0.26205,
                "volume": 65033003.0
            }
        }
    })
    db.pricedata.insert_one(
        {
            "exchange": "binance",
            "instrument": "XRPUSDT",
            "timeframe": "1d",
            "candles": {
                "2019-09-06:0000": {
                    "open": 0.25616,
                    "high": 0.25868,
                    "low": 0.24692,
                    "close": 0.2511,
                    "volume": 63377736.0
                },
                "2019-09-07:0000": {
                    "open": 0.25115,
                    "high": 0.26285,
                    "low": 0.25009,
                    "close": 0.25993,
                    "volume": 53971229.0
                },
                "2019-09-08:0000": {
                    "open": 0.25989,
                    "high": 0.26591,
                    "low": 0.2555,
                    "close": 0.26205,
                    "volume": 65033003.0
                }
            }
        }
    )
    
    looking_for = 'close'
    for record in db.pricedata.find({}, {"candles": 1, "_id": 0}):
        for k, v in record['candles'].items():
            print (f'{k}: {v[looking_for]}')
    

    Result:

    2019-09-06:0000: 0.2511
    2019-09-07:0000: 0.25993
    2019-09-08:0000: 0.26205
    2019-09-06:0000: 0.2511
    2019-09-07:0000: 0.25993
    2019-09-08:0000: 0.26205