更新仅适用于有时
问题描述:
当试图更新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"
}
]
}
答
我有同样的问题,当我试图更新没有字段名称,请求的对象与$
。我通过使用angular.copy
复制对象进行更新来解决此问题。
db.update(query, { $addToSet: { episodes: angular.copy(data.episodes[0]) } }, { upsert: true }, function(err, numReplaced, upsert) { ... });
angular.copy
创建object
或array
的深层副本。
其他框架和库有类似的方法(example)。
看看这个https://github.com/louischatriot/nedb/issues/229 – GPrathap 2015-01-15 02:03:45
@ user1574779我很感谢你试图帮忙(在这个问题中3个月的沉默之后,我甚至忘了它在这里哈哈) ,但我没有在我的代码中使用位置运算符。 我相信这是一个错误,但恐怕我无法检查,因为我停止开发这个问题的应用对象,现在可能有一个新版本的NeDB。再次查看代码,这可能是导致问题的“upsert”,我不确定是否检查了该问题。 – 2015-01-16 02:36:33