类:Mongo::Database
- 继承:
-
对象
- 对象
- Mongo::Database
- 扩展方式:
- 可转发
- 包括:
- 可重试
- 定义于:
- lib/mongo/database.rb,
lib/mongo/database/view.rb
Overview
表示数据库服务器上的数据库以及可以在此级别对其执行的操作。
在命名空间下定义
类: 查看
常量摘要折叠
- ADMIN =
管理员数据库名称。
'管理员'- 命令 =
数据库命令对其进行操作的“集合”。
'$cmd'- DEFAULT_OPTIONS =
默认数据库选项。
选项::已编辑.new(database: ADMIN).冻结
- NAME =
已弃用。
数据库名称字段常量。
'名称'- DATABASES =
数据库常量。
'databases'- 命名空间 =
包含所有集合集合。
'system.namespaces'
实例属性摘要折叠
-
#client ⇒ Client
只读
客户端数据库客户端。
-
#名称⇒ string
只读
名称数据库的名称。
-
# options ⇒ 哈希
只读
选项 选项。
类方法摘要折叠
-
。创建(客户端) ⇒ 数据库
private
为提供的客户端创建一个数据库,以在不希望客户端的原始数据库实例相同时使用。
实例方法摘要折叠
-
# == (other) ⇒ true, false
检查数据库对象与另一个对象的相等性。
-
# [] (collection_name, options = {}) ⇒ Mongo::Collection (又作: # 集合)
按提供的名称获取此数据库中的集合。
-
#aggregate (pipeline, options = {}) ⇒ Collection::View::Aggregation
对数据库执行聚合。
-
#cluster ⇒ Mongo::Server
从集群中获取主节点 (primary node in the replica set)服务器。
-
# collection_names (options = {}) ⇒ Array<String>
获取数据库中所有非系统集合的名称。
-
# collections (options = {}) ⇒ Array<Mongo::Collection>
获取属于此数据库的所有非系统集合。
-
# command (operation, opts = {}) ⇒ Mongo::Operation::Result
对数据库执行命令。
-
# drop (options = {}) ⇒ 结果
删除数据库及其所有相关信息。
-
# fs (options = {}) ⇒ Grid::FSBucket
获取此数据库的网格“文件系统”。
-
#initialize (客户端, name, options = {}) ⇒ 数据库
构造函数
实例化一个新的数据库对象。
-
#检查⇒ string
为数据库进行美观打印的string检查。
-
# list_collections (options = {}) ⇒ Array<Hash>
获取有关数据库中所有非系统集合的信息。
-
# operation_timeups (opts) ⇒ 哈希
private
在操作级别上设立的timeout_ms 值(如果有),和/或在集合/数据库/客户端级别上设立的timeout_ms(如果有)。
-
# read_command (operation, opts = {}) ⇒ 哈希
private
对数据库执行读取命令,必要时重试读取。
-
#timeout_ms ⇒ Integer | nil
private
该数据库或相应客户端的操作超时。
-
# users ⇒ View::User
获取此数据库的用户视图。
-
# watch (pipeline = [], options = {}) ⇒ ChangeStream
允许用户请求为客户端数据库中发生的所有更改发送通知。
Retryable 中包含的方法
#read_worker、#select_server、#with_overload_retry、#write_worker
构造函数详情
#initialize (客户端, name, options = {}) ⇒数据库
实例化一个新的数据库对象。
365 366 367 368 369 370 371 372 373 374 |
# File 'lib/ Mongo/ 数据库.rb', line 365 def 初始化(客户端, 名称, = {}) 提高 错误::InvalidDatabaseName.new 除非 名称 if Lint.已启用? && !(名称.is_a?(字符串) || 名称.is_a?(符号)) 提高 "数据库名称必须是string或符号: #{ name } " end @client = 客户端 @name = 名称.to_s.冻结 @options = .冻结 end |
实例属性详细信息
# name ⇒ string (readonly)
返回 name数据库的名称。
63 64 65 |
# File 'lib/ Mongo/ 数据库.rb', line 63 def 名称 @name end |
# options ⇒哈希(只读)
返回 options 选项。
66 67 68 |
# File 'lib/ Mongo/ 数据库.rb', line 66 def @options end |
类方法详细信息
实例方法详细信息
# == (other) ⇒ true , false
检查数据库对象与另一个对象的相等性。 将仅检查名称是否相同。
93 94 95 96 97 |
# File 'lib/ Mongo/ 数据库.rb', line 93 def ==(其他) return false 除非 其他.is_a?(Database) 名称 == 其他.名称 end |
# [] (collection_name, options = {}) ⇒ Mongo::Collection也称为:集合
按提供的名称获取此数据库中的集合。
110 111 112 113 114 115 116 117 |
# File 'lib/ Mongo/ 数据库.rb', line 110 def [ ](collection_name, = {}) if [:server_api] 提高 ArgumentError, '不能为集合对象指定 :server_api 选项。它只能在客户端级别指定' end Collection.new(self, collection_name, ) end |
#aggregate (pipeline, options = {}) ⇒ Collection::View::Aggregation
对数据库执行聚合。
454 455 456 |
# File 'lib/ Mongo/ 数据库.rb', line 454 def 聚合(管道, = {}) 查看.new(self, ).聚合(管道, ) end |
#cluster ⇒ Mongo::Server
返回 从集群中获取主节点 (primary node in the replica set)服务器。
79 80 |
# File 'lib/ Mongo/ 数据库.rb', line 79 def_delegators :cluster, :next_primary |
# collection_names (options = {}) ⇒ Array<String>
返回的集合名称集取决于完成请求的 MongoDB 服务器的版本。
获取数据库中所有非系统集合的名称。
See https://mongodb.com/zh-cn/docs/manual/reference/command/listCollections/
for more information and usage.
144 145 146 |
# File 'lib/ Mongo/ 数据库.rb', line 144 def collection_names( = {}) 查看.new(self, ).collection_names() end |
# collections (options = {}) ⇒ Array< Mongo::Collection >
返回的设立集取决于完成请求的MongoDB服务器的版本。
获取属于此数据库的所有非系统集合。
See https://mongodb.com/zh-cn/docs/manual/reference/command/listCollections/
for more information and usage.
205 206 207 |
# File 'lib/ Mongo/ 数据库.rb', line 205 def collections( = {}) collection_names().map { |名称| 集合(名称) } end |
# command (operation, opts = {}) ⇒ Mongo::Operation::Result
对数据库执行命令。
231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 |
# File 'lib/ Mongo/ 数据库.rb', line 231 def 命令(操作, opts = {}) opts = opts.dup execution_opts = opts.删除(:execution_options) || {} txn_read_pref = (opts[:session].txn_read_preference if opts[:session] && opts[:session].in_transaction?) txn_read_pref ||= opts[:read] || ServerSelector::主节点 Lint.validate_underscore_read_preference(txn_read_pref) 选择器 = ServerSelector.获取(txn_read_pref) 客户端.with_session(opts) do |会话| 上下文 = 操作::上下文.new( 客户端: 客户端, 会话: 会话, operation_timeups: operation_timeups(opts) ) op = 操作::命令.new( 选择器: 操作, db_name: 名称, 读取: 选择器, 会话: 会话 ) retry_enabled = 客户端.[:retry_reads] != false && 客户端.[:retry_writes] != false with_overload_retry(上下文: 上下文, retry_enabled: retry_enabled) do server = 选择器.select_server(集群, nil, 会话) op.执行(server, 上下文: 上下文, 选项: execution_opts) end end end |
# drop (options = {}) ⇒结果
删除数据库及其所有相关信息。
325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 |
# File 'lib/ Mongo/ 数据库.rb', line 325 def 删除( = {}) 操作 = { dropDatabase: 1 } 客户端.with_session() do |会话| write_concern = if [:write_concern] writeConcern.获取([:write_concern]) else self.write_concern end 操作::删除数据库.new({ 选择器: 操作, db_name: 名称, write_concern: write_concern, 会话: 会话 }).执行( next_primary(nil, 会话), 上下文: 操作::上下文.new( 客户端: 客户端, 会话: 会话, operation_timeups: operation_timeups() ) ) end end |
#fs(options = {}) ⇒ Grid::FSBucket
获取此数据库的网格“文件系统”。
408 409 410 |
# File 'lib/ Mongo/ 数据库.rb', line 408 def fs( = {}) 网格::FSBucket.new(self, ) end |
#检查⇒ string
为数据库进行美观打印的string检查。
384 385 386 |
# File 'lib/ Mongo/ 数据库.rb', line 384 def 检查 " #<Mongo::Database: 0 x #{ object_id } name= #{ name } > " end |
# list_collections (options = {}) ⇒ Array<Hash>
返回的集合集以及每个集合的信息哈希模式取决于完成请求的 MongoDB 服务器版本。
获取有关数据库中所有非系统集合的信息。
See https://mongodb.com/zh-cn/docs/manual/reference/command/listCollections/
for more information and usage.
177 178 179 |
# File 'lib/ Mongo/ 数据库.rb', line 177 def list_collections( = {}) 查看.new(self, ).list_collections() end |
# operation_timeups (opts) ⇒ 哈希
此方法是私有 API 的一部分。 您应尽可能避免使用此方法,因为它将来可能会被删除或更改。
返回在操作级别(如果有)上设立的timeout_ms 值,和/或在集合/数据库/客户端级别上设立的timeout_ms(如果有)。
563 564 565 566 567 568 569 570 571 572 |
# File 'lib/ Mongo/ 数据库.rb', line 563 def operation_timeups(opts) # TODO: 我们应该重新评估是否需要分别设置两次超时。 {}.点击 do |结果| if opts[:timeout_ms].nil? 结果[:inherited_timeout_ms] = timeout_ms else 结果[:operation_timeout_ms] = opts.删除(:timeout_ms) end end end |
# read_command (operation, opts = {}) ⇒哈希
此方法是私有 API 的一部分。 您应尽可能避免使用此方法,因为它将来可能会被删除或更改。
对数据库执行读取命令,必要时重试读取。
280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 |
# File 'lib/ Mongo/ 数据库.rb', line 280 def read_command(操作, opts = {}) txn_read_pref = (opts[:session].txn_read_preference if opts[:session] && opts[:session].in_transaction?) txn_read_pref ||= opts[:read] || ServerSelector::主节点 Lint.validate_underscore_read_preference(txn_read_pref) 偏好 = ServerSelector.获取(txn_read_pref) 客户端.with_session(opts) do |会话| 上下文 = 操作::上下文.new( 客户端: 客户端, 会话: 会话, operation_timeups: operation_timeups(opts) ) 操作 = 操作::命令.new( 选择器: 操作.dup, db_name: 名称, 读取: 偏好, 会话: 会话, comment: opts[:comment] ) op_name = opts[:op_name] || 'command' 追踪器.trace_operation(操作, 上下文, op_name: op_name) do read_with_retry(会话, 偏好, 上下文) do |server| 操作.执行(server, 上下文: 上下文) end end end end |
#timeout_ms ⇒ Integer | nil
此方法是私有 API 的一部分。 您应尽可能避免使用此方法,因为它将来可能会被删除或更改。
返回此数据库或相应客户端的操作超时时间。
555 556 557 |
# File 'lib/ Mongo/ 数据库.rb', line 555 def timeout_ms [:timeout_ms] || 客户端.timeout_ms end |
# users ⇒ View::User
获取此数据库的用户视图。
420 421 422 |
# File 'lib/ Mongo/ 数据库.rb', line 420 def 用户 auth::user::查看.new(self) end |
# watch (管道 = [], options = {}) ⇒ ChangeStream
变更流只允许“大多数”读关注(read concern)。
出于支持可恢复性的目的,此辅助方法优于使用 $changeStream 阶段运行原始聚合。
允许用户请求为客户端数据库中发生的所有更改发送通知。
521 522 523 524 525 526 527 528 529 530 531 |
# File 'lib/ Mongo/ 数据库.rb', line 521 def 观看(管道 = [], = {}) = .dup [:cursor_type] = :tailable_await if [:max_await_time_ms] mongo::Collection::查看::变更流.new( mongo::Collection::查看.new(集合(" #{ COMMAND } .aggregate "), {}, ), 管道, mongo::Collection::查看::变更流::数据库, ) end |