I want to use timestamp value as an integer value i.e. the total number of seconds/milliseconds (the converted timestamp value into seconds/milliseconds) and use it with $divide accumulator/operator
I tried that as this code but get error
pymongo.errors.OperationFailure: $divide only supports numeric types, not date and int
I google for convert datetime to int though get little helpful result
So how can we get around it i.e. to get $divide work with timestamp?
Related
In Mongodb, if I have an ISODate of "2021-07-29T01:57:49.075Z", how do I convert this value to the equivalent BSON timestamp value? The BSON value should be in the form of Timestamp(nnnnnnnnnn, 1). The n value is 10 digits long. I've seen a close answer using the $toLong aggregate, but the long number returned doesn't fit into the 10 digits.
Also the reverse, how to convert a BSON timestamp value to an ISODate.
This seems so simple but I can't find any reference and similar questions convert to a long number not a BSON timestamp in the form described above.
Thanks for your help.
I already have a SO question and answer on how to convert BSON Timestamp in a MongoDB aggregation, but now I have a situation where I would like to convert in node.js.
So just to repeat. My goal is to convert a "Timestamp" datatype to a javascript date, without doing it in an aggregation - is this possible?
If the BSON library you are using provides Timestamp type, you can use its getTime method to return the seconds, and create a date from that:
Date(object.clusterTime.getTime())
If you don't have that function available, the timestamp is a 64-bit value where the high 32-bits are the seconds since epoch, and the low 32-bits are a counter.
Bitshift the value 32 bits right or divide by 2^32 to get the seconds:
Date(object.clusterTime/Math.pow(2,32))
I am storing postgresql timestamptz to redis sorted set using redis-py.
timestamptz is used as score and data is used as value.
I need the set to be sorted in descending order.But I can't insert the data into redis.I don't know how to convert into redis-supported format.
Here's the code:
cursor.execute("select current_timestamp;");
timestamp_raw=cursor.fetchone()
redis_client.zadd("stream",{data:timestamp_raw})
Error is below
.....\AppData\Local\Programs\Python\Python38-32\lib\site-packages\redis\connection.py", line 117, in encode
raise DataError("Invalid input of type: '%s'. Convert to a "redis.exceptions.DataError: Invalid input of type: 'tuple'. Convert to a byte, string or number first.
How to reslove it?
In Psycopg2, timestamptz is returned as a tuple of datetime and FixedOffsetTimezone.
You need to convert the tuple to float so it can be used as a score.
See Converting datetime.date to UTC timestamp in Python
I am extracting three values (server, region, max(date)) from my postgresql> But I want to extract an additional 4th field which should be the numerical addition of 1 to 3rd field. I am unable to use date add function as in the database date field is defined as an integer.
date type in DB
date|integer|not null
tried using cast and date add function
MAX(s.date)::date + cast('1 day' as interval)
Error Received
ERROR: cannot cast type integer to date
Required output
select server, region, max(alarm_date), next date from table .....
testserver, europe, 20190901, 20190902
testserver2, europe, 20191001, 20191002
next date value should be the addition to alarm_date
To convert an integer like 20190901 to a date, use something like
to_date(CAST(s.date AS text), 'YYYYMMDD')
It is a bad idea to store dates as integers like that. Using the date data type will prevent corrupted data from entering the database, and it will make all operations natural.
First solution that came to my mind:
select (20190901::varchar)::date + 1
Which output 2019-09-02 as type date.
Other solutions can be found here.
I have troubles converting HubSpot UNIX timestamp to date. Timestamp is stored as text value.
Value looks like this:
1549324800000
My logic was first to convert the number to bigint and later converted it to date using:
TO_CHAR(TO_TIMESTAMP(properties__vape_station__value)), 'DD/MM/YYYY')
What would be the best way to achieve converting UNIX Timestamp in text type to date in PostgreSQL 11.
You can cast the value in that column to a bigint and then use to_timestamp.
But apparently that column also stores empty strings rather than NULL values if no value is present, so you need to take that into account:
to_timestamp(nullif(trim(properties__vape_station__value),'')::bigint/1000)
This would still fail however if anything else than a number is stored in that column.