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.