用$更新数组在mongodb-native nodejs中不起作用 [英] update array with $ is not working in mongodb-native nodejs

查看:66
本文介绍了用$更新数组在mongodb-native nodejs中不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的mongodb就像下面的

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

它在mongodb cli中可以正常工作,但不能在您的node-mongodb-native驱动程序中工作.

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

我写了这样的代码

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)

       });

错误返回null值,并且回调也有效.但是,数据库中的数据未更新.

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

已更新: 我发现"todo.id":taskId找不到任何行.它可以在mongo cli中工作,但不能在mongodb本地nodejs中工作

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

完整资料来源: https://github.com/saturngod/tatoo/blob/master/data -provider.js

推荐答案

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

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){

这篇关于用$更新数组在mongodb-native nodejs中不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆