模块:Mongo::Collection::View::Writable

包含在:
Mongo::Collection::View
定义于:
lib/ Mongo/ 集合/view/writable.rb

Overview

定义集合视图的写入相关行为。

由于:

  • 2.0.0

常量摘要折叠

ARRAY_FILTERS =

数组筛选器字段常量。

由于:

  • 2.5.0

' array_filters '.冻结

实例方法摘要折叠

实例方法详细信息

# delete_many (opts = {}) ⇒结果

从集合中删除文档。

例子:

从集合中删除多个文档。

collection_view.delete_many

参数:

  • opts 哈希 (默认为: {}

    选项。

  • 选项 (哈希)

    一组可自定义的选项

选项哈希 ( opts ):

  • :collation 哈希

    要使用的排序规则。

  • :session 会话

    要使用的会话。

  • :hint 哈希 | string

    用于此操作的索引。 可以指定为哈希值(例如 { _id : 1 })或string (例如 “ ID ”)。

  • :write_concern 哈希

    写关注(write concern)选项。 可以是 :w => 整数、: fsync => 布尔值、:j => 布尔值。

  • :let 哈希

    要在命令中使用的变量映射。 有关详细信息,请参阅服务器文档。

  • :comment 对象

    用户提供的待附加到该命令的注释。

返回:

  • (结果)

    来自数据库的响应。

由于:

  • 2.0.0



257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
# File 'lib/ Mongo/ 集合/view/writable.rb', line 257

def delete_many(opts = {})
  with_session(opts) do |会话|
    write_concern = if opts[:write_concern]
      writeConcern.获取(opts[:write_concern])
    else
      write_concern_with_session(会话)
    end

    查询缓存.clear_namespace(集合.namespace)

    delete_doc = {
      操作::q => 筛选器,
      操作::LIMIT => 0,
      hint: opts[:hint],
      排序规则: opts[:collation] || opts[' collation '] || 排序规则,
    }.compact
    上下文 = 操作::上下文.new(
      客户端: 客户端,
      会话: 会话,
      operation_timeups: operation_timeups(opts)
    )
    nro_write_with_retry(write_concern, 上下文: 上下文) do |连接, txn_num, 上下文|
      gte_4_4 = 连接.server.描述.server_version_gte?(' 4.4 ')
      if !gte_4_4 && opts[:hint] && write_concern && !write_concern.已确认?
        提高 错误::UnsupportedOption.hint_error(unacknowledged_write: true)
      end

      操作::删除.new(
        删除: [ delete_doc ],
        db_name: 集合.database.名称,
        coll_name: 集合.名称,
        write_concern: write_concern,
        bypass_document_validation: !!opts[:bypass_document_validation],
        会话: 会话,
        让: opts[:let],
        comment: opts[:comment],
      ).execute_with_connection(连接, 上下文: 上下文)
    end
  end
end

# delete_one (opts = {}) ⇒结果

从集合中删除文档。

例子:

从集合中删除单个文档。

collection_view.delete_one

参数:

  • opts 哈希 (默认为: {}

    选项。

  • 选项 (哈希)

    一组可自定义的选项

选项哈希 ( opts ):

  • :collation 哈希

    要使用的排序规则。

  • :comment 对象

    用户提供的待附加到该命令的注释。

  • :hint 哈希 | string

    用于此操作的索引。 可以指定为哈希值(例如 { _id : 1 })或string (例如 “ ID ”)。

  • :let 哈希

    要在命令中使用的变量映射。 有关详细信息,请参阅服务器文档。

  • :session 会话

    要使用的会话。

  • :write_concern 哈希

    写关注(write concern)选项。 可以是 :w => 整数、: fsync => 布尔值、:j => 布尔值。

返回:

  • (结果)

    来自数据库的响应。

由于:

  • 2.0.0



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
# File 'lib/ Mongo/ 集合/view/writable.rb', line 323

def delete_one(opts = {})
  with_session(opts) do |会话|
    write_concern = if opts[:write_concern]
      writeConcern.获取(opts[:write_concern])
    else
      write_concern_with_session(会话)
    end

    查询缓存.clear_namespace(集合.namespace)

    delete_doc = {
      操作::q => 筛选器,
      操作::LIMIT => 1,
      hint: opts[:hint],
      排序规则: opts[:collation] || opts[' collation '] || 排序规则,
    }.compact

    上下文 = 操作::上下文.new(
      客户端: 客户端,
      会话: 会话,
      operation_timeups: operation_timeups(opts)
    )
    write_with_retry(write_concern, 上下文: 上下文) do |连接, txn_num, 上下文|
      gte_4_4 = 连接.server.描述.server_version_gte?(' 4.4 ')
      if !gte_4_4 && opts[:hint] && write_concern && !write_concern.已确认?
        提高 错误::UnsupportedOption.hint_error(unacknowledged_write: true)
      end

      操作::删除.new(
        删除: [ delete_doc ],
        db_name: 集合.database.名称,
        coll_name: 集合.名称,
        write_concern: write_concern,
        bypass_document_validation: !!opts[:bypass_document_validation],
        会话: 会话,
        txn_num: txn_num,
        让: opts[:let],
        comment: opts[:comment],
      ).execute_with_connection(连接, 上下文: 上下文)
    end
  end
end

# find_one_and_delete (opts = {}) ⇒ BSON::Document ?

通过 findAndModify 在数据库中查找单个文档并将其删除,同时返回原始文档。

例子:

查找一个文档并将其删除。

view.find_one_and_delete

参数:

  • opts 哈希 (默认为: {}

    选项。

  • 选项 (哈希)

    一组可自定义的选项

选项哈希 ( opts ):

  • :max_time_ms 整数

    允许命令运行的最长时间(以毫秒为单位)。 此选项已弃用,请改用 :timeout_ms。

  • :projection 哈希

    要在返回的文档中包含或排除的字段。

  • :sort 哈希

    对结果设立进行排序的键和方向对。

  • :collation 哈希

    要使用的排序规则。

  • :session 会话

    要使用的会话。

  • :hint 哈希 | string

    用于此操作的索引。 可以指定为哈希值(例如 { _id : 1 })或string (例如 “ ID ”)。

  • :write_concern 哈希

    写关注(write concern)选项。 可以是 :w => 整数、: fsync => 布尔值、:j => 布尔值。

  • :let 哈希

    要在命令中使用的变量映射。 有关详细信息,请参阅服务器文档。

  • :comment 对象

    用户提供的待附加到该命令的注释。

返回:

  • (BSON::Document, nil)

    该文档(如果找到)。

由于:

  • 2.0.0



64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
# File 'lib/ Mongo/ 集合/view/writable.rb', line 64

def find_one_and_delete(opts = {})
  with_session(opts) do |会话|
    write_concern = if opts[:write_concern]
      writeConcern.获取(opts[:write_concern])
    else
      write_concern_with_session(会话)
    end

    查询缓存.clear_namespace(集合.namespace)

    cmd = {
      findAndModify: 集合.名称,
      query: 筛选器,
      删除: true,
      字段: 投影,
      sort: sort,
      maxTimeMS: max_time_ms,
      bypassDocumentValidation: opts[:bypass_document_validation],
      hint: opts[:hint],
      排序规则: opts[:collation] || opts[' collation '] || 排序规则,
      让: opts[:let],
      comment: opts[:comment],
    }.compact

    上下文 = 操作::上下文.new(
      客户端: 客户端,
      会话: 会话,
      operation_timeups: operation_timeups(opts)
    )
    write_with_retry(write_concern, 上下文: 上下文) do |连接, txn_num, 上下文|
      gte_4_4 = 连接.server.描述.server_version_gte?(' 4.4 ')
      if !gte_4_4 && opts[:hint] && write_concern && !write_concern.已确认?
        提高 错误::UnsupportedOption.hint_error(unacknowledged_write: true)
      end

      操作::WriteCommand.new(
        选择器: cmd,
        db_name: database.名称,
        write_concern: write_concern,
        会话: 会话,
        txn_num: txn_num,
      ).execute_with_connection(连接, 上下文: 上下文)
    end
  end.first&。获取('value', nil)
end

# find_one_and_replace (replacement, opts = {}) ⇒ BSON::Document

查找并更新单个文档。

例子:

查找文档并将其替换,从而返回原始文档。

view.find_one_and_replace({ name: 'test' }, :return_document => :before)

查找并替换文档,从而返回新文档。

view.find_one_and_replace({ name: 'test' }, :return_document => :after)

参数:

  • 替换 ( BSON::Document )

    替换。

  • opts 哈希 (默认为: {}

    选项。

  • 选项 (哈希)

    一组可自定义的选项

选项哈希 ( opts ):

  • :return_document 符号

    :before 或 :after。

  • :更新或插入(upsert) ( true , false )

    如果文档不存在,是否进行更新或插入(upsert)或插入。

  • :bypass_document_validation ( true , false )

    是否跳过文档级验证。

  • :collation 哈希

    要使用的排序规则。

  • :hint 哈希 | string

    用于此操作的索引。 可以指定为哈希值(例如 { _id : 1 })或string (例如 “ ID ”)。

  • :write_concern 哈希

    写关注(write concern)选项。 可以是 :w => 整数、: fsync => 布尔值、:j => 布尔值。

  • :let 哈希

    要在命令中使用的变量映射。 有关详细信息,请参阅服务器文档。

返回:

  • ( BSON::Document )

    文档。

由于:

  • 2.0.0



140
141
142
# File 'lib/ Mongo/ 集合/view/writable.rb', line 140

def find_one_and_replace(替换, opts = {})
  find_one_and_update(替换, opts)
end

#find_one_and_update(document, opts = {}) ⇒ BSON::Document | nil

查找并更新单个文档。

应应用更新。

例子:

查找并更新文档,同时返回原始文档。

view.find_one_and_update({ "$set" => { name: 'test' }}, :return_document => :before)

参数:

  • 文档 ( BSON::Document )

    更新。

  • opts 哈希 (默认为: {}

    选项。

  • 选项 (哈希)

    一组可自定义的选项

选项哈希 ( opts ):

  • :max_time_ms 整数

    允许命令运行的最长时间(以毫秒为单位)。 此选项已弃用,请改用 :timeout_ms。

  • :projection 哈希

    要在返回的文档中包含或排除的字段。

  • :sort 哈希

    对结果设立进行排序的键和方向对。

  • :return_document 符号

    :before 或 :after。

  • :更新或插入(upsert) ( true , false )

    如果文档不存在,是否进行更新或插入(upsert)或插入。

  • :bypass_document_validation ( true , false )

    是否跳过文档级验证。

  • :collation 哈希

    要使用的排序规则。

  • :array_filters 数组

    一设立筛选器,用于指定哪些大量元素

  • :session 会话

    要使用的会话。

  • :hint 哈希 | string

    用于此操作的索引。 可以指定为哈希值(例如 { _id : 1 })或string (例如 “ ID ”)。

  • :write_concern 哈希

    写关注(write concern)选项。 可以是 :w => 整数、: fsync => 布尔值、:j => 布尔值。

  • :let 哈希

    要在命令中使用的变量映射。 有关详细信息,请参阅服务器文档。

  • :comment 对象

    用户提供的待附加到该命令的注释。

返回:

  • ( BSON::Document | nil )

    该文档;如果未找到,则为 nil。

由于:

  • 2.0.0



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
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
# File 'lib/ Mongo/ 集合/view/writable.rb', line 182

def find_one_and_update(文档, opts = {})
   = with_session(opts) do |会话|
    write_concern = if opts[:write_concern]
      writeConcern.获取(opts[:write_concern])
    else
      write_concern_with_session(会话)
    end

    查询缓存.clear_namespace(集合.namespace)

    cmd = {
      findAndModify: 集合.名称,
      query: 筛选器,
      arrayFilters: opts[:array_filters] || opts[' array_filters '],
      update: 文档,
      字段: 投影,
      sort: sort,
      新客户: !!(opts[:return_document] && opts[:return_document] == :after),
      更新或插入: opts[:更新或插入(upsert)],
      maxTimeMS: max_time_ms,
      bypassDocumentValidation: opts[:bypass_document_validation],
      hint: opts[:hint],
      排序规则: opts[:collation] || opts[' collation '] || 排序规则,
      让: opts[:let],
      comment: opts[:comment]
    }.compact

    上下文 = 操作::上下文.new(
      客户端: 客户端,
      会话: 会话,
      operation_timeups: operation_timeups(opts)
    )
    write_with_retry(write_concern, 上下文: 上下文) do |连接, txn_num, 上下文|
      gte_4_4 = 连接.server.描述.server_version_gte?(' 4.4 ')
      if !gte_4_4 && opts[:hint] && write_concern && !write_concern.已确认?
        提高 错误::UnsupportedOption.hint_error(unacknowledged_write: true)
      end

      操作::WriteCommand.new(
        选择器: cmd,
        db_name: database.名称,
        write_concern: write_concern,
        会话: 会话,
        txn_num: txn_num,
      ).execute_with_connection(连接, 上下文: 上下文)
    end
  end.first&。获取('value', nil)
   除非 .nil? || .空?
end

# replace_one (replacement, opts = {}) ⇒结果

用新文档替换数据库中的单个文档。

例子:

替换单个文档。

collection_view.replace_one({ name: 'test' })

参数:

  • 替换 (哈希)

    替换文档。

  • opts 哈希 (默认为: {}

    选项。

  • 选项 (哈希)

    一组可自定义的选项

选项哈希 ( opts ):

  • :bypass_document_validation ( true , false )

    是否跳过文档级验证。

  • :collation 哈希

    要使用的排序规则。

  • :comment 对象

    用户提供的待附加到该命令的注释。

  • :hint 哈希 | string

    用于此操作的索引。 可以指定为哈希值(例如 { _id : 1 })或string (例如 “ ID ”)。

  • :let 哈希

    要在命令中使用的变量映射。 有关详细信息,请参阅服务器文档。

  • :session 会话

    要使用的会话。

  • :write_concern 哈希

    写关注(write concern)选项。

  • :更新或插入(upsert) ( true , false )

    如果文档不存在,是否更新或插入(upsert)。 可以是 :w => 整数、: fsync => 布尔值、:j => 布尔值。

返回:

  • (结果)

    来自数据库的响应。

由于:

  • 2.0.0



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
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
# File 'lib/ Mongo/ 集合/view/writable.rb', line 396

def replace_one(替换, opts = {})
  with_session(opts) do |会话|
    write_concern = if opts[:write_concern]
      writeConcern.获取(opts[:write_concern])
    else
      write_concern_with_session(会话)
    end
    validate_replacement_documents!(替换)

    查询缓存.clear_namespace(集合.namespace)

    update_doc = {
      操作::q => 筛选器,
      arrayFilters: opts[:array_filters] || opts[' array_filters '],
      操作::u => 替换,
      hint: opts[:hint],
      排序规则: opts[:collation] || opts[' collation '] || 排序规则,
    }.compact
    if opts[:更新或插入(upsert)]
      update_doc['upsert'] = true
    end

    上下文 = 操作::上下文.new(
      客户端: 客户端,
      会话: 会话,
      operation_timeups: operation_timeups(opts)
    )
    write_with_retry(write_concern, 上下文: 上下文) do |连接, txn_num, 上下文|
      gte_4_2 = 连接.server.描述.server_version_gte?(' 4.2 ')
      if !gte_4_2 && opts[:hint] && write_concern && !write_concern.已确认?
        提高 错误::UnsupportedOption.hint_error(unacknowledged_write: true)
      end

      操作::Update.new(
        updates: [ update_doc ],
        db_name: 集合.database.名称,
        coll_name: 集合.名称,
        write_concern: write_concern,
        bypass_document_validation: !!opts[:bypass_document_validation],
        会话: 会话,
        txn_num: txn_num,
        让: opts[:let],
        comment: opts[:comment],
      ).execute_with_connection(连接, 上下文: 上下文)
    end
  end
end

# update_many (spec, opts = {}) ⇒结果

更新集合中的文档。

例子:

更新集合中的多个文档。

collection_view.update_many('$set' => { name: 'test' })

参数:

  • spec ( Hash | Array<Hash> )

    更新文档或管道。

  • opts 哈希 (默认为: {}

    选项。

  • 选项 (哈希)

    一组可自定义的选项

选项哈希 ( opts ):

  • :array_filters 数组

    一设立筛选器,用于指定应将更新应用哪些大量元素。

  • :bypass_document_validation ( true , false )

    是否跳过文档级验证。

  • :collation 哈希

    要使用的排序规则。

  • :comment 对象

    用户提供的待附加到该命令的注释。

  • :hint 哈希 | string

    用于此操作的索引。 可以指定为哈希值(例如 { _id : 1 })或string (例如 “ ID ”)。

  • :let 哈希

    要在命令中使用的变量映射。 有关详细信息,请参阅服务器文档。

  • :session 会话

    要使用的会话。

  • :更新或插入(upsert) ( true , false )

    如果文档不存在,是否进行更新或插入(upsert)或插入。

  • :write_concern 哈希

    写关注(write concern)选项。 可以是 :w => 整数、: fsync => 布尔值、:j => 布尔值。

返回:

  • (结果)

    来自数据库的响应。

由于:

  • 2.0.0



476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
# File 'lib/ Mongo/ 集合/view/writable.rb', line 476

def update_many(spec, opts = {})
  with_session(opts) do |会话|
    write_concern = if opts[:write_concern]
      writeConcern.获取(opts[:write_concern])
    else
      write_concern_with_session(会话)
    end
    validate_update_documents!(spec)

    查询缓存.clear_namespace(集合.namespace)

    update_doc = {
      操作::q => 筛选器,
      arrayFilters: opts[:array_filters] || opts[' array_filters '],
      操作::u => spec,
      操作::多分析器 => true,
      hint: opts[:hint],
      排序规则: opts[:collation] || opts[' collation '] || 排序规则,
    }.compact
    if opts[:更新或插入(upsert)]
      update_doc['upsert'] = true
    end

    上下文 = 操作::上下文.new(
      客户端: 客户端,
      会话: 会话,
      operation_timeups: operation_timeups(opts)
    )
    nro_write_with_retry(write_concern, 上下文: 上下文) do |连接, txn_num, 上下文|
      gte_4_2 = 连接.server.描述.server_version_gte?(' 4.2 ')
      if !gte_4_2 && opts[:hint] && write_concern && !write_concern.已确认?
        提高 错误::UnsupportedOption.hint_error(unacknowledged_write: true)
      end

      操作::Update.new(
        updates: [ update_doc ],
        db_name: 集合.database.名称,
        coll_name: 集合.名称,
        write_concern: write_concern,
        bypass_document_validation: !!opts[:bypass_document_validation],
        会话: 会话,
        让: opts[:let],
        comment: opts[:comment],
      ).execute_with_connection(连接, 上下文: 上下文)
    end
  end
end

# update_one (spec, opts = {}) ⇒结果

更新集合中的单个文档。

例子:

更新集合中的单个文档。

collection_view.update_one('$set' => { name: 'test' })

参数:

  • spec ( Hash | Array<Hash> )

    更新文档或管道。

  • opts 哈希 (默认为: {}

    选项。

  • 选项 (哈希)

    一组可自定义的选项

选项哈希 ( opts ):

  • :array_filters 数组

    一设立筛选器,用于指定应将更新应用哪些大量元素。

  • :bypass_document_validation ( true , false )

    是否跳过文档级验证。

  • :collation 哈希

    要使用的排序规则。

  • :comment 对象

    用户提供的待附加到该命令的注释。

  • :hint 哈希 | string

    用于此操作的索引。 可以指定为哈希值(例如 { _id : 1 })或string (例如 “ ID ”)。

  • :let 哈希

    要在命令中使用的变量映射。 有关详细信息,请参阅服务器文档。

  • :session 会话

    要使用的会话。

  • :更新或插入(upsert) ( true , false )

    如果文档不存在,是否进行更新或插入(upsert)或插入。

  • :write_concern 哈希

    写关注(write concern)选项。 可以是 :w => 整数、: fsync => 布尔值、:j => 布尔值。

返回:

  • (结果)

    来自数据库的响应。

由于:

  • 2.0.0



556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
# File 'lib/ Mongo/ 集合/view/writable.rb', line 556

def update_one(spec, opts = {})
  with_session(opts) do |会话|
    write_concern = if opts[:write_concern]
      writeConcern.获取(opts[:write_concern])
    else
      write_concern_with_session(会话)
    end
    validate_update_documents!(spec)

    查询缓存.clear_namespace(集合.namespace)

    update_doc = {
      操作::q => 筛选器,
      arrayFilters: opts[:array_filters] || opts[' array_filters '],
      操作::u => spec,
      hint: opts[:hint],
      排序规则: opts[:collation] || opts[' collation '] || 排序规则,
    }.compact
    if opts[:更新或插入(upsert)]
      update_doc['upsert'] = true
    end

    上下文 = 操作::上下文.new(
      客户端: 客户端,
      会话: 会话,
      operation_timeups: operation_timeups(opts)
    )
    write_with_retry(write_concern, 上下文: 上下文) do |连接, txn_num, 上下文|
      gte_4_2 = 连接.server.描述.server_version_gte?(' 4.2 ')
      if !gte_4_2 && opts[:hint] && write_concern && !write_concern.已确认?
        提高 错误::UnsupportedOption.hint_error(unacknowledged_write: true)
      end

      操作::Update.new(
        updates: [ update_doc ],
        db_name: 集合.database.名称,
        coll_name: 集合.名称,
        write_concern: write_concern,
        bypass_document_validation: !!opts[:bypass_document_validation],
        会话: 会话,
        txn_num: txn_num,
        让: opts[:let],
        comment: opts[:comment],
      ).execute_with_connection(连接, 上下文: 上下文)
    end
  end
end