0

I cannot get to update my documents using mongoose with the positional $ operator. This is a follow-up question strongly related to this question : Mongoose update 'cannot use the part (..) to traverse the element

I looked up here : http://docs.mongodb.org/manual/reference/operator/update/positional/ and did exactly as they do in the example:

    User.update({'local.email' : user, 'devices.id' : deviceID},
   {$set : {'devices.$.agenda' : agenda}}, function(err, response)
   {
     if(err)
     {
       console.log("Update agenda error", err);
     }
     else {
       console.log("Update agenda OK");
       console.log("Response", response);
     }
   });

But response returns

 Response { ok: 1,
   nModified: 0,
   n: 0,
   lastOp: { _bsontype: 'Timestamp', low_: 2, high_: 1434882344 },
   electionId: 555637ca17aac10c2bbc5d84 }

So nothing gets modified. I tried to query only to see if i manage to find the requested ID and i cannot find it even though i use the exact same variables i used for insertion.

UPDATE The user schema:

    var userSchema = mongoose.Schema({

    local            : {
        email        : String,
        password     : String,
    },
    devices : [{
      id : String,
      name : String,
      agenda : String
    }]
});

UPDATE #2

The code that makes the updates is the following :

function updateDeviceList(user, deviceID, deviceName)
{
  User.update({ 'local.email' : user},
  { $push: {'devices' : {'id': deviceID, 'name':deviceName, 'agenda' : ""}}},
  function(err, response)
  {
    if(err)
    {
      console.log("Update device error", err);
    }
    else {
      console.log("Update device OK");
    }
  });
}

function updateAgenda(user, deviceID, agenda)
{
  User.findOneAndUpdate({'local.email' : user, 'devices.id' : deviceID},
  {$set: {"devices.$.agenda" : agenda}},
  {   select: {
            'devices': {
               $elemMatch:
               {   'id' : deviceID }
            }
        }
  },
  function(err, response)
  {
    if(err)
    {
      console.log("Update agenda error", err);
    }
    else {
      console.log("Updated agenda OK");
      console.log("respnse", response);
    }
  });
  console.log("user", user);
  console.log("deviceID", deviceID);
  console.log("agenda", typeof agenda);
}

The user and the deviceID are both strings and are the same values used in both functions. Agenda console.log returns string.

Community
  • 1
  • 1
Edeph
  • 732
  • 2
  • 12
  • 29
  • @JohnnyHK i added the schema – Edeph Jun 21 '15 at 13:15
  • Schema looks fine for that query, so it's down to what's the doc you're looking to update and what are the values of `user`, `deviceID`, and `agenda`? – JohnnyHK Jun 21 '15 at 13:25
  • All three are strings, and they are the same vars i used to insert into the database, except agenda that needs to be updated after the device is inserted. – Edeph Jun 21 '15 at 13:42
  • @JohnnyHK i do get the modified:1 now in response. Do i have to save the new document or anything? – Edeph Jun 21 '15 at 13:43
  • Nope, it sounds like it's working properly. – JohnnyHK Jun 21 '15 at 13:48
  • Yet my agenda doesn't get updated :( any other ideas how can i test to see what doesnt work? – Edeph Jun 21 '15 at 13:48
  • What did you change to get the `nModified: 1` in the response? – JohnnyHK Jun 21 '15 at 13:56
  • I actually had the id null, it slipped to me. It is weird because nothing gets changed in the db. – Edeph Jun 21 '15 at 13:58
  • Can you provide a complete example that reproduces the problem? – JohnnyHK Jun 21 '15 at 14:18
  • @JohnnyHK i added the update, let me know if you need any other details. I switched between `findOneAndUpdate` and `update` methods hoping at least one would work, but they both act the same. In my opinion, logic-wise, i should use `findOneAndUpdate` method, but as long as the entry gets updated, i am fine with any of them. – Edeph Jun 21 '15 at 14:25
  • The update method is the first one posted in the question – Edeph Jun 21 '15 at 14:28
  • 2
    If you're calling those two methods one after the other then you'll get async issues. – JohnnyHK Jun 21 '15 at 15:09
  • @JohnnyHK i just noticed something weird. Initially my `agenda` has value `""` (empty string), but after the update it changes to `{}`. Why would this happen ? – Edeph Jun 21 '15 at 15:32
  • I was seriously not paying attention to my setup... i was testing from a device that actually had no contacts and that's why it was changing from "" to {}. Thank you so much @JohnnyHK! – Edeph Jun 21 '15 at 16:00

0 Answers0