How can I store new Date().getTime() in mongodb? - javascript

How can I store new Date().getTime() in mongodb? Its being stored as double value.
Example: when I store 1398856176723 , its being stored as 1398856176723.0000 and type as double

Just insert the date object and MongoDB will store it correctly (including timezone information).

Related

How to pass a JS date object to cloud function for storing it on Firestore

When I write a Date object to my Firestore, all works correctly and I can see a date in the DB stored with the type "timestamp". If I try to pass a Date object to a cloud function, I don't know why but it is an empty object when I store it from the cloud function to the firestore DB.
firebase.functions().httpsCallable("myFunction")({date: new Date()}) <---- // this doesn't work
So, I have decided to convert the Date object to a firestore timestamp in the client side before sending it to the cloud function. As follows:
date = firebase.firestore.Timestamp.fromDate(date);
firebase.functions().httpsCallable("myFunction")({date})
Then, if I get it in the cloud function and store it on my firestore, I see an object with the fields "seconds" and "nanoseconds", but not a timestamp (I mean it is not stored with the type Timestamp)... So when I get this date I cannot apply the method .toDate()
Is there any good way to store the date in this situation as a timestamp and not as an object?
The input and output of callable functions deal entirely with JSON, so they are limited to expressing only those types that are allowed in JSON: string, number, boolean, null, object, array. Date and Timestamp are not among those types. What the Firebase SDK will do is try to serialize custom objects as JSON, which is not working the way you want - the seconds and nanos fields of the Timestamp are being split up during serialization.
If you have a Date to send from the client, you should just send it as a number. Instead of passing a Date object, pass the single number inside it (milliseconds since unix epoch):
const number = date.getTime()
On the receiving side, in Cloud Functions, take the number and convert it back to a Date:
const date = new Date(number)
Then you can write the Date object to a Firestore document field.

Firebase Firestory query where stored timestamp is in the past

I need to delete all Documents which have a timestamp (stored in a field) prior to today.
The timestamp is created in the Firestore GUI. The following query doesnt return any docs.
collectionRef
.where('timestampFieldName', '<', Date.now())
.get()
What exactly is a timestamp, created in the GUI and how to compare it with any date?
Whenever passing a date to Firestore, you should pass in an actual Date object. Date.now() returns a timestamp, which is just a number and not a Date object itself. To get the actual Date for the same value, use new Date(). So:
collectionRef
.where('timestampFieldName', '<', new Date())
.get()

Querying and comparing Firestore serverTimestamp

I'm fairly new to Firebase/Firestore and I'm querying some stored data for analysis via Cloud Function.
I can't figure out how to compare timestamp within a query.
Each document has a field set using admin.firestore.FieldValue.serverTimestamp().
My initial query against the collection works, using ref.where("timestamp", ">=", startTime) where startTime is a Date object.
However, when I iterate through a snapshot - using doc.data().timestamp. Logging this value I get something like 2018-05-10T22:51:37.236Z which is not comparable.
Can I transform the doc.data().timestamp in a way that I can compare against a unix timestamp or Date object?

DATETIME in Javascript in Database

I'm trying to store a DATETIME in my Database using Javascript, there is just one problem, it doesn't store the DATETIME which was set to DATETIME in the database.
This is how I store and get the DATETIME,
var currentTime = new Date().toISOString().slice(0, 19).replace('T', ' ');
query('UPDATE `users` SET `claimed`='+currentTime+' WHERE`id`='+pool.escape(user.steamid));
The problem is that the database doesn't update, but it can't be the fault of the database, did I miss something? Can you not store a Javascript "String" as Datetime?
Thanks.
Your Javascript varible is ok.
Try this
query('UPDATE `users` SET `claimed`="'+currentTime+'" WHERE`id`='+pool.escape(user.steamid));

Creation of new JSON Object

I am using Google Graphs and I am getting data from MySQL database and encoding it with PHP to JSON and sending it from controller to view. But the problem is date is sending as string and graph cannot use this date. And I changed date to unix system and sending it to view I can get date as a date.
[[1383424123,"AAA",0.001735],[1383424518,"AAA",0.001689],[1383424123,"BBB",0.65211],[1383424518,"BBB",0.655739],[1383424123,"CCC",1],[1383424518,"CCC",1]]
Above I am getting this json object from controller.
In view I am using this json object for Google Graph :
<script> var jsonData=<?php echo $jsdata;?> </script>
I need to get date unix system values and create new date like ->
new Date(jsonData[i][0] * 1000);
And create new the same json object but with new date ( will be replaced with new Date(jsonData[i][0] * 1000); values ) which I am getting from controller and rest of data should remain. How can I do it, creating new json object with replaced date values (only date).
You can just modify the existing value of the array by assigning back to it:
jsonData[i][0] = new Date(jsonData[i][0] * 1000);.

Categories

Resources