Firestore adding to map without deleting - firebase

Im trying to add to a map field but when i try this line it replaces the current fields under the map. How can I add new elements to a map without replacing the current values?
var document = await db.collection("trips").document(widget.tripInfo.documentID).updateData({
'members': {
name : id,
},
});

If you want to update a nested field, use dot notation:
db.collection("trips").document(widget.tripInfo.documentID)
.updateData({ 'members.name': id })
Also see the Firebase documentation on updating fields on a nested object.

Related

how to add new data to existing map in firebase flutter

here is the firebase document data
I want to add a new map value to the purchased-id. this is the current code
FirebaseFirestore.instance.collection('users').doc(userID).set({
'purchased-id': {
widget.head: widget.id,
}
});
but with this code the map value is replaced,
how do I add another map value to the existing map
like this
I think need merge: true to update the map with its new value.
FirebaseFirestore.instance.collection('users').doc(userID).set({
'purchased-id': {
widget.head: widget.id,
}
}, SetOptions(merge: true));
Reference: https://firebase.google.com/docs/firestore/manage-data/add-data#update_fields_in_nested_objects
Also to differentiate with .set and .update, have a look at this answer: https://stackoverflow.com/a/47554197/11566161

How to update Map Value in Flutter Firebase Firestore

I have a Map object in my document data. Like Below
I have to add a new user bid to the same map without erasing old data but the code I used was replacing the whole Map with the new value. Any suggestions on how to achieve my goal.
FirebaseFirestore.instance.collection('products')
.doc(widget.product.uid).update({
'bids': {
auth.currentUser?.email:
currentPrice.toString()
}
});
You need to use dot notation to update a nested field:
const userEmail = auth.currentUser?.email;
FirebaseFirestore.instance.collection('products')
.doc(widget.product.uid).update({
'bids.${userEmail}': currentPrice.toString()
});
Dot notation allows you to update a single nested field without overwriting other nested field. If you update a nested field without dot notation, you will overwrite the entire map field
References:
Update fields in nested objects
FlutterFire Documentation
To update fields with . in their keys:
var fPath = FieldPath(["bids", "user.name#domain.tld"]);
FirebaseFirestore.instance.collection('products').doc(widget.product.uid).update(fPath, currentPrice.toString());

Flutter : How to add element to map field inside firebase database?

I'm trying to find a way to add element to map field inside firebase database from my flutter code. The element as well is a map. So it's a nested map.
Future updateImageDate({
String token,
int rating,
int like,
int display_count,
//Map participants,
}) async {
return await pictureCollection.document(token).updateData({
'rating': rating,
'like': like,
'display_count': display_count,
//'participants': participants,
// I want to add only one element to this existing map field in firebase database.
// Although currently it's empty, I want to keep add new element every time I use this method.
// The element as well is a map. So it's a nested map.
});
}
Please help me! I'm looking forward to hearing from you. Thank you in advance. :D
To write to a map, use dot (.) notation. So:
pictureCollection.document(token).updateData({ 'path.to.field': 'new value' });

Flutter: How to remove a specific array data in firebase

I'm having a problem right now in firebase. Where I try to delete/remove a specific array data. What is the best way to do it? Ps. I'm just new in firebase/flutter.
My database structure:
Data that i'm trying to remove in my database structure(Highlighted one):
First create a blank list and add element in the list which you want to remove then Update using below method
Note : For this method you need the documennt id of element you want to delete
var val=[]; //blank list for add elements which you want to delete
val.add('$addDeletedElements');
Firestore.instance.collection("INTERESTED").document('documentID').updateData({
"Interested Request":FieldValue.arrayRemove(val) })
Update:
Much has changed in the API, although the concept is the same.
var collection = FirebaseFirestore.instance.collection('collection');
collection
.doc('document_id')
.update(
{
'your_field': FieldValue.arrayRemove(elementsToDelete),
}
);
Firestore does not provide a direct way to delete an array item by index. What you will have to do in this case is read the document, modify the array in memory in the client, then update the new contents of the field back to the document. You can do this in a transaction if you want to make the update atomic.
This will help you to add and remove specific array data in could_firestore.
getPickUpEquipment(EquipmentEntity equipment) async{
final equipmentCollection = fireStore.collection("equipments").doc(equipment.equipmentId);
final docSnap=await equipmentCollection.get();
List queue=docSnap.get('queue');
if (queue.contains(equipment.uid)==true){
equipmentCollection.update({
"queue":FieldValue.arrayRemove([equipment.uid])
});
}else{
equipmentCollection.update({
"queue":FieldValue.arrayUnion([equipment.uid])
});
}
}
Example

How to add a value to list field in firebase?

I have a field of type array in my firebase database and I want to add to it new values.
What code I want :
Firestore.instance.collection('lists').document(123).setData({
'myList': FieldValue.add(**myValue**);
});
This should work:
Firestore.instance.collection('lists').document(123).updateData({
'myList': FieldValue.arrayUnion(['el1', 'el2'])
});

Resources