我被困住了,不知道确切该怎么办,我有一个mongodb服务器来存储来自熊猫数据帧的开盘高低成交量,我试图弄清楚如何查询每个文档并仅获取值而不指定日期时间邮票。我是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]
的键(如果您喜欢双关语)是.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
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句