I am trying a new timestamp, and want to insert it as a child. How to do that?
I have read this post:
Write serverValue.timestamp as child in Firebase .. and still don't understand
I've tried to enter ServerValue.TIMESTAMP into child and unsuccessful.
This my code:
Object timestamp = ServerValue.TIMESTAMP;
reference.child(String.valueOf(timestamp)).child(uid).child("Status").setValue(cA);
I've read this:
How to save the current date/time when I add new value to Firebase Realtime Database
I follow the code in it, and not work properly -->
What should I do?
The ServerValue.TIMESTAMP can only be written into a value, it cannot be used as the key in the database. So if you want to store the timestamp, you will have to write it as the value of a property. If you want to have chronologically ordered, unique keys, use the push() method.
So combined:
reference.push().setValue(ServerValue.TIMESTAMP);
Related
I'm using redis to design user timeline (as in tweeter). I'm using sorted set to store json value with timestamp as score
zrange feed:user:10000001 0 10 WITHSCORES
1) "{\"postId\":10000411,\"userId\":10000001}"
2) "1639073241754"
3) "{\"postId\":10000412,\"userId\":10000002}"
4) "1639073241748"
When user A unfollow B, how do I remove all posts of user B from timeline of user A?
As querying the json data is not supported in sorted set (and redis), Oher responses on SO suggested to store json value in hash having postId as subkey and storing postId as value in sorted set. but then where do I store userId to look for it when deleting based on it? How do I implement this feature?
I believe you should completely refactor the way you're storing data. I suggest this way:
having a sorted set of feed:user:{userId} which only store postId.
having a set of user:post:{userId} which store set of user's postId
So in order to "remove all posts of user B from timeline of user A", you need to first perform a query of like redis> SMEMBERS user:post:{userBId} to retrieve all B's postId and store it into a variable "results" and then perform this command ZREM feed:user:{userAId} results
According to the Documentation, the below code can set a timestamp as the key of the node using push() in the Realtime Database.
public void uploadToDB(String s) {
databaseReference.push().setValue(s);
}
The returned key are below of my push(), as an example:
a) -MpfCu14jtIkEk28D3CB
b) -MpfCxv_Nzv3YJ87MfZH
My question is:
are they timestamp?
if yes, can I decode it back to a readable timestamp?
are they timestamp?
No, those pushed IDs are not timestamps. However, it contains a time component.
As Michael Lehenbauer mentioned in this blog article:
Push IDs are string identifiers that are generated client-side. They are a combination of a timestamp and some random bits. The timestamp ensures they are ordered chronologically, and the random bits ensure that each ID is unique, even if thousands of people are creating push IDs at the same time.
And to answer the second question:
if yes, can I decode it back to readable timestamp?
If you reverse the engineering, probably yes. Please check the following answer:
How are Firebase IDs generated?
But would not count on that. To have an order according to a time component, then you should add a property of type "timestamp", as explained in my answer from the following post:
How to save the current date/time when I add new value to Firebase Realtime Database
I am searching record from Firebase Database on the formated date rang converted in String type. I am calling filtering query on requestPlaceDate.
Query query = ordersDatabaseRef.limitToFirst(1000).orderByChild(ConstanceFnc.requestPlacedDate).startAt(startDate).endAt(endDate);
query.addListenerForSingleValueEvent(orderListener);
Firebase return data including previous and next dates, not return specific date range data, what I am expecting from a query on startAt() and endAt()
I am searching record from Firebase database on the formated date rang converted in String type.
You aren't getting the desired results because your requestPlacedDate field holds a String and not a Timestamp. When you order String elements, the order is lexicographical. I answered a similar question, so please check my answer from the following post:
How to order the nodes in firebase console based on key
To accomplish the correct order for your results, you should change the type of your field to be Timestamp, as explained in my answer from the following post:
How to save the current date/time when I add new value to Firebase Realtime Database
Once your field will hold the Timestamp value, your Query will work perfectly fine.
Like firebase generating uid. Can firebase generate an unique ID as we wish? If it is possible? how to write a code for it in the following order?
For example PRO00013, PRO00014, PRO00015....
I am asking here, because I'm working in a project for online shopping. when a user adds a product to their inventory it needs to assign an id for every product. that is must be in the human readable format. If it is not possible, just tell me no, I can accept that answer.
You can just call .push() to get randomUUID and then ref.child(randomUUID).setValue(object)
where the object can contain field ID such as PRO00013.
If this doesn't fit your needs you can just place ref.child("PRO00013").setValue(object) and not include field id in this object.
Now you want to get id PRO00014 for the next pushed object. This ain't gonna happen with firebase.
But you can get the last ID added in this ref with query on
ref.orderByKey().limitToLast(1);
Now you have the last added object from this node and you can simply get the key which will be PRO00013.
1. Cast it to String
2. Remove "PRO" from this string
3. Cast the remaining to integer
4. Add 1 to this integer
5. Create new string variable and give it value "PRO" + the new integer
6. Now push the object in ref.(the new string variable).setValue(object).
Now I don't know if you gonna implement this logic in a cloud function accepting some sort of params but if this is the case - this should work just fine.
I also want to know WHY xD
I have to prepare a logic where in a table there are two types of columns for a record.
One that identifies a unique record means key fields
that are non key fields
If key fields are changing then I have to update a record with Delete/Add operator. Whereas if non key fields are updating I have to use Change operator.
I get the records in List of object which using comparator I am able to detect change in Key Fields and use Add/Delete operator.
Comparator logic is compare old Key fields in && with new key fields if there is a change Delete Operator for Old record and Add for new record added. This is straight.
But for Change operator what will be the best way to know which field is updated? Accordingly 'Change' will be added. Using comparator I can validate if something is changed or not, but what changed how to know using comparator or other thing?
As a example in below record ID, Open Date and Status are Key fields where as rest are non key fields
ID, Open Date, Closed Date, Status, Est Closed Date
I can compare complete record for Non key comparision to know if something changed but what is changed I can't know because I have to update the same existing record with Update operator instead of Delete/Add.
I will run Add/Delete comparator check first and then will validate for non key fields changes. Please guide
Update: I think I answered my own question, first I will do Key Field comparision for a record to generate Add/Delete records on basis of updates in key fields combination and then for same record i will do complete key comparision if change is detected update operator will be added and no Insert/Delete will be generated. I don't need to detect which exact non key field changed.
Thanks.
I think I answered my own question, first I will do Key Field comparison for a record and for same record i will do complete key comparison if change is detected update operator will be added and no Insert/Delete will be generated. I don't need to detect which exact non key field changed.
Thanks for looking my question and your time.