模块:Mongo::Collection::View::Readable
- 定义于:
- lib/ Mongo/ 集合/view/可读.rb
Overview
定义集合视图的读取相关行为。
实例方法摘要折叠
-
#aggregate (pipeline, options = {}) ⇒ 聚合
在集合视图上执行聚合。
-
# allow_disk_use ⇒ 查看
允许服务器在执行查找操作时将临时数据写入磁盘。
-
# allow_partial_results ⇒ 查看
如果某些分片关闭,则允许查询获取部分结果。
-
# await_data ⇒ 查看
让查询的游标保持打开状态并等待数据。
-
# batch_size (batch_size = nil) ⇒ 整数,视图
MongoDB批处理结果中返回的文档数量。
-
# comment (comment = nil) ⇒ string , View
将注释与查询关联。
-
# count (opts = {}) ⇒ 整数
已弃用
已弃用。
请改用 #count_documents 或 #estimated_document_count。 但请注意,切换到 #count_documents 时需要替换以下操作符:
* $where should be replaced with $expr * $near should be replaced with $geoWithin with $center * $nearSphere should be replaced with $geoWithin with $centerSphere -
# count_documents (opts = {}) ⇒ Integer
获取集合中匹配文档的计数。
-
# cursor_type (type = nil) ⇒ :tailable, ...
要使用的游标类型。
-
# distinct (field_name, opts = {}) ⇒ Array<Object>
获取特定字段的非重复值列表。
-
#estimated_document_count (opts = {}) ⇒ Integer
使用集合元数据获取集合中文档的估计数量。
-
#hint(hint = nil) ⇒ Hash, View
MongoDB 将强制用于查询的索引。
-
# limit (limit = nil) ⇒ Integer, View
从查询中返回的最大Docs数。
-
# map_reduce (map, reduce, options = {}) ⇒ MapReduce
在集合视图上执行 map/reduce 操作。
-
# max_await_time_ms (max = nil) ⇒ Integer, View
在游标上处理获取更多操作的累积时间限制(以毫秒为单位)。
-
# max_scan (value = nil) ⇒ Integer, View
已弃用
已弃用。
从 MongoDB 服务器版本4.0开始,此选项已弃用。
-
# max_time_ms (max = nil) ⇒ Integer, View
对游标进行处理操作的累积时间限制(以毫秒为单位)。
-
# max_value (value = nil) ⇒ 哈希,视图
将最大值设置为Atlas Search 。
-
# min_value (value = nil) ⇒ 哈希,视图
将最小值设置为搜索。
-
#modifiers(doc = nil) ⇒ Hash, View
如果不带参数或使用 nil 参数调用,则返回当前视图的传统 (OP_QUERY) 服务器修饰符。
-
#no_cursor_timeout ⇒ View
服务器通常会在不活动期( 10分钟)后使空闲游标超时,以防止过度使用内存。
- #parallel_scan (cursor_count, options = {}) ⇒ 对象
-
投影 (文档 = nil) ⇒ Hash, View
结果设立的每个文档要包含或排除的字段。
-
# read (value = nil) ⇒ 符号,视图
用于查询的读取偏好。
- # read_concern ⇒ 对象 private
- # read_preference ⇒ 对象 private
-
# return_key (value = nil) ⇒ true, ...
设置是否仅返回索引字段。
-
# show_disk_loc (value = nil) ⇒ true, ... (也:#show_record_id)
设置是否应显示每个文档的磁盘位置。
-
# skip (number = nil) ⇒ Integer, View
返回结果之前要跳过的Docs数。
-
# snapshot (value = nil) ⇒ 对象
已弃用
已弃用。
从 MongoDB 服务器版本4.0开始,此选项已弃用。
-
# sort (spec = nil) ⇒ 哈希,视图
对结果设立进行排序的键和方向对。
-
#timeout_ms(timeout_ms = nil) ⇒ Integer, View
每次操作的超时时间(以毫秒为单位)。
实例方法详细信息
#aggregate (管道, options = {}) ⇒聚合
在集合视图上执行聚合。
58 59 60 61 62 63 64 65 66 67 68 69 70 71 |
# File 'lib/ Mongo/ 集合/view/可读.rb', line 58 def 聚合(管道, = {}) = @options.合并(merge)() 除非 mongo. 聚合 = 聚合(Aggregation).new(self, 管道, ) # 因为 $merge 和 $out 管道阶段将文档写入 # 集合,执行时需要清除缓存。 # # 选择清除整个缓存而不是一个命名空间,因为 # $out 和 $merge 阶段不必写入同一命名空间 # 在其上执行聚合。 查询缓存.清除 if 聚合.写入? 聚合 end |
# allow_disk_use ⇒查看
允许服务器在执行查找操作时将临时数据写入磁盘。
77 78 79 |
# File 'lib/ Mongo/ 集合/view/可读.rb', line 77 def allow_disk_use 配置(:allow_disk_use, true) end |
# allow_partial_results ⇒查看
如果某些分片关闭,则允许查询获取部分结果。
89 90 91 |
# File 'lib/ Mongo/ 集合/view/可读.rb', line 89 def allow_partial_results 配置(:allow_partial_results, true) end |
# await_data ⇒查看
让查询的游标保持打开状态并等待数据。
101 102 103 |
# File 'lib/ Mongo/ 集合/view/可读.rb', line 101 def await_data 配置(:await_data, true) end |
# batch_size (batch_size = nil) ⇒ Integer , View
指定1或负数类似于设置限制。
MongoDB批处理结果中返回的文档数量。
118 119 120 |
# File 'lib/ Mongo/ 集合/view/可读.rb', line 118 def batch_size(batch_size = nil) 配置(:batch_size, batch_size) end |
# comment (comment = nil) ⇒ string , View
将 profilingLevel 设置为2 ,评论将与查询一起记录在配置文件集合中。
将注释与查询关联。
136 137 138 |
# File 'lib/ Mongo/ 集合/view/可读.rb', line 136 def comment(comment = nil) 配置(:comment, comment) end |
# count (opts = {}) ⇒整数
请改用 #count_documents 或 #estimated_document_count。 但请注意,切换到 #count_documents 时需要替换以下操作符:
* $where should be replaced with $expr
* $near should be replaced with $geoWithin with $center
* $nearSphere should be replaced with $geoWithin with $centerSphere获取集合中匹配文档的计数。
172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 |
# File 'lib/ Mongo/ 集合/view/可读.rb', line 172 def 数数(opts = {}) opts = @options.合并(merge)(opts) 除非 mongo. cmd = { 计数: 集合.名称, query: 筛选器 } cmd[:skip] = opts[:skip] if opts[:skip] cmd[:hint] = opts[:hint] if opts[:hint] cmd[:limit] = opts[:limit] if opts[:limit] if read_concern cmd[:readConcern] = 选项::映射器.transform_values_to_strings( read_concern ) end cmd[:maxTimeMS] = opts[:max_time_ms] if opts[:max_time_ms] mongo::Lint.validate_underscore_read_preference(opts[:read]) read_pref = opts[:read] || read_preference 选择器 = ServerSelector.获取(read_pref || server_selector) with_session(opts) do |会话| 上下文 = 操作::上下文.new( 客户端: 客户端, 会话: 会话, operation_timeups: operation_timeups(opts) ) 操作 = 操作::数数.new( 选择器: cmd, db_name: database.名称, 选项: { limit: -1 }, 读取: read_pref, 会话: 会话, # 由于某种原因,排序规则在历史上被接受为 # string key. 请注意,这不会被记录为有效用法。 排序规则: opts[:collation] || opts[' collation '] || 排序规则, comment: opts[:comment] ) 追踪器.trace_operation(操作, 上下文) do read_with_retry(会话, 选择器, 上下文) do |server| 操作.执行( server, 上下文: 上下文 ) end.n.to_i end end end |
# count_documents (opts = {}) ⇒ Integer
获取集合中匹配文档的计数。
242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 |
# File 'lib/ Mongo/ 集合/view/可读.rb', line 242 def count_documents(opts = {}) opts = @options.合并(merge)(opts) 除非 mongo. 管道 = [ { ' $match ': 过滤器 } ] 管道 << { ' $skip ': opts [:skip ] } if opts [:skip ] 管道 << { ' $limit ': opts [:limit ] } if opts [:limit ] 管道 << { ' $group ': { _id: 1, n: { ' $sum ': 1 } } } opts = opts.slice (:hint,:max_time_ms,:read,:排序规则,:session,:comment,:timeout_ms ) opts [:排序规则 ] ||= 排序规则 first = aggregate ( 管道, opts ).first return 0 unless first first [ ' n ' ].to_i end |
# cursor_type (type = nil) ⇒ :tailable , ...
要使用的游标类型。 可以是 :tailable 或 :tailable_await。
665 666 667 |
# File 'lib/ Mongo/ 集合/view/可读.rb', line 665 def cursor_type(类型 = nil) 配置(:cursor_type, 类型) end |
# distinct (field_name, opts = {}) ⇒ Array<Object>
获取特定字段的非重复值列表。
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 375 376 377 378 379 380 381 382 383 384 385 386 |
# File 'lib/ Mongo/ 集合/view/可读.rb', line 344 def distinct(field_name, opts = {}) 提高 ArgumentError, '用于非重复操作的字段名称不得为 nil ' if field_name.nil? opts = @options.合并(merge)(opts) 除非 mongo. cmd = { distinct: 集合.名称, 键: field_name.to_s, query: 筛选器, } cmd[:maxTimeMS] = opts[:max_time_ms] if opts[:max_time_ms] if read_concern cmd[:readConcern] = 选项::映射器.transform_values_to_strings( read_concern ) end mongo::Lint.validate_underscore_read_preference(opts[:read]) read_pref = opts[:read] || read_preference 选择器 = ServerSelector.获取(read_pref || server_selector) with_session(opts) do |会话| 上下文 = 操作::上下文.new( 客户端: 客户端, 会话: 会话, operation_timeups: operation_timeups(opts) ) 操作 = 操作::distinct.new( 选择器: cmd, db_name: database.名称, 选项: { limit: -1 }, 读取: read_pref, 会话: 会话, comment: opts[:comment], # 由于某种原因,排序规则在历史上被接受为 # string key. 请注意,这不会被记录为有效用法。 排序规则: opts[:collation] || opts[' collation '] || 排序规则 ) 追踪器.trace_operation(操作, 上下文) do read_with_retry(会话, 选择器, 上下文) do |server| 操作.执行( server, 上下文: 上下文 ) end.first[' values '] end end end |
#estimated_document_count (opts = {}) ⇒ Integer
使用集合元数据获取集合中文档的估计数量。
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 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 |
# File 'lib/ Mongo/ 集合/view/可读.rb', line 278 def estimated_document_count(opts = {}) 除非 查看.筛选器.空? 提高 ArgumentError, ' 使用过滤查询时无法调用estimated_document_count ' end %i[limit 跳过].每 do |opt| if .键?(opt) || opts.键?(opt) 提高 ArgumentError, " 使用 #{ opt} 进行查询时,无法调用estimated_document_count " end end opts = @options.合并(merge)(opts) 除非 mongo. mongo::Lint.validate_underscore_read_preference(opts[:read]) read_pref = opts[:read] || read_preference 选择器 = ServerSelector.获取(read_pref || server_selector) with_session(opts) do |会话| 上下文 = 操作::上下文.new( 客户端: 客户端, 会话: 会话, operation_timeups: operation_timeups(opts) ) cmd = { 计数: 集合.名称 } cmd[:maxTimeMS] = opts[:max_time_ms] if opts[:max_time_ms] cmd[:readConcern] = 选项::映射器.transform_values_to_strings(read_concern) if read_concern 操作 = 操作::数数.new( 选择器: cmd, db_name: database.名称, 读取: read_pref, 会话: 会话, comment: opts[:comment] ) 追踪器.trace_operation(操作, 上下文, op_name: 'estimatedDocumentCount') do read_with_retry(会话, 选择器, 上下文) do |server| 结果 = 操作.执行(server, 上下文: 上下文) 结果.n.to_i end 救援 错误::OperationFailure::家庭情况 => e 提高 除非 e.代码 == 26 # NamespaceNotFound # 这应该只发生在聚合管道路径上 #(服务器4.9 +)。 以前的服务器应返回0 (表示不存在) # collections. 0 end end end |
#hint (hint = nil) ⇒ Hash , View
MongoDB 将强制用于查询的索引。
398 399 400 |
# File 'lib/ Mongo/ 集合/view/可读.rb', line 398 def 提示(提示 = nil) 配置(:hint, 提示) end |
#limit(limit = nil) ⇒ Integer, View
从查询中返回的最大Docs数。
412 413 414 |
# File 'lib/ Mongo/ 集合/view/可读.rb', line 412 def limit(limit = nil) 配置(:limit, limit) end |
# map_reduce (map, reduce, options = {}) ⇒ MapReduce
在集合视图上执行 map/reduce 操作。
428 429 430 |
# File 'lib/ Mongo/ 集合/view/可读.rb', line 428 def map_reduce(map, 化简(reduce), = {}) MapReduce.new(self, map, 化简(reduce), @options.合并(merge)()) end |
# max_await_time_ms (max = nil) ⇒ Integer , View
在游标上处理获取更多操作的累积时间限制(以毫秒为单位)。
637 638 639 |
# File 'lib/ Mongo/ 集合/view/可读.rb', line 637 def max_await_time_ms(Max = nil) 配置(:max_await_time_ms, Max) end |
# max_scan (value = nil) ⇒ Integer , View
从 MongoDB 服务器版本4.0开始,此选项已弃用。
设置要扫描的最大文档数。
445 446 447 |
# File 'lib/ Mongo/ 集合/view/可读.rb', line 445 def max_scan(值 = nil) 配置(:max_scan, 值) end |
# max_time_ms (max = nil) ⇒ Integer , View
对游标进行处理操作的累积时间限制(以毫秒为单位)。
651 652 653 |
# File 'lib/ Mongo/ 集合/view/可读.rb', line 651 def max_time_ms(Max = nil) 配置(:max_time_ms, Max) end |
# max_value (value = nil) ⇒ Hash , View
将最大值设置为Atlas Search 。
459 460 461 |
# File 'lib/ Mongo/ 集合/view/可读.rb', line 459 def max_value(值 = nil) 配置(:max_value, 值) end |
# min_value (value = nil) ⇒ Hash , View
将最小值设置为搜索。
473 474 475 |
# File 'lib/ Mongo/ 集合/view/可读.rb', line 473 def min_value(值 = nil) 配置(:min_value, 值) end |
#modifiers(doc = nil) ⇒ Hash, View
如果不带参数或使用 nil 参数调用,则返回当前视图的传统 (OP_QUERY)服务器修饰符。 如果使用非 nil 参数(必须是哈希或子类)调用,则将提供的修饰符合并到当前视图中。 输入哈希中允许使用string键和符号键。
618 619 620 621 622 623 624 |
# File 'lib/ Mongo/ 集合/view/可读.rb', line 618 def modifiers(doc = nil) if doc.nil? 操作::find::生成器::Modifiers.map_server_modifiers() else new(.合并(merge)(操作::find::生成器::Modifiers.(BSON::文档.new(doc)))) end end |
# no_cursor_timeout ⇒查看
服务器通常会在不活动期( 10分钟)后使空闲游标超时,以防止过度使用内存。 设置此选项可防止出现这种情况。
486 487 488 |
# File 'lib/ Mongo/ 集合/view/可读.rb', line 486 def no_cursor_timeout 配置(:no_cursor_timeout, true) end |
#parallel_scan (cursor_count, options = {}) ⇒ 对象
707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 |
# File 'lib/ Mongo/ 集合/view/可读.rb', line 707 def parallel_scan(cursor_count, = {}) 会话 = if [:session] # 该会话稍后将被 +options+ 中的会话覆盖。 客户端.get_session(@options) end server = server_selector.select_server(集群, nil, 会话) spec = { coll_name: 集合.名称, db_name: database.名称, cursor_count: cursor_count, read_concern: read_concern, 会话: 会话, }.update() 会话 = spec[:session] op = 操作::ParallelScan.new(spec) # 请注意,上下文对象不应重复用于后续 # GetMore 操作。 上下文 = 操作::上下文.new(客户端: 客户端, 会话: 会话) 结果 = op.执行(server, 上下文: 上下文) 结果.cursor_id.map do |cursor_id| spec = { cursor_id: cursor_id, coll_name: 集合.名称, db_name: database.名称, 会话: 会话, batch_size: batch_size, # max_time_ms 没有在这里传递,我认为是故意的? } op = 操作::getMore.new(spec) 上下文 = 操作::上下文.new( 客户端: 客户端, 会话: 会话, connection_global_id: 结果.connection_global_id ) 结果 = if server.load_balancer? # 当游标耗尽时,将签入连接。 连接 = server.池.check_out(上下文: 上下文) op.execute_with_connection(连接, 上下文: 上下文) else op.执行(server, 上下文: 上下文) end Cursor.new(self, 结果, server, 会话: 会话) end end |
投影 (文档 = nil) ⇒ Hash , View
值为0会从文档中排除字段。 值为1时会将其包含在内。 除_id值外,所有值必须均为0或均为1 。 默认包含_id字段。 必须明确将其排除。
结果设立的每个文档要包含或排除的字段。
504 505 506 507 |
# File 'lib/ Mongo/ 集合/view/可读.rb', line 504 def 投影(文档 = nil) validate_doc!(文档) if 文档 配置(:projection, 文档) end |
# read (value = nil) ⇒符号,视图
如果未为查询指定任何内容,则将使用集合的读取偏好(read preference)。
用于查询的读取偏好。
520 521 522 523 524 |
# File 'lib/ Mongo/ 集合/view/可读.rb', line 520 def 读(值 = nil) return read_preference if 值.nil? 配置(:read, 值) end |
# read_concern ⇒对象
此方法是私有 API 的一部分。 您应尽可能避免使用此方法,因为它将来可能会被删除或更改。
679 680 681 682 683 684 685 |
# File 'lib/ Mongo/ 集合/view/可读.rb', line 679 def read_concern if [:session] && [:session].in_transaction? [:session].发送(:txn_read_concern) || 集合.客户端.read_concern else 集合.read_concern end end |
# read_preference ⇒对象
此方法是私有 API 的一部分。 您应尽可能避免使用此方法,因为它将来可能会被删除或更改。
688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 |
# File 'lib/ Mongo/ 集合/view/可读.rb', line 688 def read_preference @read_preference ||= 开始 # 操作读取偏好(read preference)始终受到尊重,并且具有 # 最高优先级。 如果处于ACID 事务中,我们会查看 #ACID 事务读取偏好(read preference),默认为客户端,忽略 #集合读取偏好(read preference)。 如果我们没有处于ACID 事务状态 # 查看集合读取偏好(read preference),默认为客户端。 rp = if [:read] [:read] elsif [:session] && [:session].in_transaction? [:session].txn_read_preference || 集合.客户端.read_preference else 集合.read_preference end Lint.validate_underscore_read_preference(rp) rp end end |
# return_key (value = nil) ⇒ true , ...
设置是否仅返回索引字段。
536 537 538 |
# File 'lib/ Mongo/ 集合/view/可读.rb', line 536 def return_key(值 = nil) 配置(:return_key, 值) end |
# show_disk_loc (value = nil) ⇒ true , ...也称为: show_record_id
设置是否应显示每个文档的磁盘位置。
551 552 553 |
# File 'lib/ Mongo/ 集合/view/可读.rb', line 551 def show_disk_loc(值 = nil) 配置(:show_disk_loc, 值) end |
# skip (number = nil) ⇒ Integer , View
返回结果之前要跳过的Docs数。
567 568 569 |
# File 'lib/ Mongo/ 集合/view/可读.rb', line 567 def 跳过(数字 = nil) 配置(:skip, 数字) end |
# 快照 (value = nil) ⇒对象
从 MongoDB 服务器版本4.0开始,此选项已弃用。
设立为 true 时,可防止文档多次返回。
设置视图的快照值。
585 586 587 |
# File 'lib/ Mongo/ 集合/view/可读.rb', line 585 def snapshot(值 = nil) 配置(:快照, 值) end |
# sort (spec = nil) ⇒ Hash , View
对结果设立进行排序的键和方向对。
600 601 602 |
# File 'lib/ Mongo/ 集合/view/可读.rb', line 600 def sort(spec = nil) 配置(:sort, spec) end |
#timeout_ms(timeout_ms = nil) ⇒ Integer, View
每次操作的超时时间(以毫秒为单位)。 必须为正整数。
674 675 676 |
# File 'lib/ Mongo/ 集合/view/可读.rb', line 674 def timeout_ms(timeout_ms = nil) 配置(:timeout_ms, timeout_ms) end |