Scott,
Thanks for the quick reply!
For what it's worth, in a non-sharded environment (in 1.8 and 2.0) the
non-intended usage works perfectly.
Either by accident or by design, you can safely update collections in
the same DB from the reduce() function. My extreme gratitude to
whomever is responsible for that slip :)
The link you provided is an interesting option. It will certainly
solve field renaming and simple administrative tasks like that.
We also have more requirements like:
1] Go through all documents (say 10-100M), create a temporary
collection of all locations referenced in the documents (say 10-100M;
obviously this is done using "normal" map/reduce)
then:
2] Go through a master list of all known locations and update with
various computed statistics from the temp collection
3] Go through the temp collection and update some fields within the
documents with various computed statistics (eg
update({docs.locations._id: blah1},{$set:{docs.locations.$.stats:
blah2}}, false, true))
Would the plan with a use case like this be something like (eg for
[3]):
db.documents.update({}, {$script: "for (var loc in doc.locations)
{ var update_loc = db.tmp_locs.find({_id:loc._id}); <etc> }"}, false,
true)?
That might well work, thanks (I think I recall that -empirically-
looping over locations then updating documents was preferable to
looping over documents and then updating locations, but I don't
remember how big the difference was, and I didn't investigate whether
it was real).
Any other ideas for correctly supporting use cases like the one above?
I'll certainly try to replace our map/reduce hack once this update
feature is available.
In the meantime ... I'm guessing, since I'm taking advantage of an
unsupported feature/bug, that you're not going to tell me whether it
will work in a sharded environment? :)
I will say, that (apart from the double emit and some generic
grovelling to handle reduce edge cases) I found in practice that the
map/reduce "bug" is a really nice way of writing fairly complex batch
update scripts for a variety of purposes... I guess kudos to 10gen for
even having useful bugs!
On Jan 9, 7:25 pm, Scott Hernandez <
scotthernan...@gmail.com> wrote:
> Map/Reduce is not meant to do things like this. It is not allowed to
> update collections in the map or reduce functions.
>
> There is an open feature request to support expressions (or
> javascript) in the update operators.
https://jira.mongodb.org/browse/SERVER-458