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 8066965
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T12:09:00+00:00 2026-06-05T12:09:00+00:00

I try to use out reduce but I do not know how to call

  • 0

I try to use out reduce but I do not know how to call output .
Example:

@results = Article.collection.map_reduce(map, reduce, :out => 'test')

@results.find()
 => <Mongo::Cursor:0x2c276c4 namespace='myapp_development.test' @selector={} @cursor_id=> 

When I try :

@results1 = Article.collection.map_reduce(map, reduce, :out =>{reduce: 'test'}

Expected result is duplicate @results. But I run @results1.find().to_a I see it equal with @results

And how can I call result by namespace in rubyrails using mongoid ??

  • 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-05T12:09:02+00:00Added an answer on June 5, 2026 at 12:09 pm

    TLTR: You can’t have a duplicate result.

    When a map function emit something, and when the reduce function return something, it is a couple [key, value].
    When stored into a collection, for each entry, the output is represented as a Mongoid document like this :

    {
      "_id" => "my key",
      "value" => "my value"
    }
    

    I want to be clear on that point : the Key is stored as the _id, so it is unique in a collection.

    See the Map/Reduce output options to know how MongoDb can deal with duplicate key when outputing into an existing collection :

    • replace (the default) : the content of existing collection is dropped, and the output go into it

    • merge : The existing collection is kept. When a result with the same Key (the _id) exists, it is replaced with the freshly map/reduced one.

    • reduce : The existing collection is kept. When a result with the same Key (the _id) exists, MongoDB takes it, and takes the freshly map/reduced one, and run the reduce function against the two of them, and stores the result.

    So, you can’t have a duplicate result.

    Edit:

    I respond here to “Can you show me how to apply output reduce , and how to call a collection result” (because the response is quite long) :

    There is many way. Let’s take an example among others :

    class Post
      include Mongoid::Document
      include Mongoid::Timestamps
    
      field :tags, :type => Array
    end
    
    Post.create(:tags => ["Dog", "Cat"])
    Post.create(:tags => ["Dog", "Puppy"])
    

    Let’s map/reduce this :

    map = %Q{
      function() {
        this.tags.forEach(function(tag){
          emit(tag, { count: 1 });
        }); 
      }
    }
    
    reduce = %Q{
      function(key, values) {
        var result = { count: 0 };
        values.forEach(function(value) {
          result.count += value.count;
        });
        return result;
      }
    }
    
    Post.map_reduce(map, reduce).out(replace: "tags")
    

    Ok, that put the result in a collection named “tags” overwriting it.

    We can create a Model to access it :

    class Tag
      include Mongoid::Document
    
      field :value, :type => Hash
    end
    
    dog = Tag.find("Dog")
    dog._id # => "Dog"
    dog.value["count"] # => 2
    

    For the fun, let’s say you keep the timestamp of the last time you did the map/reduce. You can do with reduce to do it incrementaly :

    Post.where(:created_at.gt => Time.at(my_timestamp)).map_reduce(map, reduce).out(reduce: "tags")
    

    ** Edit: fixed map function **

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

Sidebar

Related Questions

I try to use doctest from example from http://docs.python.org/library/doctest.html But when I run python
I find this example: ofstream ofstr(output.yaml); YAML::Emitter out(ofstr); out << some_large_document; // not necessary
I'd like to try out a distributed revision control system. I use a couple
I try use string as a regular expression pattern but I've following errors PHP
I try to use the following code: ArrayList<String> Map<String, String> Eclipse complains about both
I am trying to find out where does the output of a Map task
I try to use gallery to show image in network. But when turn left
I'm looking to branch out of plain mysql and mysqli and try to use
Try to use the FEST-Swing for Swing GUI testing, and using example from http://easytesting.org/swing/wiki/pmwiki.php?n=FEST-Swing.LaunchFromMain
I try to use the following code double d; double? a = double.TryParse(3.14, out

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.