findAndModify()有效,但等效的update()不起作用?

时间:2011-11-24 12:11:59

标签: mongodb

  

现有数据:   {"帖子" :[{" a" :{" t" :2}}]}

     
    
      

db.users.findAndModify({query:{" posts.a":{" $ exists":true}},更新:{" $ push&#34 ;:{"帖子。$。a.test":{" d":2}}}})

    
  
     

数据后命令:{" posts" :[{" a" :{" t" :2,   "测试" :[{" d" :2}]}}]}

这是正确的。但是,完全相同的命令不能用作update():

> db.users.update({{"posts.a": {"$exists" : true} }, 
                   {"$push" : {"posts.$.a.test": {"d" : 2}}}, true, false })
  

输出:Thu Nov 24 12:07:02 SyntaxError:无效的属性id(shell):1

为什么不呢?

1 个答案:

答案 0 :(得分:3)

这看起来像是一个语法错误:update方法有四个参数,两个对象和两个布尔值,而不仅仅是一个对象。所以,

db.users.update({{"posts.a": {"$exists" : true} }, 
                {"$push" : {"posts.$.a.test": {"d" : 2}}}, true, false })

应该阅读

db.users.update({"posts.a": {"$exists" : true} }, 
                {"$push" : {"posts.$.a.test": {"d" : 2}}}, true, false)
相关问题