javascript - MongoDB更新语句中如何引用变量?
下面语句可以执行成功:
1 2 | <code>Basket.update({ "pname" :pname}
,{ "$set" :{ "pprice.1.2" :123}});</code>
|
但是"pprice.1.2"中,"1.2"需要是变量的值,1取emonth的值,2取eday的值。
我创建新的变量,eprice="pprice."+emonth+"."+eday;
但是下面语句执行后没效果:
1 2 | <code>Basket.update({ "pname" :pname}
,{ "$set" :{eprice:123}});</code>
|
目前是没有"pprice"这个键的。
修改为"pprice.${emonth}.${eday}",出现下面错误:
1 2 3 4 5 6 7 | <code>{ [MongoError: The dollar ($) prefixed field '${eday}' in 'pprice.${emonth}.${eday}' is not valid for storage.]
name: 'MongoError' ,
message: 'The dollar ($) prefixed field \'${eday}\' in \'pprice.${emonth}.${eday}\' is not valid for storage.' ,
driver: true,
index: 0,
code: 52,
errmsg: 'The dollar ($) prefixed field \'${eday}\' in \'pprice.${emonth}.${eday}\' is not valid for storage.' }</code>
|
下面语句执行成功:
1 | <code> "$set" :{[`pprice.${emonth}.${eday}`]:pprice}</code>
|
PHPz2898 hari yang lalu287