How to add a timestamp to Firebase via console? - firebase

-order
-amount: 125
-date: 13/11/2019
-id: "SAV-00000001"
I am trying to add some data, something similar to above. I want the date field to be stored as a date object so that when I query it I can order by date.
When I add a date it's being stored as a string. Is there a way to make sure that it's stored as a date object instead?
Note: The questions I have found talk about how to add a date programmatically, but I'm trying to find out how to add it manually via the console.

Firstly, there is no "date" type provided by Realtime Database. You'll have to choose your own format for dates, and those will usually be a string or a number.
Second, the Firebase console doesn't have a field editor for date-related strings or numbers. If you need a string or number, you're going to have to just type that yourself into the console, or create your own tool to make that easier.

Related

Ignore Turkish Characters On Firestore Query

I have a .net app that uses Firestore as a database and It's using Firestore Query to find some data. The problem is data fields that include Turkish characters but if someone uses my app and wants to search for data and if don't use Turkish characters, the query can not find this data.
For example, if I want to search my name on my app and my name is saved like "Ertuğrul" and if the user searches like "Ertugrul", the query can not find it. I need it to find it. Is there a way to do that?
My code that uses query is here:
QRef = DataBase.Collection("CollName").Document("DocName").Collection("CollName")
.WhereGreaterThanOrEqualTo("NameSurname", $"{NameSurname}")
.WhereLessThanOrEqualTo("NameSurname", $"{NameSurname}\uF7FF");
Firestore queries always return documents where a particular field holds a perfect match. If you want to be able to search for "Ertuğrul" as well as for "Ertugrul", then besides the "NameSurname" field you should consider adding a new field called "NameSurnameWithoutSpecialCharacters" and store each name without those Turkish characters.
When a user searches, simply verify if the searched term contains "special" characters. If it does, search on the "NameSurname", otherwise search on the newly created field.

Firebase data validation based on timestamps

I need a validation rule that checks if the end date is after the start date, so the period is valid. I have found a couple resources, but I couldn't use them to get what I need. Perhaps for someone else it is of some use.
https://medium.com/#khreniak/advanced-examples-of-using-cloud-firestore-security-rules-9e641d023c7e
End-date greater than start-date validation android
Basically, I have document coming in in cloud firestore and I need to check if the value dateEnd > dateStart. Both come from the same request. I would rather store the values as day/month/year instead of storing them as a timestamp value. However, if there is no other solution than to use timestamps, I could use that.
I can't get any further than this:
match /organisations/{orgID}/people/{userID} {
allow create: if(dateEnd > dateStart)
}
Thanks in advance for your help
If you use a date format like YYYYMMDD (which allows to lexicographic order dates), the following should do the trick. It would also work with timestamps in milliseconds.
match /organisations/{orgID}/people/{userID}
allow create: if request.resource.data.dateEnd > request.resource.data.dateStart;
}
As explained in the doc, "When writing data ... the request.resource variable contains the future state of the document".
I would suggest you watch the official video about security rules, a must watch...
If you absolutely need to store the values as DD/MM/YYYY in the Firestore document you should have two pairs of fields: a dateEnd/dateStart pair of field with format DD/MM/YYYY, and another pair of fields, e.g. dateEndForRules/dateStartForRules with format YYYYMMDD that you only use in the Security rules.

How to correctly define ID and DateTime fields in Firebase Firestore database?

I am using Firebase FireStore database for the first time and I have the following question.
I have created a calendar collection. This collection will contains document representing events that have to be shown into a Calendar implemented by an Angular application.
So I am defining the following fields for these documents:
id: int. It is a unique identifier of the specific document\event.
title: string. It is the event title.
start_date_time: string. It specifies the date and the time at which the event starts.
end_date_time: string. It specifies the date and the time at which the event ends.
And here I have some doubts:
Is the id field required? From what I know I will have the document UID that will ensure the uniqueness of the document. If not strongly required adopt an id field can be convenient have something like an auto increment field? (I know that I have to handle in some other way the auto increment because FireStore is not a relational DB and doesn't automatically handle it). For example I was thinking that can be useful to order my document from the first inserted one to the last inserted one.
It seems to me that FireStore doesn't handle DateTime field (as done for example by a traditional relational database). Is this assumption correct? How can I correctly handle my start_date_time and end_date_time fields? These field have to contains the date time used by my Angular application? So for example I was thinking that I can define it as string field and put into these fields values as 2020-07-20T07:00:00 representing a specific date and a specific time. It could be considered a valid approach to the problem or not?
Is the id field required?
No fields are required. Firestore is schema-less. The only thing a document requires is a string ID that is unique to the collection where it lives.
There is no autoincrement of IDs. That doesn't scale massively the way Firestore requires. If you need ordering, you will have to define that for yourself according to your needs.
In general, you are supposed to accept the randomly generated IDs that the Firebase client APIs will generate for you. Ordering is typically defined using a field in the document.
It seems to me that FireStore doesn't handle DateTime field
Firestore has a timestamp field type that stores moments in time to nanosecond precision. There is no need to store a formatted string, unless that's something you require for other reasons.

How does Firestore save dates?

I've recently discovered firestore.Timestamp and so I tried to assign firestore.FieldValue.serverTimestamp(); to one and it didn't work. instead it says Type 'FieldValue' is not assignable to type 'Timestamp' which I would say is strange. Shouldn't serverTimestamp() be a Timestamp and not a FieldValue? What would be the purpose of firestore.Timestamp if not to save timestamps to Firestore? Is there a way to get a server timestamp that is compatible with firestore.Timestamp or should I be avoiding timestamps altogether, and just stick to firestore.FieldValue for all my dates?
serverTimestamp() just returns sentinel value that tells the Firestore server that it should use the current time on the server as the value for the field you're trying to set. It doesn't return an actual Timestamp object itself.
The reason why you'd use this is to make sure that dates are being set consistently on the server instead of depending on the clock being correct on the user's device.
If you want to know the current time on the device, just use the native date/time objects provided by the language or operating system.

Solr does not retrieve the date correctly

I have some documents indexed in Solr that contain some timestamps, like that:
2017-10-21T11:53:33Z.
When I perform queries form Solr Admin UI, I get the correct information (the exact timestamps).
However, when I perform queries from SolrJ, I get the following timestamps:
Sat Oct 21 14:53:33 EEST 2017
The field that stores this timestamp has the following type: org.apache.solr.schema.TrieDateField
So the format and the timezone is changed. I read this post and this one and I understand that SolrJ takes into consideration the local timezone when it retrieves dates, but why is this happening?
Also, the format is changed and I don't want that! I want the exact same format that I input when I index dates to Solr.
How can I change this date format when I retrieve dates from Solr using SolrJ and how can I retrieve the exact values (the exact timezone) that I can see when I use Solr Admin UI?
SolrJ parses the response from Solr, converts string "2017-10-21T11:53:33Z" to a Date object and returns you it.
By default, the timezone is printed in your local timezone (EEST), so it just a representation, but the date/time is the same. You can convert it and display in any timezone.
For example see this answer: https://stackoverflow.com/a/22364501/937970
Another option is to set a global timezone for your JVM, but this might affect the whole application:
java -Duser.timezone=UTC ...
When you make a query using Solr Admin UI it just returns you raw values, that's why you see the UTC value.

Resources