questions
-KkXXZkVBjaNwnnPtRzU
date:
"19052017"
question:
"Q9"
uid:
"WAX6aezrBzXuXW3RtnnSApMazRb2"
This is the database structure at the moment. I'm writing a code to delete the node based on the submitted key, in this case KkXXZkVBjaNwnnPtRzU. I've obtained the key using the getKey() method and now I want to use the removeValue() on an DatabaseReference object to which I'm passing the key as the reference.
How would I go about doing that?
I should mention that the current key value I'm passing is a string. Is something like this possible at all? Basically the same principle as passing the value to a child() method, but instead of accessing a child of the node, I'd go for its key.
DatabaseReference myRef = FirebaseDatabase.getInstance().getReference("questions");
myRef.child("KkXXZkVBjaNwnnPtRzU").removeValue();
It's simple as that. Of course, instead of hardcoding the values, you should use String which you've obtained previously.
Related
I am trying to get the email (in this case mikeyreid2002#gmail.com) however I don't see any method I can use that gets me the name of the child instead of the value.
https://i.stack.imgur.com/uzonn.png
In this case the email is the key of a nested object. You need to iterate over the collection documents and use their id as email
firestore.collection("Users").get().result.documents.forEach {
Log.d("Key", "${it.key}")
}
You can also use higher-order function to transform the documents, associate (using the email) and deserialize the data.
I need to update the data in Firebase database. I have displayed the data in Recyclerview. I need to get the child reference of the position i click in Recyclerview. An not able to use getRef() there to get the reference.
You need to generate a unique key with
String unique = reference.push().getKey();
when saving data to firebase, then you can use that unique key to modify content of that node later.
DatabaseReference ref = FirebaseDatabase.getInstance().getReference("Transactions").child(uniqueId)...
You can show your getRef(position) method in your question, maybe another solution can come from there.
I'm trying to remove a map<> from Google Cloud Firestore.
I use the following code for updating in Firestore
fun updateData(userId: String, values: Map<String, Any>) =
db.collection(COLLECTION_DATA).document(Id).update(values)
The document contains the used id's generated for google sign in and one of the field in the document is a map<Key,Value> . I'm trying to delete the content of the Field (removing a specific <key,value> pair)
I pass (userId, mapOf("FieldName.${Key}" to FieldValue.arrayRemove(Value))) as the parameter while calling the above firestore function.
But after updating the value in Firestore, the key is left out and the values are cleared.
If I pass (userId, mapOf("FieldName" to FieldValue.arrayRemove(Key))) as the parameter, then the whole map<> is removed (which is what I want) but the data structure of the Field is converted as an array after performing the update.
FieldValue.arrayRemove() is for array type fields only. It won't work here, because you're not actually using any arrays or lists.
You will just want to use FieldValue.delete() to remove the any map key and its value.
updateData(userId, mapOf("FieldName.${Key}" to FieldValue.delete()))
I have a database reference.
houseReference = FirebaseDatabase.getInstance().getReference("House");
I add an item to the db using:
houseReference.push().setValue(house);
Google firebase reference documentation says you can then get the key for the last item using
houseReference.getKey();
However, this returns House. What am I doing wrong?
Firebase has no concept of "the last key". But each DatabaseReference is a reference to a path in the database, and you get the last key in that path by calling getKey() on it. So if you call houseReference.getKey() you indeed get house, since that is the last key in the path that it refers to.
Now we get to the good stuff though.
When you call push() Firebase generates (in the client) a new unique key, and return a new DatabaseReference that point to that key under the current path. So houseReference.push() returns a new DatabaseReference to the new, unique, child location. And this means that you can call getKey() on this new DatabaseReference to get the last key of that new location.
So:
DatabaseReference newRef = houseReference.push();
System.out.println(newRef.getKey());
newRef.setValue(house);
This I suspect is easy, but I can't get it to work as I want. I'm referencing a database ref with my query information for my Firebase database. The code below works fine, but I can't hard code in Match_01 (this was purely done to get the code working).
String getArgument = getArguments().getString("matchid");
final DatabaseReference ref=FirebaseDatabase.getInstance().getReference().child("Matches").child("Match_01");
What I need to do is use the matchID thats been passed to the fragment and use equalTo instead of referencing the final child node.
final DatabaseReference ref=FirebaseDatabase.getInstance().getReference().child("Matches").orderByChild("gameID").equalTo(getArgument);
But this doesn't work, I can't swap out the last child reference for the orderByChild reference.
All help is appreciated.
I'm assuming from your question that the Matches node's children have the id you want to reference dynamically as a key.
Then, you need orderByKey instead of orderByChild. This should work:
final DatabaseReference ref = FirebaseDatabase.getInstance().getReference().child("Matches");
Query q=ref.orderByKey().equalTo(getArgument);
When using orderByChild, the query will match nodes against this node's attributes. In other words, your attempt would work on a collection with the following structure:
- Matches
- $key
- gameID: "Match_01"