当尝试更新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 I尝试更新的数据如下所示:
{
"_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 :(得分:0)
当我尝试更新没有以$
开头的字段名称的对象时,我遇到了同样的问题。我通过使用angular.copy
复制要更新的对象来解决这个问题。
db.update(query, { $addToSet: { episodes: angular.copy(data.episodes[0]) } }, { upsert: true }, function(err, numReplaced, upsert) { ... });
angular.copy
会创建object
或array
的深层副本。
其他框架和库有类似的方法(example)。