Class: Mongo::Protocol::Query::Upconverter

Inherits:
Object
  • Object
show all
Defined in:
lib/mongo/protocol/query.rb

Overview

Converts legacy query messages to the appropriare OP_COMMAND style message.

Since:

  • 2.1.0

Constant Summary

OPTION_MAPPINGS =

Mappings of the options to the find command options.

Since:

  • 2.1.0

{
  :project => 'projection',
  :skip => 'skip',
  :limit => 'limit',
  :batch_size => 'batchSize'
}.freeze
SPECIAL_FIELD_MAPPINGS =

Since:

  • 2.1.0

{
  :$readPreference => 'readPreference',
  :$orderby => 'sort',
  :$hint => 'hint',
  :$comment => 'comment',
  :$returnKey => 'returnKey',
  :$snapshot => 'snapshot',
  :$maxScan => 'maxScan',
  :$max => 'max',
  :$min => 'min',
  :$maxTimeMS => 'maxTimeMS',
  :$showDiskLoc => 'showRecordId',
  :$explain => 'explain'
}.freeze
FLAG_MAPPINGS =

Mapping of flags to find command options.

Since:

  • 2.1.0

{
  :tailable_cursor => 'tailable',
  :oplog_replay => 'oplogReplay',
  :no_cursor_timeout => 'noCursorTimeout',
  :await_data => 'awaitData',
  :partial => 'allowPartialResults'
}.freeze
FIND =

Find command constant.

Since:

  • 2.1.0

'find'.freeze
FILTER =

Filter attribute constant.

Since:

  • 2.1.0

'filter'.freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(collection, filter, options, flags) ⇒ Upconverter

Instantiate the upconverter.

Examples:

Instantiate the upconverter.

Upconverter.new('users', { name: 'test' }, { skip: 10 })

Parameters:

  • collection (String)

    The name of the collection.

  • filter (BSON::Document, Hash)

    The filter or command.

  • options (BSON::Document, Hash)

    The options.

  • flags (Array<Symbol>)

    The flags.

Since:

  • 2.1.0



229
230
231
232
233
234
# File 'lib/mongo/protocol/query.rb', line 229

def initialize(collection, filter, options, flags)
  @collection = collection
  @filter = filter
  @options = options
  @flags = flags
end

Instance Attribute Details

#collectionString (readonly)

Returns collection The name of the collection.

Returns:

  • (String)

    collection The name of the collection.

Since:

  • 2.1.0



207
208
209
# File 'lib/mongo/protocol/query.rb', line 207

def collection
  @collection
end

#filterBSON::Document, Hash (readonly)

Returns filter The query filter or command.

Returns:

  • (BSON::Document, Hash)

    filter The query filter or command.

Since:

  • 2.1.0



210
211
212
# File 'lib/mongo/protocol/query.rb', line 210

def filter
  @filter
end

#flagsArray<Symbol> (readonly)

Returns flags The flags.

Returns:

  • (Array<Symbol>)

    flags The flags.

Since:

  • 2.1.0



216
217
218
# File 'lib/mongo/protocol/query.rb', line 216

def flags
  @flags
end

#optionsBSON::Document, Hash (readonly)

Returns options The options.

Returns:

  • (BSON::Document, Hash)

    options The options.

Since:

  • 2.1.0



213
214
215
# File 'lib/mongo/protocol/query.rb', line 213

def options
  @options
end

Instance Method Details

#commandBSON::Document

Get the upconverted command.

Examples:

Get the command.

upconverter.command

Returns:

  • (BSON::Document)

    The upconverted command.

Since:

  • 2.1.0



244
245
246
# File 'lib/mongo/protocol/query.rb', line 244

def command
  command? ? op_command : find_command
end

#command_nameString, Symbol

Get the name of the command. If the collection is $cmd then it's the first key in the filter, otherwise it's a find.

Examples:

Get the command name.

upconverter.command_name

Returns:

  • (String, Symbol)

    The command name.

Since:

  • 2.1.0



257
258
259
# File 'lib/mongo/protocol/query.rb', line 257

def command_name
  (filter[:$query] || !command?) ? FIND : filter.keys.first
end