Blub
Blub

Reputation: 13634

findAndModify() works, but equivalent update() doesn't?

Prexisting data: { "posts" : [ { "a" : { "t" : 2}} ] }

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

Data after command: {"posts" : [ { "a" : { "t" : 2, "test" : [ { "d" : 2 } ] } } ] }

That is correct. However the exact same command does not work as update():

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

Output: Thu Nov 24 12:07:02 SyntaxError: invalid property id (shell):1

Why not?

Upvotes: 0

Views: 2083

Answers (1)

mnemosyn
mnemosyn

Reputation: 46311

This looks like a syntax error to me: the update method takes four arguments, two objects and two booleans, not just one object. So,

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

should read

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

Upvotes: 3

Related Questions