Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8361827
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T11:47:30+00:00 2026-06-09T11:47:30+00:00

In mongoid 2, this used to work: mr_collection = self.collection.map_reduce(map, reduce, { :query =>

  • 0

In mongoid 2, this used to work:

mr_collection = self.collection.map_reduce(map, reduce, {
  :query => query,
  :finalize => finalize,
  :out => {:replace => 'mr_results'}
})

limit = (options[:limit] || 10)
skip = (options[:skip].to_i || nil)
page = if skip >= limit
  ((skip+limit) / limit)
else
  1
end

sort = if options[:sort_by_vintage]
  [['value.vy', :desc], ['value.s', (options[:sort] || :desc)], ['value.pml', :asc]]
elsif options[:sort_by_sDate]
  [['value.sDate', :desc], ['value.s', (options[:sort] || :desc)], ['value.pml', :asc]]
else
  [['value.s', (options[:sort] || :desc)], ['value.pml', :asc]]
end
paginator = WillPaginate::Collection.new(page, limit, collection_count)
collection = mr_collection.find({},{
    :sort => sort,
    :limit => limit,
    :skip => skip
  }
).to_a

I have updated the map_reduce call to be:

mr_collection = self.where(query).map_reduce(map, reduce).finalize(finalize).out({:replace => 'mr_results'})

Which does not produce any errors any more, but the collection = mr_collection.find…. always fails no matter what I try. Here are a few attempts:

(rdb:1) mr_collection.find.sort(sort)

which produces
.rvm/rubies/ruby-1.9.3-p194/lib/ruby/1.9.1/debug.rb:130:in `eval’:wrong number of arguments(1 for 0)

I can see that
(rdb:1) mr_collection.class
Mongoid::Contextual::MapReduce

(rdb:1) mr_collection.find.class
Enumerator

Trying:
(rdb:1) mr_collection.sort(sort)
.rvm/rubies/ruby-1.9.3-p194/lib/ruby/1.9.1/debug.rb:130:in `eval’:wrong number of arguments(1 for 0)
so same error

Thanks for your help

UPDATE

Fixed it by using:

collection = mr_collection.find(
    :sort => sort,
    :limit => limit,
    :skip => skip
 )

My problem is now using collection.to_a, which I know works well for regular hashes, but the results in the collection are of type Moped::BSON::Document. Calling any Enumerator method on collection, results in this error:

undefined method `call' for #<Hash:

I’m going mad. Please help!!

So of the things I tried include:

collection = collection.each {|c| c.to_hash}.to_a

and

collection = collection.collect {|c| c.to_hash}.to_a

Thanks 🙂

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-09T11:47:31+00:00Added an answer on June 9, 2026 at 11:47 am

    Finally figured it out thanks to the Mongoid google group. Details here: https://groups.google.com/d/topic/mongoid/T6XhqLtofTE/discussion

    The one liner fix is:

    collection = mr_collection.send(:documents).sort(sort).limit(limit).skip(skip).to_a
    

    In an upcoming version of mongoid, Mongoid::Contextual::MapReduce#documents will be changed from a private method to a public one, and the .send(:documents) won’t be needed anymore.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have my mongoid query in the model def self.get_result collection_name, hash_params, page, per_page
self here stands for Contact model. This query will find all completed tasks for
I have some problem with this mongoid. It's my first time to use mongoDB,
I have a relationship like this class User include Mongoid::Document include Mongoid::Timestamps embeds_one :setting
Rails 3.2.1 app name: demo database: mongoDB with mongoid I have this scaffolding set
I want to use this function from mongoid: person.update_attributes(first_name: Jean, last_name: Zorg) But I
I'm trying to use the MongoID-Devise generator template by doing this under Ubuntu and
I am trying to achieve the equivalent of this in mongoid (mongoid.org ORM): select
I'm using the latest mongoid... How do I do the mongoid equivalent of this
I have to read an existing mongo database. Is mongoid suitable for this? All

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.