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

151
Vistas
How to pull from array of objects with array of objects in MongoDB?

I was trying to pull multiple objects from an array of objects, and I found this article

Using MongoDB $pull to delete documents within an Array

so here's my schema and how I did,but it does'nt work

{
        "_id": "61fed9b89763c1c3b886b74f",
        "TeamName": "Hogwarts",
        "TeamImage": "Avatar ",
        "createdAt": "2022-02-05T20:10:32.885Z",
        "updatedAt": "2022-02-05T20:59:51.359Z",
        "__v": 0,
        "TeamMember": [
            {
                "Name": "Ronne",
                "Email": "test4@gmail.com",
                "_id": "61fee1807df64451141f08df"
            },
            {
                "Name": "Kai",
                "Email": "school021195@gmail.com",
                "_id": "61fee1e3fffd0f55ed92caee"
            },
            {
                "Name": "Selina",
                "Email": "test@gmail.com",
                "_id": "61fee1e3fffd0f55ed92caef"
            },
            {
                "Name": "Jessica Wu",
                "Email": "test1@gmail.com",
                "_id": "61fee1e3fffd0f55ed92caf0"
            },
            {
                "Name": "Hormione",
                "Email": "test3@gmail.com",
                "_id": "61fee1807df64451141f08de"
            }
        ]
    },

Delete method

const team = await Team.findOneAndUpdate(
        {_id: TeamId},
        {
          $pull: { TeamMember: [ {Name: "Ronne"  },{ Name : "Hormione"} ] },
        //   $pull: { TeamMember: {$in:[ {Name: "Ronne"  },{ Name : "Hormione"} ]}},
          
        },
        { new: true,multi: true}
      );

I also try the $in method, but both don't work, did I miss something? or what's the right way to do multiple pulls using MongoDB?

BTW, I am using Mongoose for my schema, I don't know if it matters.

about 4 years ago · Juan Pablo Isaza
1 Respuestas
Responde la pregunta

0

Try using this way

const team = await Team.findOneAndUpdate(
        {_id: TeamId},
        {
          $pull: { TeamMember: {Name: {$in: ["Ronne","Hormione"] } } },          
        },
        { new: true,multi: true}
      );

https://mongoplayground.net/p/Sqo83w8YvVU

about 4 years ago · Juan Pablo Isaza Denunciar
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