Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/mongodb/12.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/node.js/35.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本机节点中不起作用_Mongodb_Node.js - Fatal编程技术网

使用$更新阵列在mongodb本机节点中不起作用

使用$更新阵列在mongodb本机节点中不起作用,mongodb,node.js,Mongodb,Node.js,我的mongodb就像下面的一样 { "_id" : ObjectId("4de20ef97065cc77c80541fd"), "todo" : [ { "id" : 1, "desc" : "hi", "done" : 0 }, { "id" : 2, "desc" : "hello", "done" : 0 } ], "user" : "saturngod" } 所以,我像这样更新数据 db.tasks.update({user:'saturngod','todo.id':2},{"$s

我的mongodb就像下面的一样

{ "_id" : ObjectId("4de20ef97065cc77c80541fd"),
"todo" : [
{
"id" : 1,
"desc" : "hi",
"done" : 0
},
{
"id" : 2,
"desc" : "hello",
"done" : 0
}
], "user" : "saturngod" }
所以,我像这样更新数据

db.tasks.update({user:'saturngod','todo.id':2},{"$set":{"todo.$.done":1}});
它在mongodb cli中工作正常,但在节点mongodb本机驱动程序中无法工作

我写了这样的代码

task_collection.update({user:username,'todo.id':taskId}, {"$set":{"todo.$.done":1}},{safe:true},function(error, result){
            sys.puts("callback user:"+username+"id:"+taskId+"error:"+error);
            if( error ) callback(error,result);
            else callback(null,result)

       });
返回空值时出错,回调也起作用。然而,数据库中的数据并没有更新

更新: 我找到了“todo.id”:taskId找不到任何行。它在mongo cli中工作,但在mongodb本机NodeJ中不工作

完整资料来源:
那是因为你有一些打字错误。见评论:

//根据有效的查询,应该是用户名,而不是用户
task_collection.update({user:user,'todo.id':taskId},{“$set”:{“todo.$.done”:1},{safe:true},函数(err,result){
系统puts(“回调用户:+user+”id:+taskId+”错误:+error”);
//错误为null,因为回调参数名称为err,而不是error
if(错误)回调(错误、结果);
else回调(空,结果)
});

已修复,问题是taskId不是数字

task_collection.update({user:username,'todo.id':Math.floor(taskId)}, {"$set":{"todo.$.done":1}},{safe:true},function(error, result){