0

我被卡住了,不知道该怎么做我有一个 mongodb 服务器,它存储来自 pandas 数据帧的打开高低关闭量我试图弄清楚如何在不指定日期时间的情况下查询每个文档并仅获取值邮票。我是 mongodb 的新手,不完全确定该怎么做



    "_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
        }

例如,我希望每个文档中的 close 值如何在 python3 中查询 mongodb 以返回类似 ["close": 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:0000,2019-09-06:0000, 2019-09-07:0000, 2019-09-08:0000]

4

1 回答 1

0

关键(如果你原谅双关语)是 .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
于 2019-09-15T00:04:58.553 回答