Removing map elements from Google Cloud Firestore - android

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()))

Related

How to get the name of child realtime database kotlin

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.

How do I insert data in firestore in array of string?

Here is want I want to do by code and insert in realtime
I Want to take userName from here and want to add in above, array of string userName_*
Create collection("room")
Add randomNumber to "room" Collection
getCurrentUser name ADD to array of users in firestore field
Keeps checking and update in recycler view if new user is found in room -> randomCode -> inRoom (userName)
I want to update the recycler view and show users only if code (randomNumber) generated from start activity (Host) matches with the join activity
Start activity
Join activity
You must use a combination Set with Merge:true to push id's to a document (create if not created) to get this result.
How you manage the document ID wasn't defined clearly, but I assume you have some internal logic, provided is an example of how to push a string into an array.
DocumentReference FirestoreRef = db.collection("room").document("myID");
Map<String, Object> map = new HashMap<>();
map.put("userList", FieldValue.arrayUnion("user_id"));
FirestoreRef.set(docRef, map, SetOptions.merge());

Not able to use getRef() of Firebase in android

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.

How to save data on top of the already saved data in Firebase?

I am saving some data in FirebaseDatabase under a child as a key:value pair.
The problem is that data is getting saved as pushed, like if data1 is saved already than data2 will get saved below it. I want to save it above this already saved data.
Here is the data structure:
- branch
- child
- uniqueKey1: data1
- uniqueKey2: data2
I am saving the data using this code:
String key = mDatabase.child("branch").child("child").push().getKey();
mDatabase.child("branch").child("child").child(key).setValue(data);
What I want is the structure below:
- branch
- child
- uniqueKey2: data2
- uniqueKey1: data1
How to save the newly added data above the already saved data? Please let me know.
In your firebase database, childs in a node are ordered in alphabetical order, no matter which order you save them.
If you want to retrieve them later in some specific order, let's say order by date added, you might want to create a timestamp reference.
Check here:
How to sort value inside a Firebase child to the rest?
Edit: a lot of answers and edited questions while writing my answer, but as others mentioned you should not worry about the order you see the data in the database, you should only care to provide the right structure to retrieve the data correctly.
As I said, in the DB the childs are ordered in alphabetical order so if you insist on ordering it by date added you should figure out a way to update the key accordingly and then update the whole node.
I don't think the order in which you save data is important as long as you have a strategy to retrieve it correctly in your order of choice. Firebase provides API to retrieve data ordered by either key or value.
Work with Lists of Data on Android
If you just want to retrieve the record first which was added last, you can put a timestamp that accompanies your data. There are also methods that gets you last record of a collection.
What you are trying to do is update data1. Then what you have to do is,
Get reference of the node you want to update.
DatabaseReference ref = mDatabase.child("branch").child("child").child("data1");
then update the child node. I'm assuming there is a child name under data1, whose value is 'Foo'. And now you want to update it 'Bar'.
Hashmap<String, String> map = new Hashmap();
map.put("name","Bar");
ref.updateChildren(map);
This is how you update a node. If you want to completely replace the node, then you can delete the node and by calling removeValue() method and pushing the data again!.

Query greater/less than using query#startAt/endAt

I am using query#startAt and query#endAt to retrieve data from Firebase
The problem with both those API is they retrieve nodes with greater/less than or equal key.
Is there anyway i can query greater-than or less-than only? Or i have to remove the item manually when i've done retrieving
I am using the Android-Java Firebase API
The operations are called startAt and endAt because they include the elements at the end points. To remove those "edge elements" you will either have to start at the next value/end at the previous value, or filter client-side.

Categories

Resources