Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/opencv/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Mongodb query.update方法不更新数组_Mongodb_Mongoose_Promise - Fatal编程技术网

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
调用中)。