Mongodb query.update方法不更新数组
以前我尝试过类似(猫鼬和承诺)的东西:Mongodb query.update方法不更新数组,mongodb,mongoose,promise,Mongodb,Mongoose,Promise,以前我尝试过类似(猫鼬和承诺)的东西: 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 cu
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;
});
}
});
但对于第一次和第二次调用,它始终返回nModified=0。直到我发现游标对象实际上没有更新函数。为什么会这样?为什么它没有抛出异常?返回一个对象,而不是光标<代码>查询
确实有一个方法,可以将查询作为更新操作执行。返回一个对象,而不是光标<代码>查询确实有一个方法,可以让您将查询作为更新操作执行。好的,因此它实际上应该可以工作。让我困惑的是,将更新方法定义为:Query#update([criteria],[doc],[options],[callback])
,但有时在没有条件的情况下使用它并直接传递doc:Model.where({u id:id}).update({title:'words'})
@develop如果在更新
调用本身中没有提供,则更新将使用已添加到查询
中的查询条件(例如,在您的示例中从where
调用中)。好的,因此它实际上应该可以工作。让我困惑的是,将更新方法定义为:Query#update([criteria],[doc],[options],[callback])
,但有时在没有条件的情况下使用它并直接传递doc:Model.where({u id:id}).update({title:'words'})
@develop如果您在更新
调用本身中未提供,则更新将使用已添加到查询
中的查询条件(例如,在您的示例中从where
调用中)。