类:Mongoid::Contextual::Memory

继承:
对象
  • 对象
显示全部
包括:
Enumerable、 Association::EagerLoadableAggregable::MemoryQueryablePositional
定义于:
lib/mongoid/contextual/memory.rb

Overview

用于对已加载到应用程序内存中的文档执行批量查询和持久化操作的 Context对象。 该类的方法接口与 Mongoid::Contextual::Mongo一致。

实例属性摘要折叠

Queryable中包含的属性

#collection#collection 要查询的集合。#criteria#criteria 上下文的条件。#klass#klass 条件的 klass。

实例方法摘要折叠

位置包含的方法

#positionally

Queryable中包含的方法

#blank?

Association::EagerLoadable 中包含的方法

#create_pipeline#eager_load#eager_load_with_lookup#eager_loadable?#preload#preload_for_lookup#switch_local_and_foreign_fields?

Aggregable::Memory中包含的方法

#aggregates#avg#max#min#sum

构造函数详情

#initialize (criteria) ⇒内存

在内存上下文中创建新内容。

例子:

创建新上下文。

Memory.new(criteria)

参数:



175
176
177
178
179
180
181
182
183
184
# File 'lib/mongoid/contextual/memory.rb', line 175

def 初始化(条件)
  @criteria, @klass = 条件, 条件.klass
  @documents = 条件.文档.SELECT do |doc|
    @root ||= doc._root
    @collection ||= .集合
    doc._matches?(条件.选择器)
  end
  apply_sorting
  apply_options
end

实例属性详细信息

# 个documents对象(只读)

返回属性文档的值。



23
24
25
# File 'lib/mongoid/contextual/memory.rb', line 23

def 文档
  @documents
end

#匹配与选择器匹配的内存文档。 (The) ⇒对象(只读)



23
# File 'lib/mongoid/contextual/memory.rb', line 23

attr_reader :documents, :path, :root, :selector

#路径对象(只读)

返回属性路径的值。



23
24
25
# File 'lib/mongoid/contextual/memory.rb', line 23

def 路径
  @path
end

# path 原子路径。 (Theatomicpath。) ⇒对象(只读)



23
# File 'lib/mongoid/contextual/memory.rb', line 23

attr_reader :documents, :path, :root, :selector

# root对象(只读)

返回属性根的值。



23
24
25
# File 'lib/mongoid/contextual/memory.rb', line 23

def 
  @root
end

# root 根文档。 (根文档。) ⇒对象(只读)



23
# File 'lib/mongoid/contextual/memory.rb', line 23

attr_reader :documents, :path, :root, :selector

#选择器对象(只读)

返回属性选择器的值。



23
24
25
# File 'lib/mongoid/contextual/memory.rb', line 23

def 选择器
  @selector
end

#选择器 根文档选择器。 (根文档选择器。)⇒对象(只读)



23
# File 'lib/mongoid/contextual/memory.rb', line 23

attr_reader :documents, :path, :root, :selector

实例方法详细信息

# == (other) ⇒ true | false

检查上下文是否等于其他对象。

例子:

检查相等性。

context == []

参数:

  • 其他 ( Array )

    另一个大量。

返回:

  • ( true | false )

    如果对象相等。



33
34
35
36
37
# File 'lib/mongoid/contextual/memory.rb', line 33

def ==(其他)
  return false 除非 其他.respond_to?(:entries)

  条目 == 其他.条目
end

# deletenil也称为: delete_all

删除数据库中与选择器匹配的所有文档。

例子:

删除所有文档。

context.delete

返回:

  • ( nil )

    Nil.



45
46
47
48
49
50
51
52
53
54
55
56
57
58
# File 'lib/mongoid/contextual/memory.rb', line 45

def 删除
  Deleted = 数数
  已删除 = map do |doc|
    prepare_remove(doc)
    doc.发送(:as_attributes)
  end
  除非 已删除.空?
    集合.find(选择器).update_one(
      按位置(选择器, '$pullAll' => { 路径 => 已删除 }),
      会话: _session
    )
  end
  Deleted
end

#destroynil也称为: destroy_all

销毁数据库中与选择器匹配的所有文档。

例子:

销毁所有文档。

context.destroy

返回:

  • ( nil )

    Nil.



67
68
69
70
71
72
73
74
# File 'lib/mongoid/contextual/memory.rb', line 67

def 销毁
  Deleted = 数数
   do |doc|
    文档.delete_one(doc)
    doc.销毁
  end
  Deleted
end

# distinct (field) ⇒ Array<Object>

获取数据库中所提供字段的非重复值。

例子:

获取不同的值。

context.distinct(:name)

参数:

  • 字段 ( string | Symbol )

    字段的名称。

返回:

  • ( Array<Object> )

    字段的非重复值。



85
86
87
# File 'lib/mongoid/contextual/memory.rb', line 85

def distinct(字段)
  采摘(字段).uniq
end

#each (&block) ⇒枚举器

遍历上下文。 如果提供了区块,则为每个区块让出一个 Mongoid文档,否则返回一个枚举。

例子:

遍历上下文。

context.each do |doc|
  puts doc.name
end

返回:

  • (枚举器)

    枚举器。



98
99
100
101
102
103
104
105
# File 'lib/mongoid/contextual/memory.rb', line 98

def ()
  if block_given?
    documents_for_iteration.()
    self
  else
    to_enum
  end
end

#是否存在? (id_or_conditions = :none) ⇒ true | false

该上下文是否存在任何文档。

例子:

该上下文是否存在任何文档。

context.exists?

给定 _id 是否存在任何文档。

context.exists?(BSON::ObjectId(...))

在给定条件下是否存在任何文档。

context.exists?(name: "...")

参数:

  • id_or_conditions 哈希 | 对象 | false (默认为: :none

    要搜索的_id 、条件哈希、nil 或 false。

返回:

  • ( true | false )

    如果计数大于零。 如果传递 nil 或 false,则始终为 false。



123
124
125
126
127
128
129
130
# File 'lib/mongoid/contextual/memory.rb', line 123

def 存在吗?(id_or_conditions = : none)
  案例 id_or_conditions
  when : none then 任何?
  when nil, false then false
  when 哈希 then 内存.new(条件.WHERE(id_or_conditions)).存在吗?
  else 内存.new(条件.WHERE(_id: id_or_conditions)).存在吗?
  end
end

#第五个文档

获取数据库中条件选择器的第五个 document。

例子:

获取第五个文档。

context.fifth

返回:

  • (文档)

    第五个文档。



456
457
458
# File 'lib/mongoid/contextual/memory.rb', line 456

def 第五
  ager_load([ 文档.第五 ]).first
end

#第五!文档

获取数据库中条件选择器的第五个document,如果未找到,则引发错误。

例子:

获取第五个文档。

context.fifth!

返回:

  • (文档)

    第五个文档。

引发:



470
471
472
# File 'lib/mongoid/contextual/memory.rb', line 470

def 第五!
  第五 || Raise_document_not_Found_error
end

# first (limit = nil) ⇒文档也称为: onefind_first

Get数据库中符合条件的选择器的第一个document。

例子:

获取第一个文档。

context.first

参数:

  • limit 整数 (默认为: nil

    要返回的文档数量。

返回:

  • (文档)

    第一个文档。



140
141
142
143
144
145
146
147
148
149
150
151
# File 'lib/mongoid/contextual/memory.rb', line 140

def first(limit = nil)
  use_first = limit.nil?
  limit ||= 1
  if 条件.use_lookup?
    @criteria = 条件.limit(limit)
    结果 = Eager_load_with_lookup
  else
    结果 = ager_load(文档.first(limit))
  end

  use_first ? 结果.first : 结果
end

# first!文档

获取数据库中条件选择器的第一个 document,如果未找到任何 document,则引发错误。

例子:

获取第一个文档。

context.first!

返回:

  • (文档)

    第一个文档。

引发:



165
166
167
# File 'lib/mongoid/contextual/memory.rb', line 165

def 首先!
  first || Raise_document_not_Found_error
end

#fourth文档

获取数据库中条件选择器的第四个 document。

例子:

获取第四个文档。

context.fourth

返回:

  • (文档)

    第四个文档。



432
433
434
# File 'lib/mongoid/contextual/memory.rb', line 432

def 第四
  ager_load([ 文档.第四 ]).first
end

#fourth!文档

获取数据库中条件选择器的第四个document,如果未找到,则引发错误。

例子:

获取第四个文档。

context.fourth!

返回:

  • (文档)

    第四个文档。

引发:



446
447
448
# File 'lib/mongoid/contextual/memory.rb', line 446

def 第四!
  第四 || Raise_document_not_Found_error
end

# inc (incs) ⇒枚举

递增所有文档的值。

例子:

执行增量。

context.inc(likes: 10)

参数:

  • incs (哈希)

    操作。

返回:

  • (枚举器)

    枚举器。



194
195
196
197
198
# File 'lib/mongoid/contextual/memory.rb', line 194

def inc(incs)
   do |文档|
    文档.inc(incs)
  end
end

#last(limit = nil) ⇒ Document

获取数据库中符合条件的选择器的最后一个文档。

例子:

获取最后一个文档。

context.last

参数:

  • limit 整数 (默认为: nil

    要返回的文档数量。

返回:

  • (文档)

    最后一个文档。



208
209
210
211
212
213
214
# File 'lib/mongoid/contextual/memory.rb', line 208

def last(limit = nil)
  if limit
    ager_load(文档.last(limit))
  else
    ager_load([ 文档.last ]).first
  end
end

# last!文档

获取数据库中条件选择器的最后一个 document,如果未找到任何 document,则引发错误。

例子:

获取最后一个文档。

context.last!

返回:

  • (文档)

    最后一个文档。

引发:



226
227
228
# File 'lib/mongoid/contextual/memory.rb', line 226

def last!
  last || Raise_document_not_Found_error
end

# lengthInteger也称为:大小

获取上下文中匹配文档的长度。

例子:

获取匹配文档的长度。

context.length

返回:

  • ( Integer )

    匹配长度。



236
237
238
# File 'lib/mongoid/contextual/memory.rb', line 236

def 长度
  文档.长度
end

# limit (value) ⇒内存

限制返回的文档数量。

例子:

限制文档。

context.limit(20)

参数:

  • ( Integer )

    要返回的文档数量。

返回:



249
250
251
252
# File 'lib/mongoid/contextual/memory.rb', line 249

def limit()
  self.限制 = 
  self
end

# pick (*fields) ⇒对象 | Array<Object>

选择内存中的字段值。

例子:

获取内存中的值。

context.pick(:name)

参数:

  • *fields ( [ string | Symbol ]... )

    要选择的字段。

返回:

  • ( Object | Array<Object> )

    选取的值。



276
277
278
279
280
# File 'lib/mongoid/contextual/memory.rb', line 276

def pick(*字段)
  return 除非 doc = 文档.first

  puck_from_doc(doc, *字段)
end

# pluck (*fields) ⇒ Array<Object> | Array<Array<Object>>

提取内存中的字段值。

例子:

获取内存中的值。

context.pluck(:name)

参数:

  • *fields ( [ string | Symbol ]... )

    要提取的字段。

返回:

  • ( Array<Object> | Array<Array<Object>> )

    提取的值。



262
263
264
265
266
# File 'lib/mongoid/contextual/memory.rb', line 262

def 采摘(*字段)
  文档.map do |doc|
    puck_from_doc(doc, *字段)
  end
end

#文档

获取数据库中条件选择器的第二个document。

例子:

获取第二个文档。

context.second

返回:

  • (文档)

    第二个文档。



384
385
386
# File 'lib/mongoid/contextual/memory.rb', line 384

def 
  ager_load([ 文档. ]).first
end

#秒!文档

获取数据库中条件选择器的第二个document,如果未找到,则引发错误。

例子:

获取第二个文档。

context.second!

返回:

  • (文档)

    第二个文档。

引发:



398
399
400
# File 'lib/mongoid/contextual/memory.rb', line 398

def 第二!
   || Raise_document_not_Found_error
end

# Second_to_last文档

获取数据库中条件选择器的倒数第二个 document。

例子:

获取倒数第二个文档。

context.second_to_last

返回:

  • (文档)

    倒数第二个文档。



480
481
482
# File 'lib/mongoid/contextual/memory.rb', line 480

def Second_to_last
  ager_load([ 文档.Second_to_last ]).first
end

# Second_to_last!文档

获取数据库中条件选择器的倒数第二个 document,如果未找到任何 document,则引发错误。

例子:

获取倒数第二个文档。

context.second_to_last!

返回:

  • (文档)

    倒数第二个文档。

引发:



494
495
496
# File 'lib/mongoid/contextual/memory.rb', line 494

def Second_to_last!
  Second_to_last || Raise_document_not_Found_error
end

# skip (value) ⇒内存

跳过所提供数量的文档。

例子:

跳过文档。

context.skip(20)

参数:

  • ( Integer )

    要跳过的文档数量。

返回:



336
337
338
339
# File 'lib/mongoid/contextual/memory.rb', line 336

def 跳过()
  self.skipping = 
  self
end

#sort(values) ⇒ Memory

按提供的规范对文档进行排序。

例子:

对文档进行排序。

context.sort(name: -1, title: 1)

参数:

  • values (哈希)

    以字段/方向 ( 1 /- 1 ) 对形式对值进行排序。

返回:



350
351
352
# File 'lib/mongoid/contextual/memory.rb', line 350

def sort(values)
  in_place_sort(values)  self
end

#take(limit = nil) ⇒ Document

从数据库中获取给定数量的文档。

例子:

获取文档。

context.take

参数:

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

    要获取的文档数量或为零。

返回:



306
307
308
309
310
311
312
# File 'lib/mongoid/contextual/memory.rb', line 306

def 采取(limit = nil)
  if limit
    ager_load(文档.采取(limit))
  else
    ager_load([ 文档.first ]).first
  end
end

#take!Document

从数据库中获取给定数量的文档,如果未找到任何文档,则引发错误。

例子:

获取文档。

context.take

返回:

引发:



324
325
326
# File 'lib/mongoid/contextual/memory.rb', line 324

def 采取!
  采取 || Raise_document_not_Found_error
end

#tally (字段)→哈希

统计内存中的字段值。

例子:

获取内存中值的计数。

context.tally(:name)

参数:

  • 字段 ( string | Symbol )

    要统计的字段。

返回:

  • (哈希)

    计数的哈希值。



290
291
292
293
294
295
296
# File 'lib/mongoid/contextual/memory.rb', line 290

def 计数(字段)
  文档.each_with_object({}) do |d, acc|
    v = retrieve_value_at_path(d, 字段)
    acc[v] ||= 0
    acc[v] += 1
  end
end

# Third文档

在数据库中获取条件选择器的第三个文档。

例子:

获取第三个文档。

context.third

返回:

  • (文档)

    第三个文档。



408
409
410
# File 'lib/mongoid/contextual/memory.rb', line 408

def 第三
  ager_load([ 文档.第三 ]).first
end

# Third!文档

在数据库中获取条件选择器的第三个document,如果未找到,则引发错误。

例子:

获取第三个文档。

context.third!

返回:

  • (文档)

    第三个文档。

引发:



422
423
424
# File 'lib/mongoid/contextual/memory.rb', line 422

def 第三!
  第三 || Raise_document_not_Found_error
end

# Third_to_last文档

获取数据库中倒数第三个document作为条件的选择器。

例子:

获取倒数第三个文档。

context.third_to_last

返回:

  • (文档)

    倒数第三个文档。



504
505
506
# File 'lib/mongoid/contextual/memory.rb', line 504

def Third_to_last
  ager_load([ 文档.Third_to_last ]).first
end

# Third_to_last!文档

获取数据库中倒数第三个document作为条件选择器,如果未找到,则引发错误。

例子:

获取倒数第三个文档。

context.third_to_last!

返回:

  • (文档)

    倒数第三个文档。

引发:



518
519
520
# File 'lib/mongoid/contextual/memory.rb', line 518

def Third_to_last!
  Third_to_last || Raise_document_not_Found_error
end

# update (attributes = nil) ⇒ nil | false

自动更新第一个匹配的文档。

例子:

更新匹配文档。

context.update(name: "Smiths")

参数:

  • 属性 哈希 (默认为: nil

    文档的新属性。

返回:

  • ( nil | false )

    如果未提供属性,则为 False。



362
363
364
# File 'lib/mongoid/contextual/memory.rb', line 362

def update(属性 = nil)
  update_documents(属性, [ first ])
end

# update_all (attributes = nil) ⇒ nil | false

自动更新所有匹配的文档。

例子:

更新所有匹配的文档。

context.update_all(name: "Smiths")

参数:

  • 属性 哈希 (默认为: nil

    每个文档的新属性。

返回:

  • ( nil | false )

    如果未提供属性,则为 False。



374
375
376
# File 'lib/mongoid/contextual/memory.rb', line 374

def update_all(属性 = nil)
  update_documents(属性, 条目)
end