I am trying to make app like Instagram with Firebase as a backend, while user adds the comment in any photo, new id is generated by Firebase now I want to add one child(likes) when user click on the like button in this randomly generated comment id but the issue is how to get that particular comment id?
There are two ways in which you can achieve this. So if you want to access a specific comment, you must know something that unique identifies that pcommentll. The first solution would be to store that random id in a variable in the exact moment when you are pushing a new comment to the database using the push() method. To get that id, you can use the following lines of code:
DatabaseReference rootRef = FirebaseDatabase.getInstance().getReference();
String commentId = rootRef
.child(UserId)
.child(PhotoId)
.child("comments")
.push()
.getKey();
You can also save that id inside the comment object if you need to use it later. So this is how it looks like in code:
Map<String, Object> map = new HashMap<>();
map.put("noOfLikes", 0);
map.put("commentId", commentId);
rootRef
.child(UserId)
.child(PhotoId)
.child("comments")
.child(commentId)
.updateChildren(map);
Once you have this key, you can use it in your reference to update the number of likes. This type of operation is usually done using Firebase transaction and for that I recommend see my answer from this post in which I have explained how to update a score property but same principle aplly in the case of likes.
The second approach would be to create an entire new top level collection like this:
Firebase-rot
|
--- comments
|
--- commentId
|
--- noOfLikes: 1
Using this solution it will be more easy for you to query the database becuase to get the number of likes you'll need only this simple reference:
DatabaseReference noOfLikesRef = rootRef
.child("comments")
.child(commentId)
.child("noOfLikes");
noOfLikesRef.addListenerForSingleValueEvent(/* ... */);
Try this:
Fetch the commentID before storing it like this:
String CommentId = myRef.push().getKey();
Save the ID along with comment data:
myRef.child(CommentId).set(yourValuesHashMap);
Now, when you retrieve the comments, you'll have the ID of each comment and you can use that the insert the likes.
Related
I have a "Users" data structure like this, I want to increment the value of ttl_user by 1 when a user is signed up. And decrement it by 1 when a user has deleted their account.
How can I update the value? Thanks.
In your particular case, it would be as simple as:
DatabaseReference db = FirebaseDatabase.getInstance().getReference();
DatabaseReference userRef = db.child("User");
userRef.child("ttl_user").setValue(ServerValue.increment(1));
And to decrement, simply pass a negative value -1.
may I know is there any way to access the Auto-Generate document ID in Firestore using Java? Because I want to add new field and data to existing document through my own android app. I have gone through the documentation, but didn't find any that solve the problem.
Elaboration of problem: I try to obtain the auto-generate ID of each "Event" document for the purpose of adding/update new field "Save Event" for each "User" in Firestore. So that 1 user can save many "event" inside the firestore.
In order to use the document ID that you are looking for, first, you need to store it in a variable. When you are adding a document to the database and you are using a document() method call without passing any arguments, a unique ID is generated. To get that ID, you should use the following code:
String docId = collRef.document().getId();
collRef.document(docId).set(obj);
In which "collRef" is a reference that points to the collection in which you want to add the document, and the "obj" is the object that you want to be added to the database.
If needed, you can also store the document ID, as a property of your object.
Once you have this ID, you can perform the update like this:
FirebaseFirestore rootRef = FirebaseFirestore.getInstance();
CollectionReference collRef = rootRef.collection("collName");
collRef.document(docId).update(map).addOnCompleteListener(/* ... /*);
I have this database structure in firebase:
Its supposed to collect votes. I have several categories such as president, minister...etc. All people who vie for a "President" seat are listed using their unique keys, and Im trying to collect all voters emails.
This is my code:
public static void insertvote(String userkey, String categ, String candId) {
System.out.println("Returned candidate's userkey or ID: "+userkey);
System.out.println("Returned category: "+categ);
DatabaseReference rootRef = FirebaseDatabase.getInstance().getReference();
DatabaseReference totalVotesRef = rootRef.child("votes").child(categ).child(candId);
Vote vote = new Vote(userkey);
totalVotesRef.setValue(vote.getVoterEmail());
}
The problem I have is that, when another user logs in and votes, instead of appending their email, its being overwritter to the existing email.
How can I resolve this?
Thanks
Instead of using setValue() at the location to collect emails, which always overwrites existing data, you should look into using push() first, which generates a new child using a random ID. You will want to become familiar with the documentation on working with lists of data, especially appending to a list of data.
totalVotesRef.push().setValue(vote.getVoterEmail())
Each email will appear under that randomly generated child value.
I am trying to make app like Instagram with Firebase as a backend, while user adds the comment in any photo, new id is generated by Firebase now I want to add one child(likes) when user click on the like button in this randomly generated comment id but the issue is how to get that particular comment id?
There are two ways in which you can achieve this. So if you want to access a specific comment, you must know something that unique identifies that pcommentll. The first solution would be to store that random id in a variable in the exact moment when you are pushing a new comment to the database using the push() method. To get that id, you can use the following lines of code:
DatabaseReference rootRef = FirebaseDatabase.getInstance().getReference();
String commentId = rootRef
.child(UserId)
.child(PhotoId)
.child("comments")
.push()
.getKey();
You can also save that id inside the comment object if you need to use it later. So this is how it looks like in code:
Map<String, Object> map = new HashMap<>();
map.put("noOfLikes", 0);
map.put("commentId", commentId);
rootRef
.child(UserId)
.child(PhotoId)
.child("comments")
.child(commentId)
.updateChildren(map);
Once you have this key, you can use it in your reference to update the number of likes. This type of operation is usually done using Firebase transaction and for that I recommend see my answer from this post in which I have explained how to update a score property but same principle aplly in the case of likes.
The second approach would be to create an entire new top level collection like this:
Firebase-rot
|
--- comments
|
--- commentId
|
--- noOfLikes: 1
Using this solution it will be more easy for you to query the database becuase to get the number of likes you'll need only this simple reference:
DatabaseReference noOfLikesRef = rootRef
.child("comments")
.child(commentId)
.child("noOfLikes");
noOfLikesRef.addListenerForSingleValueEvent(/* ... */);
Try this:
Fetch the commentID before storing it like this:
String CommentId = myRef.push().getKey();
Save the ID along with comment data:
myRef.child(CommentId).set(yourValuesHashMap);
Now, when you retrieve the comments, you'll have the ID of each comment and you can use that the insert the likes.
I implemented the FCM notification to the user in Cloud Functions. By using Cloud Functions I added a document(auto generated id) into the Firestore. I need that document id to add another field as status in the same document from android app.
I had a database path like:
/deyaPayUser
{authid}
/Split
{auth id }
/Received Invitation
{auto id}
. Amount: 10
. PhoneNumber:987654321
These is the data already exist . Now I need to add a field in that document. How to get the current document id of the user
Status : true
According to your comments, in order to find that particular document, you need to create a Query based on property within that document like this:
Query query = yourCollectionReference.whereEqualTo("PhoneNumber", 987654321);
This query will find the user that has the phone number 987654321.
Now just simply use a get() call, get the id and make the update:
Map<String, Object> map = new HashMap<>();
map.put("Status", true);
yourCollectionReference.document(documentId).update(map);