开发者

update array with $ is not working in mongodb-native nodejs

My mongodb is like following

{ "_id" : ObjectId("4de20ef97065cc77c80541fd"),
"todo" : [
{
"id开发者_如何转开发" : 1,
"desc" : "hi",
"done" : 0
},
{
"id" : 2,
"desc" : "hello",
"done" : 0
}
], "user" : "saturngod" }

So, I update the data like this.

db.tasks.update({user:'saturngod','todo.id':2},{"$set":{"todo.$.done":1}});

it's working fine in mongodb cli but can't work in your node-mongodb-native driver.

I wrote a code like this

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)

       });

error return null value and callback also working. However, data was not update in database.

Updated: I found 'todo.id':taskId can't find any rows. It's working in mongo cli but not work in mongodb-native nodejs

full source at : https://github.com/saturngod/tatoo/blob/master/data-provider.js


Fixed , problem is taskId is not number.

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


That's because you've got some typos. See the comments:

// Should be username, not user, based on the query that worked
task_collection.update({user:user,'todo.id':taskId}, {"$set":{"todo.$.done":1}},{safe:true},function(err, result){
    sys.puts("callback user:"+user+"id:"+taskId+"error:"+error);
    // error is null because your callback argument name is err, not error
    if( error ) callback(error,result);
    else callback(null,result)
});
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜