类:Mongo::Database
- 继承:
-
对象
- 对象
- Mongo::Database
- 扩展方式:
- 可转发
- 包括:
- 可重试
- 定义于:
- lib/mongo/database.rb,
lib/mongo/database/view.rb 更多...
Overview
表示数据库服务器上的数据库以及可以在此级别对其执行的操作。
在命名空间下定义
类: 查看
常量摘要折叠
- ADMIN =
管理员数据库名称。
' admin '.冻结
- 命令 =
数据库命令操作的“集合”。
'$cmd'.冻结
- DEFAULT_OPTIONS =
默认数据库选项。
选项::已编辑.new(:database => ADMIN).冻结
- NAME =
已弃用。
数据库名称字段常量。
' 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
从 MongoDB Server 3.6版本开始,聚合框架支持“$changeStream”管道阶段。
Retryable 中包含的方法
#read_worker 、 #select_server 、 #write_worker
构造函数详情
#initialize (客户端, name, options = {}) ⇒数据库
实例化一个新的数据库对象。
362 363 364 365 366 367 368 369 370 |
# File 'lib/ Mongo/ 数据库.rb', line 362 def 初始化(客户端, 名称, = {}) 提高 错误::InvalidDatabaseName.new 除非 名称 if Lint.已启用? && !(名称.is_a?(字符串) || 名称.is_a?(符号)) 提高 "数据库名称必须是string或符号: #{ name } " end @client = 客户端 @name = 名称.to_s.冻结 @options = .冻结 end |
实例属性详细信息
# name ⇒ string (readonly)
返回 name数据库的名称。
65 66 67 |
# File 'lib/ Mongo/ 数据库.rb', line 65 def 名称 @name end |
# options ⇒哈希(只读)
返回 options 选项。
68 69 70 |
# File 'lib/ Mongo/ 数据库.rb', line 68 def @options end |
类方法详细信息
实例方法详细信息
# == (other) ⇒ true , false
检查数据库对象与另一个对象的相等性。 将仅检查名称是否相同。
94 95 96 97 |
# File 'lib/ Mongo/ 数据库.rb', line 94 def ==(其他) return false 除非 其他.is_a?(Database) 名称 == 其他.名称 end |
# [] (collection_name, options = {}) ⇒ Mongo::Collection也称为:集合
按提供的名称获取此数据库中的集合。
110 111 112 113 114 115 |
# 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
对数据库执行聚合。
450 451 452 |
# File 'lib/ Mongo/ 数据库.rb', line 450 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.
142 143 144 |
# File 'lib/ Mongo/ 数据库.rb', line 142 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.
203 204 205 |
# File 'lib/ Mongo/ 数据库.rb', line 203 def collections( = {}) collection_names().map { |名称| 集合(名称) } end |
# command (operation, opts = {}) ⇒ Mongo::Operation::Result
对数据库执行命令。
229 230 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 |
# File 'lib/ Mongo/ 数据库.rb', line 229 def 命令(操作, opts = {}) opts = opts.dup execution_opts = opts.删除(:execution_options) || {} txn_read_pref = if opts[:session] && opts[:session].in_transaction? opts[:session].txn_read_preference else nil end txn_read_pref ||= opts[:read] || ServerSelector::主节点 Lint.validate_underscore_read_preference(txn_read_pref) 选择器 = ServerSelector.获取(txn_read_pref) 客户端.with_session(opts) do |会话| server = 选择器.select_server(集群, nil, 会话) op = 操作::命令.new( :selector => 操作, :db_name => 名称, :read => 选择器, :session => 会话 ) op.执行(server, 上下文: 操作::上下文.new( 客户端: 客户端, 会话: 会话, operation_timeups: operation_timeups(opts) ), 选项: execution_opts) end end |
# drop (options = {}) ⇒结果
删除数据库及其所有相关信息。
322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 |
# File 'lib/ Mongo/ 数据库.rb', line 322 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
获取此数据库的网格“文件系统”。
404 405 406 |
# File 'lib/ Mongo/ 数据库.rb', line 404 def fs( = {}) 网格::FSBucket.new(self, ) end |
#检查⇒ string
为数据库进行美观打印的string检查。
380 381 382 |
# File 'lib/ Mongo/ 数据库.rb', line 380 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.
175 176 177 |
# File 'lib/ Mongo/ 数据库.rb', line 175 def list_collections( = {}) 查看.new(self, ).list_collections() end |
# operation_timeups (opts) ⇒ 哈希
此方法是私有 API 的一部分。 您应尽可能避免使用此方法,因为它将来可能会被删除或更改。
返回在操作级别(如果有)上设立的timeout_ms 值,和/或在集合/数据库/客户端级别上设立的timeout_ms(如果有)。
560 561 562 563 564 565 566 567 568 569 |
# File 'lib/ Mongo/ 数据库.rb', line 560 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 的一部分。 您应尽可能避免使用此方法,因为它将来可能会被删除或更改。
对数据库执行读取命令,必要时重试读取。
277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 |
# File 'lib/ Mongo/ 数据库.rb', line 277 def read_command(操作, opts = {}) txn_read_pref = if opts[:session] && opts[:session].in_transaction? opts[:session].txn_read_preference else nil end 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) ) read_with_retry(会话, 偏好, 上下文) do |server| 操作::命令.new( 选择器: 操作.dup, db_name: 名称, 读取: 偏好, 会话: 会话, comment: opts[:comment], ).执行(server, 上下文: 上下文) end end end |
#timeout_ms ⇒ Integer | nil
此方法是私有 API 的一部分。 您应尽可能避免使用此方法,因为它将来可能会被删除或更改。
返回此数据库或相应客户端的操作超时时间。
552 553 554 |
# File 'lib/ Mongo/ 数据库.rb', line 552 def timeout_ms [:timeout_ms] || 客户端.timeout_ms end |
# users ⇒ View::User
获取此数据库的用户视图。
416 417 418 |
# File 'lib/ Mongo/ 数据库.rb', line 416 def 用户 auth::user::查看.new(self) end |
# watch (管道 = [], options = {}) ⇒ ChangeStream
变更流只允许“多数”读关注(read concern)。
出于支持可恢复性的目的,此辅助方法优于使用 $changeStream 阶段运行原始聚合。
从MongoDB 服务器 3.6版本开始,聚合框架支持“$changeStream”管道阶段。 从版本4.0开始,此阶段允许用户请求为客户端数据库中发生的所有更改发送通知。
519 520 521 522 523 524 525 526 527 528 |
# File 'lib/ Mongo/ 数据库.rb', line 519 def 观看(管道 = [], = {}) = .dup [:cursor_type] = :tailable_await if [:max_await_time_ms] mongo::Collection::查看::变更流.new( mongo::Collection::查看.new(集合(" #{ COMMAND } .aggregate "), {}, ), 管道, mongo::Collection::查看::变更流::数据库, ) end |