如何在Pymongo

时间:2018-05-14 13:24:15

标签: python python-3.x mongodb pymongo

我有这个mongoDB:

{
"_id" : ObjectId("5addb57d0043582d48ba898a"),
"base" : "EUR",
"date" : "2018-04-23",
"rates" : {
    "BRL" : 4.175076
}

{
"_id" : ObjectId("5addb57d0043582d48ba898a"),
"base" : "EUR",
"date" : "2018-04-24",
"rates" : {
    "BRL" : 4.185076
}

    {
"_id" : ObjectId("5addb57d0043582d48ba898a"),
"base" : "EUR",
"date" : "2018-04-25",
"rates" : {
    "BRL" : 4.205076
}

在Python中我想插入一个开始日期,在这种情况下它将是第23天和结束日期,它将是第25天,然后如果它可能以某种方式它将会还搜索开始日期和结束日期之间的日期。 我已经连接到数据库了。其余的代码只是我完整代码的一个例子。

import pymongo

#conectar à bd
uri = "mongodb://127.0.0.1:27017"
client = pymongo.MongoClient(uri)
database = client['db']
collection = database['currency']
collection2 = database['countries']

#Encontar dados na bd
p = str(input('Insert a country: '))
d = input('Insert a date (yyyy-mm-dd): ')
item = collection.find_one({"date" : d})

if p == 'Brasil':
 d = collection.find_one({})
 item = collection2.find_one({"Country": p})
 aj = item['helpCost']
 firstCoin = item['LocalCoin']
 item2 = collection.find_one({})
 secondCoin = item2['rates']['EUR']
 finalCoin = item2['rates'][firstCoin]
 res = (secondCoin / finalCoin)
 res2 = res * aj
 print(res2)

提前致谢!

0 个答案:

没有答案