I add data to firestore
with below code.
val user = hashMapOf(
"loginType" to "app",
"userId" to auth.currentUser,
"createdTime" to current_time,
"name" to (binding.nameInput.text.toString()),
"gender" to (binding.genderInput.selectedItem.toString()),
"phone" to phoneNumber,
"birth" to (birthDBInput),
"community" to (binding.communityInput.selectedItem.toString())
)
db.collection("users")
.add(user)
.addOnSuccessListener {
startActivity(Intent(this@RegisterActivity, DiaryActivity::class.java))
}
And this make data structure in firestore as below.
Here, I want to add one more field named 'community' to one data which has certain userId
in this other component.
So I tried to write some code like below.
var communityMap = hashMapOf<String, String>(
"community" to binding.communityInput.toString()
)
binding.registerBtn.setOnClickListener {
db.collection("users").whereEqualTo("userId", userId)
.update(communityMap as Map<String, Any>)
.addOnSuccessListener {
startActivity(Intent(this@CommunityRegisterActivity, DiaryActivity::class.java))
}
But update
function doesn't work (It says this function doesn't exist) And set
is also same not working.
For this purpose, How can I extract certain data and add field?
Firestore doesn't support so-called update queries, where you send the query conditions and an update instruction to the database. Instead you will have to execute the query in your application code, read the results, and then update each individual resulting document.
Something like:
db.collection("users").whereEqualTo("userId", userId)
.get()
.addOnSuccessListener { documents ->
for (document in documents) {
document.reference.update(communityMap as Map<String, Any>)
}
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.