Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

248
Visualizações
How to delete a Firestore field inside a map that is an array element?

I would like to be able to delete varToDelete field inside a map if a condition occurs.

For some reason, the code to delete it is reached, but nothing is happening

Here is the structure of my document:

loc [
  {
    var1,
    var2,
    varToDelete
  },
  {
    var1,
    var2,
    varToDelete
  }
]

So, I started to think about this code:

await db.runTransaction(async (t) => {

  var locRef = db.collection('documents').doc('123-456');
  var locSnapshot = await locRef.get();
  var loc = locSnapshot.data().loc;

  loc.forEach(l => {
    if (typeof l.varToDelete !== 'undefined') {
      t.update(locRef, {
        "varToDelete": admin.firestore.FieldValue.delete()
      });
    }
  });
});
about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

"varToDelete": admin.firestore.FieldValue.delete()

This will delete a field 'varToDelete' in the document itself. There is not direct method to delete a field from maps in an array. You must read the doc, remove that property from the objects and update the document with the whole new 'loc' array.

const newArr = locSnapshot.data().loc.map(({varToDelete, ...rest}) => rest)
t.update(locRef, {loc: newArr})
about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda