10.1.2 值表達式字串

值表達式字串用於計算值,然後將該值指派給指定的欄位或資料行。這對於 modify()update() 以及在插入時計算文件中的值都是必要的。

值表達式字串的使用範例是遞增計數器。expr() 函數用於包裝字串,否則它們將被逐字解釋。例如,要遞增計數器

// the expression is evaluated on the server
collection.modify('true').set("counter", expr("counter + 1")).execute()

如果您不使用 expr() 包裝字串,它會將字串文字 "counter + 1" 指派給 "counter" 成員

// equivalent to directly assigning a string: counter = "counter + 1"
collection.modify('true').set("counter", "counter + 1").execute()