Print date from MongoDB in Python - python

I have two objects in the db with diferent dates:
{
"_id" : ObjectId("5addeaf92602ff20497e9406"),
"success" : true,
"timestamp" : 1524477784,
"base" : "EUR",
"date" : "2018-04-22",
"rates" : {
"AED" : 4.492662,
"ALL" : 128.39508,
"AMD" : 586.837094}
and the second one:
{
"_id" : ObjectId("5addb57d0043582d48ba898a"),
"success" : true,
"timestamp" : 1524477784,
"base" : "EUR",
"date" : "2018-04-23",
"rates" : {
"AED" : 4.492662,
"ALL" : 128.39508,
"AMD" : 586.837094}
My python code:
import pymongo
uri = "mongodb://127.0.0.1:27017"
client = pymongo.MongoClient(uri)
database = client['db']
collection = database['currency']
d=(*something I guess*)(input('Insert date: '))
item = collection.find_one({})
data= item['date']['d']
print(data)
What I want is to insert a day or a specific date, and then the program would print that specific day info.
In the db the date is in String and I think that I have to convert it.
Thanks in advance.

Well, first of all data= item['date']['d'] this will not gona work because the 2nd [] is used for indexing. And in your case its up to you either you take input same as your string date or convert it into date the take input then put a check on it
For converting string into date do this:
from datetime import date
year, month, day = map(int, (d['date']).split("-"))
required_date = date(year, month, day)
print required_date
Note: assuming that i have your mongo object/dictionary in d
and for second case:
take input from user in the same format in string for e.g:
userinput = str(raw_input('Enter Date: in Format(year, month, day) with seprator "-" e.g: 2018-04-23 \n'))
if userinput == d['date']:
print 'Correct'
again assuming that d is your mongo object.
Note:
Its for exmaple what ever case you are using use then put a if check on it.
Hope it helps you! :)

Related

JSON Schema for DateTime Validation in Python - AWS Lambda

I currently have a JSON schema that required date time validation.
"type": "array",
"items": {
"type": "object",
"properties": {
"activeFrom": {"type": "string", "format": "datetime", "pattern": ********},
}
...
Validation should exist so that
Dates are in the format YYYY-MM-DDTHH:MM:SS.00-00:00
Represents (Year-Month-Day"T"Hour:Minute:Second.Milisecond-TimeZoneHour:TimeZoneMinute)
GMT Time Zone = 00:00
Example: 2022-11-12T16:32:21.00-00:00
Incorrect dates are not acceptable (i.e. 31 days in February, or incorporate leap year)
I have tried many different patterns (******) but can't seem to find one that works.
Appreciate any help and apologies if this questions structure is incorrect, new to SO :)
I think this will check dates in the way you're looking for.
First though pip install python-dateutil
EDIT: Have changed following comments.
from dateutil.parser import parse, ParserError
from datetime import timedelta
def check_date_is_legit(input_date):
"""
Checks if an input_date is legitimate
:param input_date: string, date to check.
:return result: False if the date isn't legit and True if it is.
"""
try:
parsed = parse(input_date)
if parsed.tzname() == 'UTC':
result = True
else:
result = False
except ParserError:
result = False
return result

How to use regex on date to extract entry by year?

My entries in MongoDB have a publishedDate field as follows:
publishedDate:"{'$date': '1999-08-01T00:00:00.000-0700'}"
How do I retrieve the entries via collection.find with $regex, using user's input for year?
From MongoDB version >= 4.4 we can write custom filters using $function operator so try this:
let yearRegex = /^1999/;
db.testCollection.find({
$expr: {
$function: {
body: function(publishedDate, yearRegex) {
return yearRegex.test(publishedDate);
},
args: [{ $toString: "$publishedDate" }, yearRegex],
lang: "js"
}
}
});
Note: Instead of $toString we can also use $dateToString with timezone to cover edge cases.
"{'$date': '1999-08-01T00:00:00.000-0700'}" looks like MongoDB extended JSON notation for a Datetime object.
If the data in the collection is actually a date, note that the timezone in the database will be UTC, so the start/end would be off by a few hours if you intended to use any other timezone.
You can build a date object for the beginning of the year, and another for the beginning of the following year, and query for dates between:
let queryYear = 1999
db.collection.find({
publishedDate:{
$gte: new Date( queryYear + "-01-01T00:00:00-0700" ),
$lt: new Date( (queryYear+1) + "-01-01T00:00:00-0700")
}})
This allows to you build a date object with the desired timezone, and this query could also make use of an index on the publishedDate field.

MongoDB/Python - Date in collection (to use for query)

I just started using mongoDB (Version 3.6.8) today, and I like it.
I was reading that it should be possible to have a date object directly in the database, but I can't get it to work.
Also I was wondering if it is the best solution or if I should just store my dates as "Epoch millis" instead?
I am trying to use use the $dateFromString keyword which should work but i receive this error:
bson.errors.InvalidDocument: key '$dateFromString' must not start with '$'
My code looks like this:
from datetime import date
import pymongo
dbcli = pymongo.MongoClient('mongodb://192.168.1.8:27017')
db = dbcli['washbase']
col = db['machine']
def conv(dato):
return {
'$dateFromString': {
'dateString': dato,
'format': '%Y-%m-%d',
'timezone':'Europe/Copenhagen',
}
}
today = date.today().isoformat()
data = {
'day': conv(today),
'time':12,
'room':'2B',
}
col.insert_one(data)
The reason why I need something like a date-object in the database is because I want to do a conditional query on the data, so that the database only sends the data i require. So i expect to do something like this.
result = col.find(
{
'day' : {
'$gt' : {
'$date' : '2020-01-01'
}
}
}
)
for x in results:
print(x)
But when I do this the app prints nothing.
The $dateFromString is an operator for MongoDB aggregations. An aggregation is a powerful way to create complex queries in MongoDB. Hence, this might not be what you need.
I would recommend storing the dates in the normal format. So your code should look something like this:
from datetime import date
import pymongo
dbcli = pymongo.MongoClient('mongodb://192.168.1.8:27017')
db = dbcli['washbase']
col = db['machine']
today = date.today().isoformat()
data = {
'day': today,
'time':12,
'room':'2B',
}
col.insert_one(data)
If you are concerned about timezones, MongoDB stores each date in UTC by default, converting whatever timezone is specified in your date to UTC. When reading the dates, you can then convert them to whatever timezone you need.
EDIT:
When writing your query, try using an actual date object. This converts the query date to an actual ISO date that the DB can understand.
col.find({'day': {'$gte': ISODate(date.today) }})
If you're trying to find entries that fall within a date range, you can do something like:
col.find({'day': {'$gte': ISODate(date.today), '$lte': ISODate(date.today + 24 hours) }})

python coinbase_api placing order returns "{'message': 'Invalid expire_time '}"

i am trying to place an order with the coinbase pro api, but it fails because of the invalid expire_time.
i tried to pass the "cancel_after" parameter as timestamp, date string in iso format and seconds but still no success.
i dont really understand what is meant by
cancel_after [optional]* min, hour, day
my params are as follows:
params = {
"type":"limit",
"side":"buy",
"product_id": "BTC-EUR",
"price": "1000.0",
"size": "0.01",
"stop_price": "1100.0",
"stop": "entry",
"time_in_force": "GTT",
"cancel_after": ?
}
has some faced the same problem ?
API-Reference for placing an order
cancel_after [optional]* min, hour, day
min,hour or day muste passed as string.
there is no real datetime value you can only set:
"min" => 1 Minute
"hour" => 1 Hour
"day" => 1 Day

Putting a Date object into MongoDB, getting back a float when querying with pymongo

I'm adding a date value into a MongoDB collection as part of a map-reduce call:
day = Date.UTC(this.time.getFullYear(), this.time.getMonth(), this.time.getDate());
emit({ user : this.user, day : day }, { count : 1 });
When I later query this collection in the Mongo shell I see:
{ "_id" : { "user" : "assaf", "day" : 1331769600000 }, "value" : { "count" : 15 } }
{ "_id" : { "user" : "assaf", "day" : 1331856000000 }, "value" : { "count" : 57 } }
Somehow the date looks like an integer - I guess it's some timestamp representation.
If I do this:
PRIMARY> new Date(db.my_collection.find()[0]["_id"]["day"])
I get back the correct date:
ISODate("2012-03-19T00:00:00Z")
My question is how to do the same in pymongo. If I run any query on the above collection, pymongo returns documents in which the day value as a float type with the same value as the timestamp:
dict: {u'_id': {u'user': u'ariel', u'day': 1332115200000.0}, u'value': {u'count': 99.0}}
How do I turn this timestamp into a Python datetime?
Looks like milliseconds since epoch (1 Jan 1970):
>>> from __future__ import division
>>> dict = {u'_id': {u'user': u'ariel', u'day': 1332115200000.0}, u'value': {u'count': 99.0}}
>>> datetime.datetime.utcfromtimestamp(dict['_id']['day'] / 1000.0)
datetime.datetime(2012, 3, 19, 0, 0)
>>>
UPDATE: Added division check from first comment.
The title of the question is not the same as the code.
Date.UTC() returns a integer, not a date object.
You are storing the integer and mongoDB is fine with that.
Later, you pull the integer out and use it in the Date() construct and in the javascript environment this is all fine.
But in python, it only sees the integer.
The conversion posted earlier seems to be a good one.

Categories

Resources