1

I have this 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
}

And in Python I would like to insert a beginning date, that in this case it would be day 23 and an end date that it would be day 25, and then if it's possible that in some way it would also search for the dates that are between the beginning and the end date. I have already a connection to the database. The rest of the code is just an example of my full code.

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)

Thanks in advance!

  • Just ask the database to return the valid dates. You really should be using `BSON Date` which would be converted from `datetime` since it takes a lot less to store. As long as the dates are presented in "lexical" ISO Format order ( as they are ) then the same `$gte` and `$lt` operators apply for the selection. In the real world you ask the database rather than filtering the whole collection content "after" returning to the client. – Neil Lunn May 15 '18 at 02:16

0 Answers0