2016-11-09 284 views
2

我想更新現有的文檔,其中有一個空值的字段,我得到以下錯誤。

文件:

{ 
    "_id" : ObjectId("582299f71e21dbf65027325e"), 
    "b" : "5555",  
    "f" : null 
} 

查詢:

db.getCollection('temp').update({"b":"5555"},{"$set":{"f.1.b":1,"f.2.b":2}}) 

錯誤:

WriteResult({ 
    "nMatched" : 0, 
    "nUpserted" : 0, 
    "nModified" : 0, 
    "writeError" : { 
     "code" : 16837, 
     "errmsg" : "cannot use the part (f of f.1.b) to traverse the element ({f: null})" 
    } 
}) 

誰能告訴我,爲什麼它是不更新文檔中的值。

謝謝。

回答

2

因爲有發現設置 在這種情況下,你可以試試這個 準備一個合適的對象,並嘗試設置 例如沒有f.1.b and f.2.b對象:

var temp = { 
1 : {b: 1}, 
2: {b : 2} 
} 
db.getCollection('temp').update({"b":"5555"},{"$set":{f:temp}}) 
+1

它工作正常,如果我的目標是零和在有些值已經存在的情況下,它將取代現有的對象。感謝您的回答。 – Prabhakaran

+0

歡迎你,如果你有解決方案,你可以打勾標記這個答案,感謝 –

相關問題