类:Mongo::Database
- 继承:
-
对象
- 对象
- Mongo::Database
- 扩展方式:
- 可转发
- 包括:
- 可重试
- 定义于:
- lib/mongo/database.rb,
lib/mongo/database/view.rb,
lib/mongo/database/cursor_command_view.rb
Overview
表示数据库服务器上的数据库以及可以在此级别对其执行的操作。
在命名空间下定义
常量摘要折叠
- ADMIN =
管理员数据库名称。
'管理员'- 命令 =
数据库命令对其进行操作的“集合”。
'$cmd'- DEFAULT_OPTIONS =
默认数据库选项。
选项::已编辑.new(database: ADMIN).冻结
- NAME =
已弃用。
数据库名称字段常量。
'名称'- DATABASES =
数据库常量。
'databases'- 命名空间 =
包含所有集合集合。
'system.namespaces'
实例属性摘要折叠
-
#client ⇒ Client
只读
客户端数据库客户端。
-
#名称⇒ string
只读
名称数据库的名称。
-
# options ⇒ 哈希
只读
选项 选项。
实例方法摘要折叠
-
# == (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
对数据库执行命令。
-
#游标_command(command, options = {}) ⇒ Mongo::Cursor
运行返回游标的命令,并将响应解析为游标。
-
# 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 = {}) ⇒数据库
实例化一个新的数据库对象。
479 480 481 482 483 484 485 486 487 488 |
# File 'lib/ Mongo/ 数据库.rb', line 479 def 初始化(客户端, 名称, = {}) 提高 错误::InvalidDatabaseName.new 除非 名称 if Lint.已启用? && !(名称.is_a?(字符串) || 名称.is_a?(符号)) 提高 "数据库名称必须是string或符号: #{ name } " end @client = 客户端 @name = 名称.to_s.冻结 @options = .冻结 end |
实例属性详细信息
# name ⇒ string (readonly)
返回 name数据库的名称。
64 65 66 |
# File 'lib/ Mongo/ 数据库.rb', line 64 def 名称 @name end |
# options ⇒哈希(只读)
返回 options 选项。
67 68 69 |
# File 'lib/ Mongo/ 数据库.rb', line 67 def @options end |
实例方法详细信息
# == (other) ⇒ true , false
检查数据库对象与另一个对象的相等性。 将仅检查名称是否相同。
94 95 96 97 98 |
# File 'lib/ Mongo/ 数据库.rb', line 94 def ==(其他) return false 除非 其他.is_a?(Database) 名称 == 其他.名称 end |
# [] (collection_name, options = {}) ⇒ Mongo::Collection也称为:集合
按提供的名称获取此数据库中的集合。
111 112 113 114 115 116 117 118 |
# File 'lib/ Mongo/ 数据库.rb', line 111 def [ ](collection_name, = {}) if [:server_api] 提高 ArgumentError, '不能为集合对象指定 :server_api 选项。它只能在客户端级别指定' end Collection.new(self, collection_name, ) end |
#aggregate (pipeline, options = {}) ⇒ Collection::View::Aggregation
对数据库执行聚合。
568 569 570 |
# File 'lib/ Mongo/ 数据库.rb', line 568 def 聚合(管道, = {}) 查看.new(self, ).聚合(管道, ) end |
#cluster ⇒ Mongo::Server
返回 从集群中获取主节点 (primary node in the replica set)服务器。
80 81 |
# File 'lib/ Mongo/ 数据库.rb', line 80 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.
145 146 147 |
# File 'lib/ Mongo/ 数据库.rb', line 145 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.
206 207 208 |
# File 'lib/ Mongo/ 数据库.rb', line 206 def collections( = {}) collection_names().map { |名称| 集合(名称) } end |
# command (operation, opts = {}) ⇒ Mongo::Operation::Result
对数据库执行命令。
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 261 |
# File 'lib/ Mongo/ 数据库.rb', line 232 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 |
#cursor_command(command, options = {}) ⇒ 游标
运行返回游标的命令,并将响应解析为游标。
命令以未修改的形式发送到服务器;驱动程序不得检查或更改命令。如果响应不包含游标字段,则会报错。命令从未重试。
注:如果命令文档上已设置 maxTimeMS 字段,则保持不变。下面的 max_time_ms 选项仅适用于 getMore 命令。不支持同时设置 timeout_ms 和 max_time_ms,并且具有未定义行为。
301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 |
# File 'lib/ Mongo/ 数据库.rb', line 301 def cursor_command(命令, = {}) = .dup execution_opts = .删除(:execution_options) || {} = () txn_read_pref = ([:session].txn_read_preference if [:session] && [:session].in_transaction?) txn_read_pref ||= [:read] || ServerSelector::主节点 Lint.validate_underscore_read_preference(txn_read_pref) 选择器 = ServerSelector.获取(txn_read_pref) # 会话有意不包装在 #with_session 中:隐式 # 会话必须比此方法存在时间长,并由游标结束 # 已用尽或已关闭。直到游标取得所有权,会话和 # 每次退出路径都会在此处清理任何负载均衡连接。 会话 = 客户端.get_session() 上下文 = 操作::上下文.new( 客户端: 客户端, 会话: 会话, operation_timeups: operation_timeups() ) op = 操作::CursorCommand.new( 选择器: 命令, db_name: 名称, 读取: 选择器, 会话: 会话 ) # 根据客户端背压规范,对通用命令进行重试 # 重载错误要求可重试读取和写入都是 # 已启用,与 数据库命令 相同。 retry_enabled = 客户端.[:retry_reads] != false && 客户端.[:retry_writes] != false server = nil 连接 = nil cursor = nil 开始 结果 = with_overload_retry(上下文: 上下文, retry_enabled: retry_enabled) do server = 选择器.select_server(集群, nil, 会话) if server.load_balancer? # 当游标耗尽时,将签入连接。 连接 = check_out_cursor_command_connection(server, 上下文) 开始 op.execute_with_connection(连接, 上下文: 上下文, 选项: execution_opts) 救援 StandardError # 在错误传播之前释放连接,以便 # 重试尝试检查一个新的。 连接.connection_pool.check_in(连接) 除非 连接.已固定? 连接 = nil 提高 end else op.执行(server, 上下文: 上下文, 选项: execution_opts) end end 除非 结果.cursor? 提高 错误::InvalidCursorOperation, '命令响应中没有包含游标。' \ '对不返回游标的命令使用数据库命令。' end view = CursorCommandView.new(self,) cursor = 游标.new(view,result, 服务器, session: session, context: context) ensure # 如果创建了游标,则它拥有会话和连接; # 否则(响应中的错误或无游标),在此释放它们。 unless 游标 connection.connection_pool.check_in(connection)if connection && !connection.pinned?session.end_sessionif session && session.implicit?end end 游标 end |
# drop (options = {}) ⇒结果
删除数据库及其所有相关信息。
439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 |
# File 'lib/ Mongo/ 数据库.rb', line 439 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
获取此数据库的网格“文件系统”。
522 523 524 |
# File 'lib/ Mongo/ 数据库.rb', line 522 def fs( = {}) 网格::FSBucket.new(self, ) end |
#检查⇒ string
为数据库进行美观打印的string检查。
498 499 500 |
# File 'lib/ Mongo/ 数据库.rb', line 498 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.
178 179 180 |
# File 'lib/ Mongo/ 数据库.rb', line 178 def list_collections( = {}) 查看.new(self, ).list_collections() end |
# operation_timeups (opts) ⇒ 哈希
此方法是私有 API 的一部分。 您应尽可能避免使用此方法,因为它将来可能会被删除或更改。
返回在操作级别(如果有)上设立的timeout_ms 值,和/或在集合/数据库/客户端级别上设立的timeout_ms(如果有)。
659 660 661 662 663 664 665 666 667 668 |
# File 'lib/ Mongo/ 数据库.rb', line 659 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 的一部分。 您应尽可能避免使用此方法,因为它将来可能会被删除或更改。
对数据库执行读取命令,必要时重试读取。
394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 |
# File 'lib/ Mongo/ 数据库.rb', line 394 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 的一部分。 您应尽可能避免使用此方法,因为它将来可能会被删除或更改。
返回此数据库或相应客户端的操作超时时间。
651 652 653 |
# File 'lib/ Mongo/ 数据库.rb', line 651 def timeout_ms [:timeout_ms] || 客户端.timeout_ms end |
# users ⇒ View::User
获取此数据库的用户视图。
534 535 536 |
# File 'lib/ Mongo/ 数据库.rb', line 534 def 用户 auth::user::查看.new(self) end |
# watch (管道 = [], options = {}) ⇒ ChangeStream
变更流只允许“大多数”读关注(read concern)。
出于支持可恢复性的目的,此辅助方法优于使用 $changeStream 阶段运行原始聚合。
允许用户请求为客户端数据库中发生的所有更改发送通知。
635 636 637 638 639 640 641 642 643 644 645 |
# File 'lib/ Mongo/ 数据库.rb', line 635 def 观看(管道 = [], = {}) = .dup [:cursor_type] = :tailable_await if [:max_await_time_ms] mongo::Collection::查看::变更流.new( mongo::Collection::查看.new(集合(" #{ COMMAND } .aggregate "), {}, ), 管道, mongo::Collection::查看::变更流::数据库, ) end |