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()
Related
We have a check-in and check-out component in our web app that simply uses firebase timestamps. However, we need the ability to edit those timestamps in the event someone makes a mistake.
checkInTimeStamp: fb.firestore.FieldValue.serverTimestamp("17:00") simply returns the current time.
Instead of 17:00, what object do we need to pass through the function to get the correct timestamp?
The serverTimestamp() generates current timestamp on server side. You can use a Date object instead to store a future date:
const futureDate = new Date();
// update date to future time
futureDate.setDate(futureDate.getDate() + 5);
db.document('col/doc').set({ checkInTimeStamp: futureDate })
Checkout other method like setDate() in MDN docs.
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.
earlier we push date use native Firebase format
createAt:admin.firestore.Timestamp.fromDate(new Date)
so data store like this
"createAt": {
"_seconds": 1563181560,
"_nanoseconds": 567000000
}
new we change like this
createAt:new Date().toISOString()
now store like this
"createAt": "2019-07-17T07:17:05.115Z"
buts some date store like this because we use native date format
now how can i call old date to new date format? moste of the data sote like native date method is that any way to change new method?
The Firestore Timestamp type has many conversion methods. Just like it has a fromDate() it also has a toDate() method. So if you reload the data from Firestore and get a Timestamp object, you can then get a date with timestamp.toDate() and thus an ISO-8859 formatted string with timestamp.toDate().toISOString().
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?
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).