Reputation: 61
I am using cloud_firestore for flutter .I want to add onto a map field (userInfo) of a specific document of my firebase database on the press of a button. I do not want to add any other fields, only append more data to the map(userInfo).In this case using this code name is always unique (the userID) of a user.
Firestore.instance.collection("prayerRooms")
.document(docID)
.updateData({
'userInfo.userId': name,
'userInfo.userCount': 2
}
);
Upvotes: 4
Views: 5558
Reputation: 66
2023:
FirebaseFirestore db = FirebaseFirestore.instance;
db.collection("collection")
.doc("DOC-ID")
.update({
'userInfo.users': "user1"
});
Upvotes: 0
Reputation: 447
You can user SetOptions(merge: true)
for that
Full code:
Firestore.instance.collection("prayerRooms")
.document(docID)
.set({
'userInfo.userId': name,
'userInfo.userCount': 2
},
SetOptions(merge: true),
);
Upvotes: 1
Reputation: 80904
This code:
Firestore.instance.collection("prayerRooms")
.document(docID)
.updateData({
'userInfo.userId': name,
'userInfo.userCount': 2
});
will update both the userId
and the userCount
inside the userInfo
map. If you want to add more attribute inside the userInfo
map, then you can do:
Firestore.instance.collection("prayerRooms")
.document(docID)
.updateData({
'userInfo.users': "user1",
});
This will add a new attribute inside the map called users
.
Upvotes: 2