Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Mongodb cannot use the part (...) to traverse the element ({...: undefined})]

After upgrade to 3.0 mongo driver i am receiving some new error on update request. For update like this:

db.table.update({_id: .... } , {$set : { "tags.Tag1" : true }});

I am receiving

cannot use the part (tags of tags.Tag1) to traverse the element ({tags: null})]]

The problem is that my updated document already contains default value for tags : null. If I manually remove it from document , update starts to work correctly. It is some new behavior for me , and it is happens after updating mongo driver from 2 to 3 ( not even database itself).

But now I wonder now how to avoid this error. I can of course check if "tags" already defined and only then make $set to element or the whole map. But it means 3 requests vs one old and the other problems like atomicity.

like image 895
Oleg Avatar asked Mar 12 '23 08:03

Oleg


2 Answers

Although it's an old post but I think what you are looking for is the $ positional operator

I am guessing your "tags" is an array. So the above example could be something like

db.table.update({_id: .... } , {$set : { "tags.$.Tag1" : true }});

Hope it helps!

like image 139
DanNut Avatar answered Apr 19 '23 10:04

DanNut


Yes, it can be updated... I had resolved similar problem

Resolved this

db.table.updateById({_id: .... } , {$set : { "levelSpecificData.scale.uom": "feet"}});

5b1f566ed65c7dcc34aaa7d5 MongoError: cannot use the part (scale of levelSpecificData.scale.uom) to traverse the element ({scale: false})

where in my collection 'levelSpecificData.scale' was a Boolean type T/F

I changed the default value type of levelSpecificData.scale to '{}' empty object... Surprisingly it worked fine after changing default values to object, since I want to treat scale as an object reference this solution was all good for it.

like image 22
spacedev Avatar answered Apr 19 '23 09:04

spacedev