class Mongo::Protocol::Query
MongoDB Wire protocol Query
message.
This is a client request message that is sent to the server in order to retrieve documents matching provided query.
Users may also provide additional options such as a projection, to select a subset of the fields, a number to skip or a limit on the number of returned documents.
There are a variety of flags that can be used to adjust cursor parameters or the desired consistency and integrity the results.
@api semipublic
Constants
Attributes
Public Class Methods
Creates a new Query
message
@example Find all users named Tyler.
Query.new('xgen', 'users', {:name => 'Tyler'})
@example Find all users named Tyler skipping 5 and returning 10.
Query.new('xgen', 'users', {:name => 'Tyler'}, :skip => 5, :limit => 10)
@example Find all users with secondaryOk bit set
Query.new('xgen', 'users', {:name => 'Tyler'}, :flags => [:secondary_ok])
@example Find all user ids.
Query.new('xgen', 'users', {}, :fields => {:id => 1})
@param [ String, Symbol
] database The database to query. @param [ String, Symbol
] collection The collection to query. @param [ Hash ] selector The query selector. @param [ Hash ] options The additional query options.
@option options [ Array<Symbol> ] :flags The flag bits.
Currently supported values are :await_data, :exhaust, :no_cursor_timeout, :oplog_replay, :partial, :secondary_ok, :tailable_cursor.
@option options [ Integer ] :limit The number of documents to return. @option options [ Hash ] :project The projection. @option options [ Integer ] :skip The number of documents to skip.
# File lib/mongo/protocol/query.rb, line 64 def initialize(database, collection, selector, options = {}) @database = database @namespace = "#{database}.#{collection}" if selector.nil? raise ArgumentError, 'Selector cannot be nil' end @selector = selector @options = options @project = options[:project] @limit = determine_limit @skip = options[:skip] || 0 @flags = options[:flags] || [] @upconverter = Upconverter.new( collection, BSON::Document.new(selector), BSON::Document.new(options), flags, ) super end
Public Instance Methods
Compress the message, if the command being sent permits compression. Otherwise returns self.
@param [ String, Symbol
] compressor The compressor to use. @param [ Integer ] zlib_compression_level The zlib compression level to use.
@return [ Message
] A Protocol::Compressed
message or self,
depending on whether this message can be compressed.
@since 2.5.0 @api private
# File lib/mongo/protocol/query.rb, line 125 def maybe_compress(compressor, zlib_compression_level = nil) compress_if_possible(selector.keys.first, compressor, zlib_compression_level) end
Return the event payload for monitoring.
@example Return the event payload.
message.payload
@return [ BSON::Document ] The event payload.
@since 2.1.0
# File lib/mongo/protocol/query.rb, line 93 def payload BSON::Document.new( command_name: upconverter.command_name, database_name: @database, command: upconverter.command, request_id: request_id ) end
Query
messages require replies from the database.
@example Does the message require a reply?
message.replyable?
@return [ true ] Always true for queries.
@since 2.0.0
# File lib/mongo/protocol/query.rb, line 110 def replyable? true end
Serializes message into bytes that can be sent on the wire.
@param [ BSON::ByteBuffer ] buffer where the message should be inserted. @param [ Integer ] max_bson_size The maximum bson object size.
@return [ BSON::ByteBuffer ] buffer containing the serialized message.
Mongo::Protocol::Message#serialize
# File lib/mongo/protocol/query.rb, line 135 def serialize(buffer = BSON::ByteBuffer.new, max_bson_size = nil, bson_overhead = nil) validate_document_size!(max_bson_size) super end
Private Instance Methods
# File lib/mongo/protocol/query.rb, line 177 def determine_limit [ @options[:limit] || @options[:batch_size], @options[:batch_size] || @options[:limit] ].min || 0 end
Validate that the documents in this message are all smaller than the maxBsonObjectSize. If not, raise an exception.
# File lib/mongo/protocol/query.rb, line 149 def validate_document_size!(max_bson_size) max_bson_size ||= Mongo::Server::ConnectionBase::DEFAULT_MAX_BSON_OBJECT_SIZE documents = if @selector.key?(:documents) @selector[:documents] elsif @selector.key?(:deletes) @selector[:deletes] elsif @selector.key?(:updates) @selector[:updates] else [] end contains_too_large_document = documents.any? do |doc| doc.to_bson.length > max_bson_size end if contains_too_large_document raise Error::MaxBSONSize.new('The document exceeds maximum allowed BSON object size after serialization') end end