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

How can I query unknown values from arrays in mongodb

我被卡住了,不知道该怎么做我有一个 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 查询为 return 之类的东西 ["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]

关键(如果你原谅双关语的话)是 .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