moped
moped copied to clipboard
Collection#aggregate does not support options
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
I just ran into the same issue. +1
Same problem here.
I have the same problem too
+1
+1
+1
+1 Note: with the `cursor' option should return a Cursor and not a Document
+1
+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)
+1
would like to have a possibility to run explain on aggregate
Same here. So huge data can't even be queried from mongoid . This makes it useless.
+1
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