模块:Mongoid::Persistable::Upsertable
- 包含在:
- Mongoid::Persistable
- 定义于:
- lib/mongoid/persistable/upsertable.rb
Overview
定义更新或插入(upsert)文档的持久性操作的行为。
实例方法摘要折叠
-
# 更新或插入(upsert) (options = {}) ⇒ true
对文档执行更新或插入(upsert)或插入。
实例方法详细信息
# 更新或插入(upsert) (options = {}) ⇒ true
对文档执行更新或插入(upsert)或插入。 如果数据库中不存在该文档,则mongo将插入一个新文档,否则字段将被现有文档上的新值覆盖。
如果替换选项为 true,则删除未指定的属性;如果为 false,则保留未指定的属性。 在 Mongoid 9中,替换选项默认为 false。
36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 |
# File 'lib/mongoid/persistable/upsertable.rb', line 36 def 更新插入( = {}) prepare_upsert() do if [:replace] if [:set_on_insert] 提高 ArgumentError, "无法指定 :set_on_insert with `replace: true` " end 集合.find(atomic_selector).replace_one( as_attributes, 更新或插入: true, 会话: _session) else attrs = { " $set " => as_attributes } attrs["$setOnInsert"] = [:set_on_insert] if [:set_on_insert] 集合.find(atomic_selector).update_one( attrs, 更新或插入: true, 会话: _session) end end end |