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()