2017-04-19 20 views
1

我正在嘗試編寫一個Mongo更新查詢來更新特定列並使用該字段中的當前數據修改它。因此,而不是:Mongo查詢更新字段並修改當前列中的數據

$set: { 
    displayName: 'test' 
} 

我需要追加一些現有的數據,如:

$set: { 
    displayName: 'test' + displayName 
} 

但上面的語法不工作 - 我只是在尋找的語法簡單地更新列爲當前值+'測試'。

回答

2

更新操作中無法引用文檔字段。您應該將執行移至客戶端並使用forEach

db.collection.find({ /* your query here */}).forEach(function(doc) { 
    db.collection.update({_id: doc._id}, {$set: {displayName: 'test' + doc.displayName}}); 
}) 
相關問題