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

2 Answers 2

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){
Sign up to request clarification or add additional context in comments.

Comments

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)
});

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.