ruby - MongoDB and MongoRuby: Sorting on mapreduce -


i trying simple mapreduce on documents stored in mongodb. use

map = bson::code.new "function() { emit(this.userid, 1); }" 

for mapping and

reduce = bson::code.new "function(key, values) {    var sum = 0;    values.foreach(function(value) {     sum += value;    });    return sum; }" 

for reduction. works fine when call map_reduce following way:

output = col.map_reduce(map, reduce, # col collection in mongodb, e.g. db.users             {                 :out => {:inline => true},                 :raw => true             }           ) 

now real question: how can use upper call map_reduce enable sorting? the manual says, must use sort , array of [key, direction] pairs. guessed following should work, doesn't:

output = col.map_reduce(map, reduce,             {                 :sort => [["value", mongo::ascending]],                 :out => {:inline => true},                 :raw => true             }           ) 

do have choose datatype? option doesn't work (same error), when using empty [], although manual says default option. unfortunately error message mongodb doesn't much:

/usr/lib/ruby/gems/1.9.1/gems/mongo-1.3.1/lib/mongo/db.rb:506:in `command': database command 'mapreduce' failed: {"assertion"=>"sort has blank or object", "assertioncode"=>13609, "errmsg"=>"db assertion failure", "ok"=>0.0} (mongo::operationfailure)     /usr/lib/ruby/gems/1.9.1/gems/mongo-1.3.1/lib/mongo/collection.rb:576:in `map_reduce'     ./mapreduce.rb:26:in `<main>' 

if need full runnable code, please in comments. exclude contains initialization of connection mongodb , initialization of collection col querying database.

use bson::orderedhash , work.

output = col.map_reduce(map, reduce,     {          :sort => bson::orderedhash.new[{"value", mongo::ascending}],          :out => {:inline => true},          :raw => true     } ) 

Comments

Popular posts from this blog

objective c - Change font of selected text in UITextView -

php - Accessing POST data in Facebook cavas app -

c# - Getting control value when switching a view as part of a multiview -