Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Mongoose update 'cannot use the part (..) to traverse the element

Tags:

I have this really annoying issue where i can't update anything using mongoose. It's really frustrating to use, and the documentation is not helping at all.

I have this schema:

var userSchema = mongoose.Schema({      local            : {         email        : String,         password     : String,     },     devices : [{       id : String,       name : String     }] }); 

And this is the code where i want to add a device to the array devices :

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

At this point i get the error: errmsg: 'cannot use the part (devices of devices.id) to traverse the element ({devices: []})' }

I didn't manage to find an explanation to why this is happening. I have to mention that the document (there is pretty much only one document in the database), is this one:

{     "_id": {         "$oid": "5585a196fe11b21100635c74"     },     "devices": [],     "local": {         "password": "$2a$10$7hXVHw7izcYlqbD6xe/te.0w2zucZ7lA007g9kXdoIMPhZhRyCIru",         "email": "[email protected]"     },     "__v": 0 } 
like image 647
Edeph Avatar asked Jun 20 '15 17:06

Edeph


People also ask

How to update data in array of object in MongoDB?

Update Documents in an ArrayThe positional $ operator facilitates updates to arrays that contain embedded documents. Use the positional $ operator to access the fields in the embedded documents with the dot notation on the $ operator.

How to update embedded array document in MongoDB?

To perform an update on all embedded array elements of each document that matches your query, use the filtered positional operator $[<identifier>] . The filtered positional operator $[<identifier>] specifies the matching array elements in the update document.

What is update query in mongoose?

Mongoose | update() Function The update() function is used to update one document in the database without returning it.


2 Answers

Try to use the positional $ operator in the update which identifies an element in an array to update without explicitly specifying the position of the element in the array, but this will only ever match one element at a time:

 User.update(     {          "local.email": user,         "devices.id": { "$ne": deviceID },         "devices.name": { "$ne": deviceName }     },      {          "$set": {              "devices.$.id": deviceID,             "devices.$.name": deviceName          }     }  ); 

From the docs, the positional $ operator acts as a placeholder for the first element that matches the query document, and the array field must appear as part of the query document hence the query document

"devices.id": { "$ne": deviceID }, "devices.name": { "$ne": deviceName } 

contains the device array and will match those documents where the device array id is not equal to deviceID and the name is not the same as the name which you are trying to update. This will even match documents where the device array is empty.

like image 139
chridam Avatar answered Sep 22 '22 13:09

chridam


Use the $push or other array update operators to add elements to an array. For details, refer http://docs.mongodb.org/manual/reference/operator/update/push/#up._S_push

like image 25
Sasikanth Bharadwaj Avatar answered Sep 24 '22 13:09

Sasikanth Bharadwaj