Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

147
Vistas
Specifying a condition in payload sent in findByIdAndUpdate

Accounts model:

let accountsSchema = new mongoose.Schema({
    posts:{
        type: Array
    }
});

const Post = mongoose.model("Account", postSchema);

controller in Posts microservice, which populates the posts array in the Accounts microservice:

 let myAccountPosts = await axios.get(`${process.env.ACCOUNTS_MICROSERVICE_URL}/router/account/${userID}`)
.then((resp) => {

    resp.data.message.map((account) =>{


        account.posts.map((post) =>{


            if(myUserPostID !== post._id){



        //the following resets the entire posts array in the other microservice, consider resetting/deleting only that particular post
                let deletePostFromAccountsMicroservice = axios.patch(`${process.env.ACCOUNTS_MICROSERVICE_URL}/router/account/update/${userID}`, {"posts.$[]": "abc"}, (err) =>{
                    if(err) return err;
                });


            }else{
                console.log("no")

            }

        });

    });
}).catch((err) =>{
    console.log("err: ", err.message);
    return err;
});

Now I need to be able to update a single object in the array and not override the whole array.

This is what the response looks like:

{
  posts:[{_id:'6290ed85716a08d29dab3aa5'}, { _id: '1234ed85716a08d29b35342' } ]
} 

In the following line of code:

let deletePostFromAccountsMicroservice = axios.patch(`${process.env.ACCOUNTS_MICROSERVICE_URL}/router/account/update/${userID}`, {"posts.$[]": "abc"}, (err) =>{
    if(err) return err;
});

I want the posts.$[] to target only the object that has an _id that matches this condition (myUserPostID !== post._id)

How do achieve that?

about 4 years ago · Juan Pablo Isaza
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda