simple update question

13 views
Skip to first unread message

Lincoln

unread,
Aug 24, 2009, 5:28:14 PM8/24/09
to google groups
Hey guys,

If I want to update all docs in a collection using update(), what should the doc for query look like?  Ideally, could someone show me what it would look like with the java driver?

I was trying to do the following:
db.collection.update(new DBO(), new DBO("field", "value"), false, false)

And it looks like it would only update a single random document in the collection.

Thanks,
Lincoln

dwight_10gen

unread,
Aug 24, 2009, 8:05:11 PM8/24/09
to mongodb-user
update works on a single object. perhaps in the future it should
allow updating many objects with things like $set -- originally those
operations didn't exist so that didn't make sense.

for now you can use db.eval() to do a series of updates server-side.
here's an example in shell syntax:

> db.foo.find()
{"_id" : ObjectId( "4a932a282130f2104ff63022") , "x" : 1}
{"_id" : ObjectId( "4a932a292130f2104ff63023") , "x" : 3}

> function f() { db.foo.find().forEach( function(o){o.y="abc";db.foo.save(o);} ) }

> db.eval(f)
null

> db.foo.find()
{"_id" : ObjectId( "4a932a282130f2104ff63022") , "x" : 1 , "y" :
"abc"}
{"_id" : ObjectId( "4a932a292130f2104ff63023") , "x" : 3 , "y" :
"abc"}

Dwight Merriman

unread,
Aug 24, 2009, 8:16:28 PM8/24/09
to google groups
I will look tomorrow to see how hard this would be to add (multi-update
support)

Lincoln

unread,
Aug 24, 2009, 8:22:21 PM8/24/09
to mongod...@googlegroups.com
Thanks Dwight!  That clears it up.

Alexander

unread,
Aug 28, 2009, 10:04:05 AM8/28/09
to mongodb-user
http://jira.mongodb.org/browse/SERVER-268

On Aug 25, 2:16 am, Dwight Merriman <dwi...@exch.alleycorp.com> wrote:
> I will look tomorrow to see how hard this would be to add (multi-update
> support)
>
> On 8/24/09 8:05 PM, "Dwight Merriman" <dwi...@10gen.com> wrote:
>
>
>
>
>
> >update works on a single object.  perhaps in the future it should
> > allow updating many objects with things like $set -- originally those
> > operations didn't exist so that didn't make sense.
>
> > for now you can use db.eval() to do a series of updates server-side.
> > here's an example in shell syntax:
>
> >> db.foo.find()
> > {"_id" :  ObjectId( "4a932a282130f2104ff63022")  , "x" : 1}
> > {"_id" :  ObjectId( "4a932a292130f2104ff63023")  , "x" : 3}
>
> >> function f() { db.foo.find().forEach( function(o){o.y="abc";db.foo.save(o);}
> >> ) }
>
> >> db.eval(f)
> > null
>
> >> db.foo.find()
> > {"_id" :  ObjectId( "4a932a282130f2104ff63022")  , "x" : 1 , "y" :
> > "abc"}
> > {"_id" :  ObjectId( "4a932a292130f2104ff63023")  , "x" : 3 , "y" :
> > "abc"}
>
> > On Aug 24, 5:28 pm, Lincoln <linxbet...@gmail.com> wrote:
> >> Hey guys,
> >> If I want toupdateall docs in a collection usingupdate(), what should the
> >> doc for query look like?  Ideally, could someone show me what it would look
> >> like with the java driver?
>
> >> I was trying to do the following:
> >> db.collection.update(new DBO(), new DBO("field", "value"), false, false)
>
> >> And it looks like it would onlyupdatea single random document in the
> >> collection.
>
> >> Thanks,
> >> Lincoln

Lincoln

unread,
Aug 28, 2009, 3:57:46 PM8/28/09
to mongod...@googlegroups.com
Thanks this makes much more sense to me.
Reply all
Reply to author
Forward
0 new messages