更新 Firestore 文档字段

使用集合让一切井井有条 根据您的偏好保存内容并对其进行分类。

更新 Firestore 文档字段

深入探索

如需查看包含此代码示例的详细文档,请参阅以下内容:

代码示例

C#

DocumentReference cityRef = db.Collection("cities").Document("new-city-id");
Dictionary<string, object> updates = new Dictionary<string, object>
{
    { "Capital", false }
};
await cityRef.UpdateAsync(updates);

// You can also update a single field with: await cityRef.UpdateAsync("Capital", false);

Go

_, err = client.Collection("cities").Doc("DC").Update(ctx, []firestore.Update{
	{
		Path:  "capital",
		Value: true,
	},
})
if err != nil {
	// Handle any errors in an appropriate way, such as returning them.
	log.Printf("An error has occurred: %s", err)
}

Java

// Update an existing document
DocumentReference docRef = db.collection("cities").document("DC");

// (async) Update one field
ApiFuture<WriteResult> future = docRef.update("capital", true);

// ...
WriteResult result = future.get();
System.out.println("Write result: " + result);

Node.js

const cityRef = db.collection('cities').doc('DC');

// Set the 'capital' field of the city
const res = await cityRef.update({capital: true});

PHP

$cityRef = $db->collection('samples/php/cities')->document('DC');
$cityRef->update([
    ['path' => 'capital', 'value' => true]
]);

Python

city_ref = db.collection(u'cities').document(u'DC')

# Set the capital field
city_ref.update({u'capital': True})

Ruby

city_ref = firestore.doc "#{collection_path}/DC"
city_ref.update({ capital: true })

后续步骤

如需搜索和过滤其他 Google Cloud 产品的代码示例,请参阅 Google Cloud 示例浏览器