Class: Mongo::Database::View
Relationships & Source Files | |
Super Chains via Extension / Inclusion / Inheritance | |
Class Chain:
self,
Forwardable
|
|
Instance Chain:
self,
::Mongo::Retryable ,
Enumerable
|
|
Inherits: | Object |
Defined in: | lib/mongo/database/view.rb |
Overview
A class representing a view of a database.
Class Method Summary
-
.new(database) ⇒ View
constructor
Create the new database view.
Instance Attribute Summary
- #batch_size ⇒ Integer readonly
- #collection ⇒ Collection readonly
- #database readonly Internal use only Internal use only
- #limit ⇒ Integer readonly
Instance Method Summary
-
#aggregate(pipeline, options = {}) ⇒ Collection::View::Aggregation
Internal use only
Internal use only
Execute an aggregation on the database view.
-
#collection_names(options = {}) ⇒ Array<String>
Get all the names of the non-system collections in the database.
-
#list_collections(options = {}) ⇒ Array<Hash>
Get info on all the collections in the database.
- #collections_info(session, server_selector, options = {}, &block) private
- #collections_info_spec(session, options = {}) private
- #initial_query_op(session, options = {}) private
-
#send_initial_query(server, session, options = {}) ⇒ Operation::Result
private
Sends command that obtains information about the database.
::Mongo::Retryable
- Included
#read_worker | Returns the read worker for handling retryable reads. |
#select_server | This is a separate method to make it possible for the test suite to assert that server selection is performed during retry attempts. |
#write_worker | Returns the write worker for handling retryable writes. |
Constructor Details
.new(database) ⇒ View
Create the new database view.
Instance Attribute Details
#batch_size ⇒ Integer
(readonly)
# File 'lib/mongo/database/view.rb', line 36
attr_reader :batch_size
#collection ⇒ Collection (readonly)
# File 'lib/mongo/database/view.rb', line 42
attr_reader :collection
#database (readonly)
# File 'lib/mongo/database/view.rb', line 135
attr_reader :database
#limit ⇒ Integer
(readonly)
# File 'lib/mongo/database/view.rb', line 39
attr_reader :limit
Instance Method Details
#aggregate(pipeline, options = {}) ⇒ Collection::View::Aggregation
Execute an aggregation on the database view.
# File 'lib/mongo/database/view.rb', line 151
def aggregate(pipeline, = {}) Collection::View::Aggregation.new(self, pipeline, ) end
#collection_names(options = {}) ⇒ Array
<String
>
The set of returned collection names depends on the version of MongoDB server that fulfills the request.
Get all the names of the non-system collections in the database.
# File 'lib/mongo/database/view.rb', line 67
def collection_names( = {}) @batch_size = [:batch_size] session = client.send(:get_session, ) cursor = read_with_retry_cursor(session, ServerSelector.primary, self) do |server| send_initial_query(server, session, .merge(name_only: true)) end cursor.map do |info| if cursor.initial_result.connection_description.features.list_collections_enabled? info['name'] else (info['name'] && info['name'].sub("#{@database.name}.", '')) end end.reject do |name| name.start_with?('system.') || name.include?('$') end end
#collections_info(session, server_selector, options = {}, &block) (private)
# File 'lib/mongo/database/view.rb', line 157
def collections_info(session, server_selector, = {}, &block) description = nil cursor = read_with_retry_cursor(session, server_selector, self) do |server| # TODO take description from the connection used to send the query # once https://jira.mongodb.org/browse/RUBY-1601 is fixed. description = server.description send_initial_query(server, session, ) end # On 3.0+ servers, we get just the collection names. # On 2.6 server, we get collection names prefixed with the database # name. We need to filter system collections out here because # in the caller we don't know which server version executed the # command and thus what the proper filtering logic should be # (it is valid for collection names to have dots, thus filtering out # collections named system.* here for 2.6 servers would actually # filter out collections in the system database). if description.server_version_gte?('3.0') cursor.reject do |doc| doc['name'].start_with?('system.') || doc['name'].include?('$') end else cursor.reject do |doc| doc['name'].start_with?("#{database.name}.system") || doc['name'].include?('$') end end end
#collections_info_spec(session, options = {}) (private)
# File 'lib/mongo/database/view.rb', line 184
def collections_info_spec(session, = {}) { selector: { listCollections: 1, cursor: batch_size ? { batchSize: batch_size } : {} }, db_name: @database.name, session: session }.tap do |spec| spec[:selector][:nameOnly] = true if [:name_only] spec[:selector][:filter] = [:filter] if [:filter] spec[:selector][: ] = true if [: ] spec[:comment] = [:comment] if [:comment] end end
#initial_query_op(session, options = {}) (private)
# File 'lib/mongo/database/view.rb', line 198
def initial_query_op(session, = {}) Operation::CollectionsInfo.new(collections_info_spec(session, )) end
#list_collections(options = {}) ⇒ Array
<Hash
>
The set of collections returned, and the schema of the information hash per collection, depends on the MongoDB server version that fulfills the request.
Get info on all the collections in the database.
# File 'lib/mongo/database/view.rb', line 114
def list_collections( = {}) session = client.send(:get_session, ) collections_info(session, ServerSelector.primary, ) end
#send_initial_query(server, session, options = {}) ⇒ Operation::Result (private)
Sends command that obtains information about the database.
This command returns a cursor, so there could be additional commands, therefore this method is called send initial command.
# File 'lib/mongo/database/view.rb', line 227
def send_initial_query(server, session, = {}) opts = .dup execution_opts = {} if opts.key?(:deserialize_as_bson) execution_opts[:deserialize_as_bson] = opts.delete(:deserialize_as_bson) end initial_query_op(session, opts).execute( server, context: Operation::Context.new(client: client, session: session), options: execution_opts ) end