Pandas 如何从mongodb中的数组中查询未知值

Pandas 如何从mongodb中的数组中查询未知值,pandas,mongodb,time-series,pymongo,ccxt,Pandas,Mongodb,Time Series,Pymongo,Ccxt,我被卡住了,不知道该怎么办我有一个mongodb服务器,它存储来自pandas数据帧的打开-高-低-关闭卷我试图弄清楚如何查询每个文档,只获取值而不指定日期戳。我是mongodb的新手,不知道该怎么做 “_id”:ObjectId(“5d7d5aa984323fa67c2e9002”), “交换”:“二进制”, “文书”:“XRPUDTT”, “时间框架”:“1d”, “蜡烛”:{ "2019-09-06:0000" : { “开放”:0.25616, “高”:0.25868, “低”:0.2

我被卡住了,不知道该怎么办我有一个mongodb服务器,它存储来自pandas数据帧的打开-高-低-关闭卷我试图弄清楚如何查询每个文档,只获取值而不指定日期戳。我是mongodb的新手,不知道该怎么做


“_id”:ObjectId(“5d7d5aa984323fa67c2e9002”),
“交换”:“二进制”,
“文书”:“XRPUDTT”,
“时间框架”:“1d”,
“蜡烛”:{
"2019-09-06:0000" : {
“开放”:0.25616,
“高”:0.25868,
“低”:0.24692,
“关闭”:0.2511,
“体积”:63377736.0
},
"2019-09-07:0000" : {
“开放”:0.25115,
“高”:0.26285,
“低”:0.25009,
“关闭”:0.25993,
“卷”:53971229.0
},
"2019-09-08:0000" : {
“开放”:0.25989,
“高”:0.26591,
“低”:0.2555,
“收盘”:0.26205,
“体积”:65033003.0
}
“_id”:ObjectId(“5D7D74925BF7734C6C348A0”),
“交换”:“二进制”,
“文书”:“XRPUDTT”,
“时间框架”:“1d”,
“蜡烛”:{
"2019-09-06:0000" : {
“开放”:0.25616,
“高”:0.25868,
“低”:0.24692,
“关闭”:0.2511,
“体积”:63377736.0
},
"2019-09-07:0000" : {
“开放”:0.25115,
“高”:0.26285,
“低”:0.25009,
“关闭”:0.25993,
“卷”:53971229.0
},
"2019-09-08:0000" : {
“开放”:0.25989,
“高”:0.26591,
“低”:0.2555,
“收盘”:0.26205,
“体积”:65033003.0
}
例如,我希望每个文档中的close值如何在python3中查询mongodb以仅返回如下内容 [“收盘”:0.2511,0.25993,0.26205,0.2511,0.25993,0.26205]

还可以从每个文档中获取所有时间戳,如 [2019-09-06:0000,2019-09-07:0000,2019-09-08:0000,2019-09-06:0002019-09-07:0000,2019-09-07:0000,2019-09-08:0000]

这个的键(如果你不介意这个双关语的话)是.items(),它允许你获取键、值对。在这之后,其他的一切都只是字典操作符,你可以根据需要进行操作

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]}')
结果:

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

我建议将时间戳作为键转换为字段名为
date
或类似字段名的rval。这将大大简化您对日期和日期范围的查询。感谢您的帮助。另一件事是,我在另一个集合中使用了更多的数据尝试了此操作,数据结构与我得到的相同。回溯(最近一次呼叫最后一次):打印(v[查找])KeyError:“close”,即使集合具有密钥。我获得了所需的数据,同时还返回了该错误。您需要添加一些错误处理;然后尝试包装print语句…除了并打印k和v。这将有助于隔离问题。好的,谢谢您帮助我解决了问题我忘记了我尝试的另一个集合它有一些具有不同模式的文档,您可以使用regex或类似的工具来过滤输入数据。