3

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.

Oleg
  • 2,650
  • 3
  • 32
  • 45

2 Answers2

5

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!

morten.c
  • 3,188
  • 5
  • 36
  • 43
DanNut
  • 101
  • 1
  • 8
1

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.

spacedev
  • 624
  • 7
  • 12
  • actually if scale has true or false value it is pretty reasonable to receive this error. – Oleg Jun 27 '18 at 09:42