Uploaded image for project: 'Ruby Driver'
  1. Ruby Driver
  2. RUBY-964

Support String collection name as :out option in #map_reduce

    • Type: Icon: Task Task
    • Resolution: Done
    • Priority: Icon: Blocker - P1 Blocker - P1
    • 2.1.0
    • Affects Version/s: 2.0.6
    • Component/s: None
    • Labels:
      None

      In the process of upgrading from 1.8 to 2.0.6, I've tried to convert the following query:

      col.map_reduce(map, reduce, :query => query, :out => "foobar", :read => :primary)
      {code]
      
      I've rewritten it as follows:
      
      

      col.find(query).map_reduce(map, reduce, :out => "foobar", :read => :primary)

      
      When calling `.to_a` on the above `Mongo::Collection::View::MapReduce` object, I get the following error:
      
      

      NoMethodError: undefined method `values' for "ASEMASMWRE":String
      from /Users/nick/.gem/ruby/2.1.5/gems/mongo-2.0.6/lib/mongo/collection/view/map_reduce.rb:195:in `fetch_query_spec`

      
      It seems that `#fetch_query_spec` always expects a hash as the value of the `:out` option:
      
      

      def fetch_query_spec
      { :selector => {},
      :options => {},
      :db_name => database.name,
      :coll_name => out.values.first } # <-- expects a hash
      end

      
      But looking at the docs, I see that it's valid to also pass just a string: http://docs.mongodb.org/manual/reference/method/db.collection.mapReduce/#output-to-a-collection.
      
      If this behavior was not intented and the driver should really accept strings too, I think that this would be a proper fix:
      
      

      def fetch_query_spec
      { :selector => {},
      :options => {},
      :db_name => database.name,
      :coll_name => out.is_a?(String) ? out : out.values.first } # also accept a string
      end

      
      

      What do you think? If that makes any sense I'd be happy to prepare and submit a PR.

      Thanks.

            Assignee:
            emily.stolfo Emily Stolfo
            Reporter:
            agis Agis
            Votes:
            0 Vote for this issue
            Watchers:
            2 Start watching this issue

              Created:
              Updated:
              Resolved: