如何通过PUT请求体批量更新Cloud Firestore文档字段?
Absolutely! That’s exactly how Firestore’s update() method is intended to be used.
The update() method accepts a plain JavaScript object where each key corresponds to a field in your Firestore document, and the value is the new data you want to set for that field. This works for single fields (like the {capital: true} example from the docs) and multiple fields alike.
So if your PUT request body is { name: "Chicago", capital: true }, passing it directly to cityRef.update(request.body) will:
- Update the
namefield of the document to"Chicago" - Set the
capitalfield totrue - Leave all other existing fields in the document untouched (unlike
set(), which would overwrite the entire document unless you use merge options)
A quick note to keep in mind: Make sure none of the keys in request.body contain Firestore-reserved characters or patterns (like starting with $ or containing . unless you’re intentionally targeting nested fields). For nested fields, you can even use dot notation in the keys—for example, { "location.city": "Chicago" } would update a nested city field under location.
内容的提问来源于stack exchange,提问作者scientiffic

