Atualize um documento do Firestore que contenha um campo de matriz

Atualize um documento do Firestore que contenha um campo de matriz.

Explore mais

Para ver documentação detalhada que inclui este exemplo de código, consulte o seguinte:

Exemplo de código

C#

Para se autenticar no Firestore, configure as Credenciais padrão da aplicação. Para mais informações, consulte o artigo Configure a autenticação para um ambiente de desenvolvimento local.

DocumentReference washingtonRef = db.Collection("cities").Document("DC");

// Atomically add a new region to the "regions" array field.
await washingtonRef.UpdateAsync("Regions", FieldValue.ArrayUnion("greater_virginia"));

// Atomically remove a region from the "regions" array field.
await washingtonRef.UpdateAsync("Regions", FieldValue.ArrayRemove("east_coast"));

Java

Para se autenticar no Firestore, configure as Credenciais padrão da aplicação. Para mais informações, consulte o artigo Configure a autenticação para um ambiente de desenvolvimento local.

DocumentReference washingtonRef = db.collection("cities").document("DC");

// Atomically add a new region to the "regions" array field.
ApiFuture<WriteResult> arrayUnion =
    washingtonRef.update("regions", FieldValue.arrayUnion("greater_virginia"));
System.out.println("Update time : " + arrayUnion.get());

// Atomically remove a region from the "regions" array field.
ApiFuture<WriteResult> arrayRm =
    washingtonRef.update("regions", FieldValue.arrayRemove("east_coast"));
System.out.println("Update time : " + arrayRm.get());

Node.js

Para se autenticar no Firestore, configure as Credenciais padrão da aplicação. Para mais informações, consulte o artigo Configure a autenticação para um ambiente de desenvolvimento local.

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

// Atomically add a new region to the "regions" array field.
const unionRes = await washingtonRef.update({
  regions: FieldValue.arrayUnion('greater_virginia')
});
// Atomically remove a region from the "regions" array field.
const removeRes = await washingtonRef.update({
  regions: FieldValue.arrayRemove('east_coast')
});

// To add or remove multiple items, pass multiple arguments to arrayUnion/arrayRemove
const multipleUnionRes = await washingtonRef.update({
  regions: FieldValue.arrayUnion('south_carolina', 'texas')
  // Alternatively, you can use spread operator in ES6 syntax
  // const newRegions = ['south_carolina', 'texas']
  // regions: FieldValue.arrayUnion(...newRegions)
});

PHP

Para se autenticar no Firestore, configure as Credenciais padrão da aplicação. Para mais informações, consulte o artigo Configure a autenticação para um ambiente de desenvolvimento local.

$cityRef = $db->collection('samples/php/cities')->document('DC');

// Atomically add a new region to the "regions" array field.
$cityRef->update([
    ['path' => 'regions', 'value' => FieldValue::arrayUnion(['greater_virginia'])]
]);

// Atomically remove a region from the "regions" array field.
$cityRef->update([
    ['path' => 'regions', 'value' => FieldValue::arrayRemove(['east_coast'])]
]);

Python

Para se autenticar no Firestore, configure as Credenciais padrão da aplicação. Para mais informações, consulte o artigo Configure a autenticação para um ambiente de desenvolvimento local.

city_ref = db.collection("cities").document("DC")

# Atomically add a new region to the 'regions' array field.
city_ref.update({"regions": firestore.ArrayUnion(["greater_virginia"])})

# // Atomically remove a region from the 'regions' array field.
city_ref.update({"regions": firestore.ArrayRemove(["east_coast"])})

Ruby

Para se autenticar no Firestore, configure as Credenciais padrão da aplicação. Para mais informações, consulte o artigo Configure a autenticação para um ambiente de desenvolvimento local.

city_ref = firestore.doc "#{collection_path}/DC"

# Atomically add a new region to the 'regions' array field.
city_ref.update({ regions: firestore.field_array_union("greater_virginia") })

# Atomically remove a region from the 'regions' array field.
city_ref.update({ regions: firestore.field_array_delete("east_coast") })

O que se segue?

Para pesquisar e filtrar exemplos de código para outros Google Cloud produtos, consulte o Google Cloud navegador de exemplos.