markbreneman
markbreneman

Reputation: 33

Removing an Object from an Array in Mongoose/MongoDB

I'm a mongoDB rookie and having trouble removing a team object from an array in MongoDB using Mongoose. My collection for a user looks like;

{  "orders":[],
   "teams":[
      {
         "teamname":"Team One",
         "_id":{"$oid":"566a038404ebcdc344c5136c"},
         "members":[
            {
               "firstname":"Member ",
               "lastname":"Three",`enter code here`
               "email":"[email protected]",
               "initials":"MT",
               "_id":{"$oid":"566a038404ebcdc344c5136d"}
            },
         ]
      },
      {
         "teamname":"Team Two",
         "_id":{"$oid":"566a07f404ebcdc344c51370"},
         "members":[
            {
               "firstname":"Member",
               "lastname":"Two",
               "email":"[email protected]",
               "initials":"MT",
               "_id":{"$oid":"566a07f404ebcdc344c51371"}
            },
         ]
      },
      {
         "teamname":"Team Three",
         "_id":{"$oid":"566a083504ebcdc344c51373"},
         "members":[
            {
               "firstname":"Member",
               "lastname":"Four",
               "email":"[email protected]",
               "initials":"MF",
               "_id":{"$oid":"566a083504ebcdc344c51374"}
            },
         ]
      }
   ],
} 

And my removal route looks like;

exports.getTeamDelete = function(req, res) {

  User.findById(req.user.id, function(err, user) {
    if (err) return next(err);
    var selectedTeamIndex;
    for (i = 0; i < user.teams.length; i++){
      if (user.teams[i]._id==req.params.teamid){
        selectedTeamIndex=i;
        break
      }
    }

    console.log("before", user.teams)
    console.log(req.params.teamid)

    teamID=req.params.teamid;
    userID=req.user.id;

    User.update(
    {'_id': userID},
    { $pull: { "teams" : { teamID } } },
    { multi: true },
                 function(err, data){
                      console.log(err, data);
                 }
    );

    user.markModified("teams");
    user.save(function(err) {
      console.log("after", user.teams)
      if (err) return next(err);
      req.flash('success', { msg: 'Team deleted' });
    });
  });
};

The console logs before and and after the User.update are exactly the same and I get no errors thrown. The output of console.log(err,data) ends up being

null { ok: 1, nModified: 0, n: 1 } 

which I'm not really sure how to interpret. I've tried also not passing in the userID as suggested here;https://stackoverflow.com/a/24289161/574869 to no avail(same results). Additionally I've tried doing the user update as;

User.update(
    {'_id': userID},
    { $pull: { "teams" : { id: teamID } } },
    false,
    true
);

as mentioned here https://stackoverflow.com/a/15641895/574869 which results in and error of;

    /Users/markbreneman/Desktop/GatherRoundApp/node_modules/mongoose/lib/query.js:2022
      oldCb(error, result ? result.result : { ok: 0, n: 0, nModified: 0 });
      ^

TypeError: oldCb is not a function

which I'm also not sure how to interpret. If anyone's got any insights into what I'm doing wrong, or how to easily remove a team from my teams array I'd be greatful.

Upvotes: 3

Views: 2617

Answers (1)

Hiren S.
Hiren S.

Reputation: 2832

User.update( 
    { "_id" : userID} , 
    { "$pull" : { "teams" : { "_id" :  teamID } } } , 
    { "multi" : true }  
)

Upvotes: 1

Related Questions