模块:Mongoid::Matcher Private
- 定义于:
- lib/mongoid/matcher.rb ,
lib/mongoid/matcher/eq.rb,
lib/mongoid/matcher/gt.rb,
lib/mongoid/matcher/in.rb,
lib/mongoid/matcher/lt.rb,
lib/mongoid/matcher/ne.rb,
lib/mongoid/matcher/or.rb,
lib/mongoid/matcher/all.rb,
lib/mongoid/matcher/and.rb,
lib/mongoid/matcher/gte.rb,
lib/mongoid/matcher/lte.rb,
lib/mongoid/matcher/mod.rb,
lib/mongoid/matcher/nin.rb,
lib/mongoid/matcher/nor.rb,
lib/mongoid/matcher/not.rb,
lib/mongoid/matcher/bits.rb,
lib/mongoid/matcher/size.rb,
lib/mongoid/matcher/type.rb,
lib/mongoid/matcher/regex.rb,
lib/mongoid/matcher/exists.rb,
lib/mongoid/matcher/eq_impl.rb,
lib/mongoid/matcher/elem_match.rb,
lib/mongoid/matcher/ 表达式.rb,
lib/mongoid/matcher/bits_all_set.rb,
lib/mongoid/matcher/bits_any_set.rb,
lib/mongoid/matcher/bits_all_clear.rb,
lib/mongoid/matcher/bits_any_clear.rb,
lib/mongoid/matcher/field_operator.rb,
lib/mongoid/matcher/field_expression.rb,
lib/mongoid/matcher/eq_impl_with_regexp.rb,
lib/mongoid/matcher/expression_operator.rb,
lib/mongoid/matcher/elem_match_expression.rb
Overview
该模块是私有 API 的一部分。 您应尽可能避免使用此模块,因为它将来可能会被删除或更改。
实用程序模块,其中包含有助于使用MQL查询表达式执行文档内存匹配的方法。
在命名空间下定义
模块: All 、 And 、 Bits 、 BitsAllClear 、 BitsAllSet 、 BitsAnyClear 、 BitsAnySet 、 ElemMatch 、 ElemMatchExpression 、 Eq 、 EqImpl 、 EqImplWithRegexp 、 Exists 、 Expression 、 ExpressionOperator 、 FieldExpression 、 FieldOperator 、 Gt 、 Gte 、 In 、 Lt 、 Lte 、 Mod 、 Ne 、 Nin 、 Nor 、 Not 、 Or 、 Regex 、 Size 、 Type
类方法摘要折叠
-
。 extract_attribute (document, key) ⇒ 对象 |数组
private
提取文档中指定键处的字段值。
-
。 find_exact_key (hash, key) ⇒ string |符号 | nil
private
无关紧要的string或符号键查找,返回精确的键。
类方法详细信息
。 extract_attribute (文档, key) ⇒对象 |数组
此方法是私有 API 的一部分。 您应尽可能避免使用此方法,因为它将来可能会被删除或更改。
提取文档中指定键处的字段值。
文档可以是哈希或模型实例。
该密钥是有效的MongoDB点表示法密钥。 支持以下使用案例:
-
简单字段遍历 ('foo`) — 检索当前文档中的字段“foo”。
-
哈希/嵌入式文档字段遍历 ('foo.bar`) — 检索当前文档中的字段“foo”,然后从“foo”的值中检索字段“bar”。 每个路径段都可以深入到一个嵌入式文档或哈希字段。
-
数组元素检索 ('foo.N`) — 从字段“foo”中检索第 N 个数组元素,该字段必须是数组。 N 必须是非负整数。
-
数组遍历 ('foo.bar`) - 如果“foo”是一个大量字段,并且“foo”的元素是哈希值或嵌入式文档,则返回“foo”大量中每个哈希值中“bar”字段的值的大量。
此方法可以返回某些文档中的单个字段值,也可以返回多个文档中的值的大量。 可以返回该大量,因为指定路径中的字段值是原始值的大量(例如 整数)或因为指定路径中的字段值是文档大量(例如一对多嵌入式关联),在这种情况下,叶值可能是每个单独文档的标量。 如果叶子值是一个大量并且遍历了一对多关联,则返回值将是一个大量的数组。 请注意,单个字段值也可以是大量,这种示例与出于后续大量元素检索等目的而进行的关联遍历没有区别,且行为相同。
46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 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 |
# File 'lib/mongoid/matcher.rb', line 46 module_function def extract_attribute(文档, key) # 匹配器系统最终也会向其发送原子值, # 当尝试匹配更复杂的类型时。 如果除 # 给定文档或哈希,我们将简化逻辑,然后 # 返回一个空大量。 return [] 除非 文档.is_a?(哈希) || 文档.is_a?(文档) # 性能优化;如果键不包含“.”字符, # 它必须引用文档的直接属性。 除非 key.包括?(' 。 ') 哈希 = 文档.respond_to?(:attributes) ? 文档.属性 : 文档 key = find_exact_key(哈希, key) return key ? [ 哈希[key] ] : [] end if 文档.respond_to?(:as_attributes, true) # 如果文档包含哈希字段,as_attributes 将保留这些字段 # 作为哈希实例,不提供无关紧要的访问权限。 # 转换为BSON::Document 以访问权限对哈希字段的无关访问权限。 文档 = 文档.发送(:as_attributes) end Current = [文档] key.to_s.拆分(' 。 ').每 do |字段| new = [] Current.每 do |doc| 案例 doc when 哈希 actual_key = find_exact_key(doc, 字段) if !actual_key.nil? new << doc[actual_key] end when 阵列 if (索引(index) = 字段.to_i).to_s == 字段 if doc.长度 > 索引(index) new << doc[索引(index)] end end doc.每 do |subdoc| if 哈希 === subdoc actual_key = find_exact_key(subdoc, 字段) if !actual_key.nil? new << subdoc[actual_key] end end end end end Current = new 中断 if Current.空? end Current end |
。 find_exact_key (hash, key) ⇒ string |符号 | nil
此方法是私有 API 的一部分。 您应尽可能避免使用此方法,因为它将来可能会被删除或更改。
无关紧要的string或符号键查找,返回精确的键。
108 109 110 111 112 113 114 |
# File 'lib/mongoid/matcher.rb', line 108 module_function def find_exact_key(哈希, key) key_s = key.to_s return key_s if 哈希.键?(key_s) key_sym = key.to_sym 哈希.键?(key_sym) ? key_sym : nil end |