moped icon indicating copy to clipboard operation
moped copied to clipboard

Collection#aggregate does not support options

Open glampr opened this issue 10 years ago • 13 comments

I have found no way to pass options to the Collection#aggregate method as explained here: http://docs.mongodb.org/manual/reference/method/db.collection.aggregate/ for example to allowDiskUse for external sort operations. Maybe a patch like this should be considered? http://stackoverflow.com/questions/23302047/adding-allowdiskuse-parameter-to-db-collection-aggregate-query-using-mongoid

glampr avatar Jun 08 '14 20:06 glampr

I just ran into the same issue. +1

execdd17 avatar Jun 09 '14 18:06 execdd17

Same problem here.

jiop avatar Jul 08 '14 09:07 jiop

I have the same problem too

srilumpa avatar Jul 08 '14 09:07 srilumpa

+1

semarco avatar Jul 29 '14 13:07 semarco

+1

azevedo avatar Jan 19 '15 15:01 azevedo

+1

adlersantos avatar Feb 24 '15 06:02 adlersantos

+1 Note: with the `cursor' option should return a Cursor and not a Document

topac avatar Jul 03 '15 12:07 topac

+1

jasoares avatar Jul 03 '15 17:07 jasoares

+1 Same issue as @topac , would like to pass options to the Moped::Collection#aggregate method. http://docs.mongodb.org/manual/reference/method/db.collection.aggregate/#db.collection.aggregate (see the option cursor)

fmquaglia avatar Jul 24 '15 09:07 fmquaglia

+1

would like to have a possibility to run explain on aggregate

y9v avatar Feb 08 '16 10:02 y9v

Same here. So huge data can't even be queried from mongoid . This makes it useless.

nitvik avatar Sep 28 '16 09:09 nitvik

+1

vetalpaprotsky avatar Apr 19 '18 16:04 vetalpaprotsky

I just monkeypatched it in a Rails initializer to work around this:

# config/initializers/moped_aggregate.rb
require "moped"

if !defined?(Moped) || Gem::Version.new(Moped::VERSION) > Gem::Version.new('2.0.7')
  raise 'Moped has been updated or removed. Please confirm that '\
        '`config/initializers/moped_aggregate.rb` is still needed.'
end

module Moped
  class Collection
    # Mongo 3.6 requires a `cursor` option be passed as part of aggregate queries.  This overrides
    # `Moped::Collection#aggregate` to include a cursor, which is not provided by Moped otherwise.
    #
    # Per the [MongoDB documentation](https://docs.mongodb.com/manual/reference/command/aggregate/):
    #
    #   Changed in version 3.6: MongoDB 3.6 removes the use of `aggregate` command *without* the `cursor` option unless
    #   the command includes the `explain` option. Unless you include the `explain` option, you must specify the
    #   `cursor` option.
    #
    #   To indicate a cursor with the default batch size, specify `cursor: {}`.
    #
    #   To indicate a cursor with a non-default batch size, use `cursor: { batchSize: <num> }`.
    #
    def aggregate(*pipeline)
      # Ordering of keys apparently matters to Mongo -- `aggregate` has to come before `cursor` here.
      extract_result(session.command(aggregate: name, pipeline: pipeline.flatten, cursor: {}))
    end

    private

    def extract_result(response)
      response.key?("cursor") ? response["cursor"]["firstBatch"] : response["result"]
    end
  end
end

mhuggins avatar May 23 '18 18:05 mhuggins