2014-10-03 40 views
0

當試圖更新NeDB數據庫中的數據時,我得到Field names cannot begin with the $ character但是它只發生在第二次或第三次更新數據庫之後。更新僅適用於有時

下面是我使用更新的代碼/插入數據:

addNew: function(data) { 
    var deferred = $q.defer(), 
     query = { 
     _id: data._id, 
     title: data.title, 
     year: data.year, 
     genres: data.genres, 
     poster: data.poster 
     }; 

    db.update(query, { $addToSet: { episodes: data.episodes[0] } }, { upsert: true }, function(err, numReplaced, upsert) { 
    if (!err) { 
     var data = numReplaced + '/' + upsert; 
     deferred.resolve(data); 
    } else { 
     deferred.reject(err); 
    } 
    }); 

    return deferred.promise; 
} 

data.episodes[0]看起來是這樣的:

{ 
    "number": 5, 
    "season": 3, 
    "title": "Kissed by Fire", 
    "overview": "The Hound is judged by the gods. Jaime is judged by men. Jon proves himself. Robb is betrayed. Tyrion learns the cost of weddings.", 
    "screen": "http://slurm.trakt.us/images/episodes/1395-3-5.80.jpg" 
} 

中DB我試圖更新看起來像這樣的數據:

{ 
    "_id": "tt0944947", 
    "title": "Game of Thrones", 
    "year": 2011, 
    "genres": "Drama, Fantasy, Adventure", 
    "poster": "http://slurm.trakt.us/images/posters/1395-300.jpg", 
    "episodes": [ 
    { 
     "number": 10, 
     "season": 4, 
     "title": "The Children", 
     "overview": "Circumstances change after an unexpected arrival from north of the Wall. Daenerys must face harsh realities. Bran learns more about his destiny. Tyrion sees the truth about his situation.", 
     "screen": "http://slurm.trakt.us/images/episodes/1395-4-10.80.jpg" 
    } 
    ] 
} 
+0

看看這個https://github.com/louischatriot/nedb/issues/229 – GPrathap 2015-01-15 02:03:45

+0

@ user1574779我很感謝你試圖幫忙(在這個問題中3個月的沉默之後,我甚至忘了它在這裏哈哈) ,但我沒有在我的代碼中使用位置運算符。 我相信這是一個錯誤,但恐怕我無法檢查,因爲我停止開發這個問題的應用對象,現在可能有一個新版本的NeDB。再次查看代碼,這可能是導致問題的「upsert」,我不確定是否檢查了該問題。 – 2015-01-16 02:36:33

回答

0

我有同樣的問題,當我試圖更新沒有字段名稱,請求的對象與$。我通過使用angular.copy複製對象進行更新來解決此問題。

db.update(query, { $addToSet: { episodes: angular.copy(data.episodes[0]) } }, { upsert: true }, function(err, numReplaced, upsert) { ... }); 

angular.copy創建objectarray的深層副本。

其他框架和庫有類似的方法(example)。

相關問題