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

167
Visualizações
Remove all fields associated linked to a document that I removed on another collection

For the context I'm using firebase/nodeJS on my application.

I'm deleting an user of a collection called workers. To do so I update the field deleted : true on the worker document. I'm not deleting the document, because I want to keep their infos if needed. But when I delete a worker, I must remove him from all the properties is working on (they have a field called workers, which is an array of all the workers associated.)

Hence, I must browse all the properties of the collection associated, check if the worker is on it and remove him.

Is there a way to do so ? I saw this page : https://firebase.google.com/docs/firestore/manage-data/add-data#node.js_11 But don't know how to use theses ressources to do so.

about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

So I finally did it alone. First, I had to fetch all the properties where the work is working on, to do this I used array-contains.

let docsProperties = await db.collection('user').doc(id).collection("properties")
            .where("workers", "array-contains", workerId).get();

Then I wanted to browse all theses properties and remove the worker. Thought about doing a ForEach first. But the problem is I wanted to use an await .update() with arrayRemove and async methods aren't usable inside ForEach. So I used promises.

            let promises = [];

        docsProperties.forEach(doc => {
            promises.push(doc.ref.update({
                workers: admin.firestore.FieldValue.arrayRemove(workerId)
            })); });
        return Promise.all(promises);

arrayRemove is a method from the firebase documentation to remove all the occurences of an element on a following array field. So I browsed all properties, added all promises and executed them!

worked perfectly, if anyone got questions don't hesitate.

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