模块:Mongoid::Clients::Options

扩展方式:
ActiveSupport::Concern
包含在:
Mongoid::ClientsMongoid::Criteria
定义于:
lib/mongoid/clients/options.rb

Overview

Mongoid::Document 中包含的 Mixin 模块能力了管理数据库上下文以实现持久性和查询操作的能力。 示例,这包括将文档保存到不同的集合,以及从从从节点(secondary node from replica set)实例读取文档。

在命名空间下定义

模块: 类方法

实例方法摘要折叠

实例方法详细信息

# 集合 (parent = nil) ⇒ Mongo::Collection

获取文档的当前持久性上下文的集合。

例子:

获取当前持久性上下文的集合。

document.collection

参数:

  • 父项 对象 (默认为: nil

    使用其集合名称而不是当前持久性上下文的集合名称的父对象。

返回:

  • ( Mongo::Collection )

    当前持久性上下文的集合。



47
48
49
# File 'lib/mongoid/clients/options.rb', line 47

def 集合(父项 = nil)
  persistence_context.集合(父项)
end

# collection_namestring

获取文档的当前持久性上下文的集合名称。

例子:

获取当前持久性上下文的集合名称。

document.collection_name

返回:

  • ( string )

    当前持久性上下文的集合名称。



58
59
60
# File 'lib/mongoid/clients/options.rb', line 58

def collection_name
  persistence_context.collection_name
end

# mongo_clientMongo::Client

获取文档的当前持久性上下文的数据库客户端。

例子:

获取当前持久性上下文的客户端。

document.mongo_client

返回:

  • ( Mongo::Client )

    当前持久性上下文的客户端。



69
70
71
# File 'lib/mongoid/clients/options.rb', line 69

def mongo_client
  persistence_context.客户端
end

#persistence_contextMongoid::PersistenceContext

注意:

对于嵌入式文档,返回根父文档的持久性上下文。

获取文档的当前持久性上下文。

例子:

获取当前的持久化上下文。

document.persistence_context

返回:



83
84
85
86
87
88
89
90
91
# File 'lib/mongoid/clients/options.rb', line 83

def persistence_context
  if 嵌入式? && !_root?
    _root.persistence_context
  else
    PersistenceContext.获取(self) ||
      PersistenceContext.获取(self.class) ||
      PersistenceContext.new(self.class, default_storage_options)
  end
end

#persistence_context?true | false

注意:

对于嵌入式文档,使用根父文档的持久性上下文。

返回是否为文档或文档的类设立了持久性上下文。

例子:

获取当前的持久化上下文。

document.persistence_context?

返回:

  • ( true | false )

    是否设立了持久化上下文。



103
104
105
106
107
108
109
110
111
# File 'lib/mongoid/clients/options.rb', line 103

def persistence_context?
  if 嵌入式? && !_root?
    _root.persistence_context?
  else
    membered_storage_options&。任何? ||
      PersistenceContext.获取(self).现在? ||
      PersistenceContext.获取(self.class).现在?
  end
end

# with (options_or_context, &block) ⇒对象

在区块期间更改此对象的持久性上下文。

例子:

将当前文档保存到其他集合中。

model.with(collection: "bands") do |m|
  m.save
end

参数:

  • options_or_context ( Hash | Mongoid::PersistenceContext )

    存储选项或持久性上下文。

  • 选项 (哈希)

    一组可自定义的选项



28
29
30
31
32
33
34
35
# File 'lib/mongoid/clients/options.rb', line 28

def 通过(options_or_context, )
  ORIGIN_CONTEXT = PersistenceContext.获取(self)
  original_cluster = persistence_context.集群
  set_persistence_context(options_or_context)
  产量 self
确保
  clear_persistence_context(original_cluster, ORIGIN_CONTEXT)
end