UpdateMany

2 views
Skip to first unread message

Gregg Lind

unread,
Dec 13, 2009, 10:03:42 AM12/13/09
to mongodb-dev
I'm glad to see all the recent work on update.cpp from Eliot and
Mathias. Since it is still in flux, I won't dig in too deeply!

Digression: As I carp about often in #mongodb, I think the atomic
update feature ($inc and friends) is genius, and really separates
Mongo from other NoSQL solutions. The idea of pushing around
operators rather than 'userialize, update, serialize' is a stronger
pattern, especially when it comes to idempotent operations, like
increment.

I do wonder why the 2001 message (update), only takes one doc and one
spec rather than (doc_or_docs, spec_or_specs). My use case is pretty
simple:

given a list of ids, count/increment them all.

As it stands now, the fastest way of doing it, is to preaggreate by
ids, then do several update ( 'id': id, {counter: {$inc: n_id})
calls, each of which requires getting a cursor, etc.

I don't know if a new message type is necessary, or changes to
UpdateResult and updateObjects is sufficient.

I guess in my vision, *everything* would be an update, since in my
head, inserting is a subset of updating.

I welcome correction and edification.

GL

Eliot Horowitz

unread,
Dec 13, 2009, 10:16:31 AM12/13/09
to mongo...@googlegroups.com
I don't think it makes sense for update to take multiple specs, updates.

However, you can easily solve your problem with the multi-update
option added in 1.1.3

in js, would be:
db.foo.update( { _id : { $in : [ 1 , 2 , 3 ] } } , { $inc : { counter
: 1 } } , false , true );
> --
>
> You received this message because you are subscribed to the Google Groups "mongodb-dev" group.
> To post to this group, send email to mongo...@googlegroups.com.
> To unsubscribe from this group, send email to mongodb-dev...@googlegroups.com.
> For more options, visit this group at http://groups.google.com/group/mongodb-dev?hl=en.
>
>
>

Michael Dirolf

unread,
Dec 13, 2009, 10:16:50 AM12/13/09
to mongo...@googlegroups.com
why not do a multi-update with $in?

On Sun, Dec 13, 2009 at 10:03 AM, Gregg Lind <gregg...@gmail.com> wrote:

Gregg Lind

unread,
Dec 13, 2009, 10:28:17 AM12/13/09
to mongodb-dev
Can you explain this further?

On Dec 13, 9:16 am, Michael Dirolf <m...@10gen.com> wrote:
> why not do a multi-update with $in?
>
> > mongodb-dev...@googlegroups.com<mongodb-dev%2Bunsu...@googlegroups.com>
> > .

Michael Dirolf

unread,
Dec 13, 2009, 10:46:42 AM12/13/09
to mongo...@googlegroups.com
if you pass multi=True to update() then all documents matching the spec will be updated. so you can make a spec that matches all of your documents (using $in for example) and then update multiple documents using a single call to update()

To unsubscribe from this group, send email to mongodb-dev...@googlegroups.com.

Gregg Lind

unread,
Dec 13, 2009, 10:47:26 AM12/13/09
to mongodb-dev
Something in my python call is screwed up here.

def test_update_in(data,summaries):
''' testing update / increment using _in '''
summaries.update({"uid": {"$in": data}} , {"$inc": { "n" : 1 }},
upsert=True, multi=False)

On Dec 13, 9:16 am, Michael Dirolf <m...@10gen.com> wrote:
> why not do a multi-update with $in?
>
> > mongodb-dev...@googlegroups.com<mongodb-dev%2Bunsu...@googlegroups.com>
> > .

Gregg Lind

unread,
Dec 13, 2009, 10:49:33 AM12/13/09
to mongodb-dev
I'm curious about what doesn't make sense to you about it? Is it too
"transaction"-ish?

Thank you for solving this specific case though.

On Dec 13, 9:16 am, Eliot Horowitz <eliothorow...@gmail.com> wrote:
> I don't think it makes sense for update to take multiple specs, updates.
>
> However, you can easily solve your problem with the multi-update
> option added in 1.1.3
>
> in js, would be:
> db.foo.update( { _id : { $in : [ 1 , 2 , 3 ] } } , { $inc : { counter
> : 1 } } , false , true );
>

Gregg Lind

unread,
Dec 13, 2009, 11:26:44 AM12/13/09
to mongodb-dev
@Eliot,

Every time we talk about this, I think you mis-understand what I'm
asking for :)

I'm asking for updates against multiple specs, not the "multi" option
as we have it now, but rather, just like in insert, a series of update
instructions that get batch processed. To make it more concrete,
assume that each update instruction is to one item in the database
(making the multi flag irrelevant)). For example, I have a list of
keys that I want to count. Each has a 'counter' key. I want to pour
in the instruction set: "for each of these ids, increment their
counter". Right now, to do this, one has to call Update separately.
When there are tens of thousands of them, the get and release
connection calls, and other overhead adds up!

@Michael

I don't think the $in operator addresses this correctly, since some
keys get
should get upsert/incremented once, some get upsert/incremented dozens
of times.

imagine this:

data = [1,2,2,3,4,4,3,2,3,4,2,1,3,4,4,4,4,5,6,... ]

Goal: count them.

update($in, multi=true) doesn't really handle this, does it?


Maybe stealthily, I'm asking for a cursor that stays open for multiple
instructions,
which I agree *would* complicate things quite a lot!

Almost all of my operations happen to be updates rather than inserts,
so it rubs me a little
rawer than most :)

Anywho, if it's BDFL'd down here, I won't complain about it again for
a while!

GL




On Dec 13, 9:16 am, Michael Dirolf <m...@10gen.com> wrote:
> why not do a multi-update with $in?
>
> > mongodb-dev...@googlegroups.com<mongodb-dev%2Bunsu...@googlegroups.com>
> > .

Eliot Horowitz

unread,
Dec 13, 2009, 5:33:42 PM12/13/09
to mongo...@googlegroups.com
There isn't much benefit and it adds complication. If you turn off
nagle, you'll basically get identical behavi to that. Or build big
update packets yourself.
Reply all
Reply to author
Forward
0 new messages