亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

在mongodb更新中使用變量

在mongodb更新中使用變量

墨色風雨 2019-10-30 13:03:15
我正在使用Meteor進行如下更新:Items.update(Session.get('selectedItem'), {'$set': {'directions.0.name': area.value}})但是我在努力如何動態設置方向的數組索引,就像這樣:var index = //a value determined dynamicallyItems.update(Session.get('selectedItem'), {'$set': {'directions[index]name': area.value}})這不起作用,因為[index]被包裝在字符串中。我還嘗試形成一個自定義字符串,如下所示:var string = 'directions.'+itemIndex+'.name'Items.update(Session.get('selectedItem'), {'$set': {string: area.value}})但這是行不通的。關于如何執行此操作的任何想法?
查看完整描述

1 回答

?
拉風的咖菲貓

TA貢獻1995條經驗 獲得超2個贊

您需要以$set編程方式構建對象:


var setModifier = { $set: {} };

setModifier.$set['directions.' + index + '.name'] = area.value;

Items.update(Session.get('selectedItem'), setModifier);

更新資料


如果您的JavaScript環境支持計算出的屬性名稱(例如,node.js 4+),則可以一步完成:


Items.update(Session.get('selectedItem'), { $set: {

    ['directions.' + index + '.name']: area.value

}});


查看完整回答
反對 回復 2019-10-30
  • 1 回答
  • 0 關注
  • 829 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號