Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

149
Views
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 answers
Answer question

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 Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!