I am running a python script which connects to a MongoDB database and a time stream collection. I am trying to insert a new document in the timestream.
newdata = {"metadata": { "ser": "129031", "type": "data" }, "timestamp": {"$date" : timestamp}, "val": value}
result = cur_collection.insert_one(newdata)
The problem is that the timestamp value is supposed to be in some BSON format. However, no matter what I’ve tried, I cannnot properly generate this value in python. All I’m trying to do is generate the current UTC timestamp and pass it.
This BSON value is supposed to be 64bit, with 32 bit seconds since epoch, and 32 bit with miliseconds. I could probably generate this manually, but It’s hard to believe
Some of the things I’ve tried include:
#timestamp = bson.datetime_ms.DatetimeMS(time.time())
#timestamp = int(time.time()*1000.0)#time.time()
#Timestamp.Timestamp(datetime.now(), 1) #dt.now()
#timestamp = datetime.now()
#timestamp = datetime.datetime.now(pytz.utc)
#eastern = dateutil.tz.gettz('US/Eastern')
#timestamp = datetime.now(tz=eastern)
#timestamp = new Date().getTime()
timestamp = Timestamp(1412180887, 1)
We need an integer and the best I get is XXXXXXX.YYYYY. Maybe multiplying the fraction and adding it could work if I shift the integer by 32.
When i try to run the code I get the error
"'timestamp' must be present and contain a valid BSON UTC datetime value"