0
以前我曾試圖像(與貓鼬和承諾):MongoDB的query.update方法不更新陣列
var cursor = User.find({email: from.address, token: tokenMatches[1]});
然後
return cursor.update(
{'votes.title': b},
{'$set': { 'votes.$.category': a }}
).then(function (result) {
if(result.nModified == 0) {
return cursor.update({}, {'$push': { votes: { category: a, title: b }}}).then(function (res) {
ServerLog('updatePush', res);
return res;
});
}
});
但它總是返回n修改= 0的第和第二個電話。直到我發現遊標對象實際上沒有更新功能。那麼爲什麼如此呢?爲什麼它不拋出異常?
好吧,所以它實際上應該工作。令我困惑的是[docs](http://mongoosejs.com/docs/api.html#query_Query-update)將update方法定義爲:'Query#update([criteria],[doc],[options] ,[callback])''但是有時候在沒有條件的情況下使用它,直接傳遞doc:'Model.where({_id:id})。update({title:'words'})' – velop
@如果你沒有在'update'調用中提供它,查詢條件已經添加到'Query'中(例如從你的例子中的where調用中)。 – JohnnyHK