2

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

saturngod
  • 24,649
  • 17
  • 62
  • 87

2 Answers2

1

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){
saturngod
  • 24,649
  • 17
  • 62
  • 87
0

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)
});
onteria_
  • 68,181
  • 7
  • 71
  • 64