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  

MySQL Query : SELECT * FROM `codeshelper`.`v9_news` WHERE status=99 AND catid='6' ORDER BY rand() LIMIT 5
MySQL Error : Disk full (/tmp/#sql-temptable-64f5-1ea996c-47d41.MAI); waiting for someone to free some space... (errno: 28 "No space left on device")
MySQL Errno : 1021
Message : Disk full (/tmp/#sql-temptable-64f5-1ea996c-47d41.MAI); waiting for someone to free some space... (errno: 28 "No space left on device")
Need Help?