模块:Mongoid::Fields::ClassMethods

定义于:
lib/mongoid/fields.rb ,
lib/mongoid/fields.rb

实例方法摘要折叠

实例方法详细信息

#attribute_namesArray<String>

返回此对象可用属性的名称大量。

以与 ORM 无关的方式提供字段名称。 Rails v 3.1 + 使用此方法自动包装JSON请求中的参数。

例子:

获取字段名称

Model.attribute_names

返回:

  • ( Array<String> )

    字段名称



449
450
451
# File 'lib/mongoid/fields.rb', line 449

def attribute_names
  字段.密钥
end

# auto_embed_field (name, model: 'voyage-4', num_dimensions: nil, quantization: nil, 相似度: nil, 索引: nil) ⇒ 对象

声明一个文本字段,并使用自动嵌入 (autoEmbed) 类型为其注册相应的Atlas Vector Search索引。Atlas在索引和查询时自动生成嵌入;无需预先计算向量。

例子:

最小声明。

class Article
  include Mongoid::Document
  auto_embed_field :description
end

包含所有选项。

class Article
  include Mongoid::Document
  auto_embed_field :description,
                   model: 'voyage-4',
                   num_dimensions: 512,
                   quantization: 'binary',
                   similarity: 'cosine',
                   index: :article_embed
end

参数:

  • 名称 (符号 | string )

    要声明和自动嵌入的文本字段的名称。

  • 模型 string (默认为: 'voyage-4')

    embedding model 名称。默认为“voyage-4”(推荐)。公开预览版支持的值: voyage-4-large、 voyage-4、 voyage-4-lite、 voyage-code-3。

  • num_dimensions ( Integer | nil ) (默认为: nil

    向量 dimensions 数。支持的值:256、512、1024、2048。默认值:1024。

  • 量化 ( string | nil ) (默认为: nil

    quantization type: float, scalar, binary, or binaryNoRescore。默认值:标量。

  • 相似度 ( string | nil ) (默认为: nil

    相似度函数:dotProduct、cosine 或 euclidean。

  • 索引(index) (Symbol | String | nil) (默认为: nil

    索引名称。如果省略,则索引未命名,Atlas将其调用为“默认”。



542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
# File 'lib/mongoid/fields.rb', line 542

def auto_embed_field(名称, 模型: 'voyage-4', num_dimensions: nil, quantization: nil, 相似度: nil, index: nil)
  字段(名称, 类型: 字符串)

  field_spec = {
    类型: 'autoEmbed',
    模态: 'text',
    path: 名称.to_s,
    模型: 模型
  }
  field_spec[numDimensions] = num_dimensions if num_dimensions
  field_spec[quantization]  = 量化  if 量化
  field_spec[:similarity]    = 相似度    if 相似度

  if 索引(index)
    vector_search_index(索引(index), 字段: [ field_spec ])
  else
    vector_search_index(字段: [ field_spec ])
  end
end

# clean_localized_field_names (name) ⇒字段

从给定字段名称中删除 _translations。仅当尚不存在具有相同名称的字段名称或关系(即带有 _translations 后缀)时,才会执行此操作。对现有字段的检查是以递归方式完成的

参数:

  • 名称 ( string | Symbol )

    要清理的字段的名称。

返回:

  • (字段)

    不含 _translations 的字段名称



98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
# File 'lib/mongoid/fields.rb', line 98

def clean_localized_field_names(名称)
  名称 = database_field_name(名称.to_s)

  klass = self
  [].点击 do |res|
    ar = 名称.拆分('  ')
    ar.each_with_index do |fn, i|
      key = fn
      除非 klass.字段.键?(fn) || klass.关系.键?(fn)
        key = if fn.end_with?(TRANSLATIONS_SFX)
                fn.delete_suffix(TRANSLATIONS_SFX)
              else
                fn
              end

      end
      res.推动(key)

      if klass.字段.键?(fn)
        res.推动(ar.删除(i + 1).连接 (JOIN)('  ')) 除非 i == ar.长度 - 1
        中断
      elsif klass.关系.键?(fn)
        klass = klass.关系[key].klass
      end
    end
  end.连接 (JOIN)('  ')
end

# database_field_name (name) ⇒ string

获取所提供字段存储在数据库中的名称。 用于确定字段是否有别名。

参数:

  • 名称 ( string | Symbol )

    要获取的名称。

返回:

  • ( string )

    存储在数据库中的字段名称。



459
460
461
# File 'lib/mongoid/fields.rb', line 459

def database_field_name(名称)
  字段.database_field_name(名称, 关系, aliased_fields, aliased_associations)
end

#extract_id_field (attributes) ⇒对象

此方法是私有 API 的一部分。 您应尽可能避免使用此方法,因为它将来可能会被删除或更改。

根据此类中定义的别名,从指定属性哈希中提取ID字段。

参数:

  • 属性 (哈希)

    要检查的属性。

返回:

  • ( Object )

    ID 值。



81
82
83
84
85
86
87
88
# File 'lib/mongoid/fields.rb', line 81

def extract_id_field(属性)
  id_fields. do |k|
    if v = 属性[k]
      return v
    end
  end
  nil
end

# 字段 (name, options = {}) ⇒ Field

定义文档上可访问的所有字段。对于定义的每个字段,都会将 getter 和 setter 作为实例方法添加到文档中。

例子:

定义一个字段。

field :score, type: Integer, default: 0

参数:

  • 名称 (符号)

    字段的名称。

  • 选项 哈希 (默认为: {}

    要传递给字段的选项。

选项哈希 ( options ):

  • :type 类 | 符号 | string

    字段的类型。

  • :label string

    字段的标签。

  • :default 对象 | 过程

    该字段的默认。

返回:

  • (字段)

    生成的字段



577
578
579
580
581
582
583
584
585
# File 'lib/mongoid/fields.rb', line 577

def 字段(名称, 选项 = {})
  命名 = 名称.to_s
  验证器::.验证(self, 名称, 选项)
  添加 = add_field(命名, 选项)
  descendants. do |子类|
    子类.add_field(命名, 选项)
  end
  添加
end

# id_fieldsArray<Symbol | string >

此方法是私有 API 的一部分。 您应尽可能避免使用此方法,因为它将来可能会被删除或更改。

以字符串和符号形式返回此模型类的ID字段列表。

返回:

  • ( Array<Symbol | string > )

    ID 字段列表。



62
63
64
65
66
67
68
69
70
71
# File 'lib/mongoid/fields.rb', line 62

def id_fields
  IDS.dup.点击 do |id_fields|
    aliased_fields. do |k, v|
      if v == '_id'
        id_fields << k.to_sym
        id_fields << k
      end
    end
  end
end

# replace_field (name, type) =" Serializable "

将字段替换为新类型。

例子:

替换字段。

Model.replace_field("_id", String)

参数:

  • 名称 ( string )

    字段的名称。

  • 类型 ( class )

    新的字段类型。

返回:



596
597
598
599
# File 'lib/mongoid/fields.rb', line 596

def replace_field(名称, 类型)
  remove_defaults(名称)
  add_field(名称, 字段[名称].选项.合并(merge)(类型: 类型))
end

# traverse_association_tree (key) {|The, The,是否| ... } ⇒字段

此方法是私有 API 的一部分。 您应尽可能避免使用此方法,因为它将来可能会被删除或更改。

向下遍历关联树并搜索给定键的字段。

参数:

  • key ( string )

    用于搜索关联树的键。

  • 区块(&B)

    区块。

收益参数:

  • 使用 (符号)

    当前方法。

  • 使用 (符号 | string )

    字段或关系。

  • 是否 ( true | false )

    第二个让出参数是否为字段。

返回:

  • (字段)

    搜索结束时为给定键找到的字段。 如果找到的最后一个是关联或者没有找到给定键的字段,则返回 nil。



627
628
629
# File 'lib/mongoid/fields.rb', line 627

def traverse_association_tree(key, )
  字段.traverse_association_tree(key, 字段, 关系, aliased_associations, )
end

# using_object_id?true | false

确定是否使用BSON::ObjectIds作为 ID 的便捷方法。

例子:

这个类是否使用对象ID?

person.using_object_ids?

返回:

  • ( true | false )

    如果该类使用BSON::ObjectIds 作为ID 。



608
609
610
# File 'lib/mongoid/fields.rb', line 608

def using_object_id?
  字段['_id'].object_id_field?
end

# vector_field(name,dimensions:,similarity:'cosine', 索引:nil) ⇒ 对象

声明一个vector embeddings字段,并一步为其注册相应的Atlas Vector Search索引。

该字段存储为数组。对于高级索引选项(quantization、索引方法、HNSW 调整),请改用显式 field + vector_search_index 组合。

例子:

最小声明。

class Article
  include Mongoid::Document
  vector_field :embedding, dimensions: 1536
end

包含所有选项。

class Article
  include Mongoid::Document
  vector_field :embedding, dimensions: 1536,
                           similarity: 'dotProduct',
                           index: :article_vectors
end

参数:

  • 名称 (符号 | string )

    嵌入字段的名称。

  • 维度 ( Integer )

    向量的 dimensions。必需。

  • 相似度 string (默认: 'cosine'

    要使用的相似度指标:“cosine”(默认)、“euclidean”或“dotProduct”。

  • 索引(index) (Symbol | String | nil) (默认为: nil

    向量搜索索引的名称。如果省略,则索引未命名,Atlas将其调用为“默认”。



490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
# File 'lib/mongoid/fields.rb', line 490

def vector_field(名称, dimensions:, 相似度: 'cosine', index: nil)
  字段(名称, 类型: 阵列)

  field_spec = {
    类型: ' vector ',
    path: 名称.to_s,
    numDimensions: 维度,
    相似度: 相似度
  }

  if 索引(index)
    vector_search_index(索引(index), 字段: [ field_spec ])
  else
    vector_search_index(字段: [ field_spec ])
  end
end