On the problem of mongodb nesting operation

the operation of two-tier nesting is normal.

db.collection.update({"xxx": "xxx"}, {$inc: {"aaa.bbb": 1}})

but now if bbb is uncertain, it is a variable, such as

let x = "bbb";
db.collection.update({"xxx": "xxx"}, {$inc: {`aaa.${x}`: 1}})

this will go wrong. What should I do?

Dec.01,2021

this has nothing to do with mongodb . It is a syntax error of js and cannot use an expression as an attribute of an object. When you use backquotes `aaa.$ {x} ` becomes an expression, the syntax will fail.
to achieve results, you can use the following two ways

  object extension  

Menu