类: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 中包含的方法

#eager_load#eager_loadable? , #preload

Aggregable::Memory中包含的方法

#aggregates#avg#max#min#sum

构造函数详情

#initialize (criteria) ⇒内存

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

例子:

创建新上下文。

Memory.new(criteria)

参数:



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

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

实例属性详细信息

# 个documents对象(只读)

返回属性文档的值。



25
26
27
# File 'lib/mongoid/contextual/memory.rb', line 25

def 文档
  @documents
end

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



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

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

#路径对象(只读)

返回属性路径的值。



25
26
27
# File 'lib/mongoid/contextual/memory.rb', line 25

def 路径
  @path
end

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



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

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

# root对象(只读)

返回属性根的值。



25
26
27
# File 'lib/mongoid/contextual/memory.rb', line 25

def 
  @root
end

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



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

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

#选择器对象(只读)

返回属性选择器的值。



25
26
27
# File 'lib/mongoid/contextual/memory.rb', line 25

def 选择器
  @selector
end

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



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

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

实例方法详细信息

# == (other) ⇒ true | false

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

例子:

检查相等性。

context == []

参数:

  • 其他 ( Array )

    另一个大量。

返回:

  • ( true | false )

    如果对象相等。



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

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

# deletenil也称为: delete_all

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

例子:

删除所有文档。

context.delete

返回:

  • ( nil )

    Nil.



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

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.



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

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

# distinct (field) ⇒ Array<Object>

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

例子:

获取不同的值。

context.distinct(:name)

参数:

  • 字段 ( string | Symbol )

    字段的名称。

返回:

  • ( Array<Object> )

    字段的非重复值。



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

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

#each枚举器

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

例子:

遍历上下文。

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

返回:

  • (枚举器)

    枚举器。



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

def 
  if block_given?
    documents_for_iteration. do |doc|
      产量(doc)
    end
    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。



126
127
128
129
130
131
132
133
# File 'lib/mongoid/contextual/memory.rb', line 126

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

#第五个文档

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

例子:

获取第五个文档。

context.fifth

返回:

  • (文档)

    第五个文档。



454
455
456
# File 'lib/mongoid/contextual/memory.rb', line 454

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

#第五!文档

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

例子:

获取第五个文档。

context.fifth!

返回:

  • (文档)

    第五个文档。

引发:



468
469
470
# File 'lib/mongoid/contextual/memory.rb', line 468

def 第五!
  第五 || Raise_document_not_Found_error
end

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

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

例子:

获取第一个文档。

context.first

参数:

  • limit 整数 (默认为: nil

    要返回的文档数量。

返回:

  • (文档)

    第一个文档。



143
144
145
146
147
148
149
# File 'lib/mongoid/contextual/memory.rb', line 143

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

# first!文档

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

例子:

获取第一个文档。

context.first!

返回:

  • (文档)

    第一个文档。

引发:



163
164
165
# File 'lib/mongoid/contextual/memory.rb', line 163

def 首先!
  first || Raise_document_not_Found_error
end

#fourth文档

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

例子:

获取第四个文档。

context.fourth

返回:

  • (文档)

    第四个文档。



430
431
432
# File 'lib/mongoid/contextual/memory.rb', line 430

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

#fourth!文档

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

例子:

获取第四个文档。

context.fourth!

返回:

  • (文档)

    第四个文档。

引发:



444
445
446
# File 'lib/mongoid/contextual/memory.rb', line 444

def 第四!
  第四 || Raise_document_not_Found_error
end

# inc (incs) ⇒枚举

递增所有文档的值。

例子:

执行增量。

context.inc(likes: 10)

参数:

  • incs (哈希)

    操作。

返回:

  • (枚举器)

    枚举器。



192
193
194
195
196
# File 'lib/mongoid/contextual/memory.rb', line 192

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

#last(limit = nil) ⇒ Document

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

例子:

获取最后一个文档。

context.last

参数:

  • limit 整数 (默认为: nil

    要返回的文档数量。

返回:

  • (文档)

    最后一个文档。



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

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

# last!文档

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

例子:

获取最后一个文档。

context.last!

返回:

  • (文档)

    最后一个文档。

引发:



224
225
226
# File 'lib/mongoid/contextual/memory.rb', line 224

def last!
  last || Raise_document_not_Found_error
end

# lengthInteger也称为:大小

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

例子:

获取匹配文档的长度。

context.length

返回:

  • ( Integer )

    匹配长度。



234
235
236
# File 'lib/mongoid/contextual/memory.rb', line 234

def 长度
  文档.长度
end

# limit (value) ⇒内存

限制返回的文档数量。

例子:

限制文档。

context.limit(20)

参数:

  • ( Integer )

    要返回的文档数量。

返回:



247
248
249
250
# File 'lib/mongoid/contextual/memory.rb', line 247

def limit()
  self.限制 = 
  self
end

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

选择内存中的字段值。

例子:

获取内存中的值。

context.pick(:name)

参数:

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

    要选择的字段。

返回:

  • ( Object | Array<Object> )

    选取的值。



274
275
276
277
278
# File 'lib/mongoid/contextual/memory.rb', line 274

def pick(*字段)
  if doc = 文档.first
    puck_from_doc(doc, *字段)
  end
end

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

提取内存中的字段值。

例子:

获取内存中的值。

context.pluck(:name)

参数:

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

    要提取的字段。

返回:

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

    提取的值。



260
261
262
263
264
# File 'lib/mongoid/contextual/memory.rb', line 260

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

#文档

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

例子:

获取第二个文档。

context.second

返回:

  • (文档)

    第二个文档。



382
383
384
# File 'lib/mongoid/contextual/memory.rb', line 382

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

#秒!文档

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

例子:

获取第二个文档。

context.second!

返回:

  • (文档)

    第二个文档。

引发:



396
397
398
# File 'lib/mongoid/contextual/memory.rb', line 396

def 第二!
   || Raise_document_not_Found_error
end

# Second_to_last文档

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

例子:

获取倒数第二个文档。

context.second_to_last

返回:

  • (文档)

    倒数第二个文档。



478
479
480
# File 'lib/mongoid/contextual/memory.rb', line 478

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

# Second_to_last!文档

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

例子:

获取倒数第二个文档。

context.second_to_last!

返回:

  • (文档)

    倒数第二个文档。

引发:



492
493
494
# File 'lib/mongoid/contextual/memory.rb', line 492

def Second_to_last!
  Second_to_last || Raise_document_not_Found_error
end

# skip (value) ⇒内存

跳过所提供数量的文档。

例子:

跳过文档。

context.skip(20)

参数:

  • ( Integer )

    要跳过的文档数量。

返回:



334
335
336
337
# File 'lib/mongoid/contextual/memory.rb', line 334

def 跳过()
  self.skipping = 
  self
end

#sort(values) ⇒ Memory

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

例子:

对文档进行排序。

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

参数:

  • values (哈希)

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

返回:



348
349
350
# File 'lib/mongoid/contextual/memory.rb', line 348

def sort(values)
  in_place_sort(values)  self
end

#take(limit = nil) ⇒ Document

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

例子:

获取文档。

context.take

参数:

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

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

返回:



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

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

#take!Document

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

例子:

获取文档。

context.take

返回:

引发:



322
323
324
# File 'lib/mongoid/contextual/memory.rb', line 322

def 采取!
  采取 || Raise_document_not_Found_error
end

#tally (字段)→哈希

统计内存中的字段值。

例子:

获取内存中值的计数。

context.tally(:name)

参数:

  • 字段 ( string | Symbol )

    要统计的字段。

返回:

  • (哈希)

    计数的哈希值。



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

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

# Third文档

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

例子:

获取第三个文档。

context.third

返回:

  • (文档)

    第三个文档。



406
407
408
# File 'lib/mongoid/contextual/memory.rb', line 406

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

# Third!文档

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

例子:

获取第三个文档。

context.third!

返回:

  • (文档)

    第三个文档。

引发:



420
421
422
# File 'lib/mongoid/contextual/memory.rb', line 420

def 第三!
  第三 || Raise_document_not_Found_error
end

# Third_to_last文档

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

例子:

获取倒数第三个文档。

context.third_to_last

返回:

  • (文档)

    倒数第三个文档。



502
503
504
# File 'lib/mongoid/contextual/memory.rb', line 502

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

# Third_to_last!文档

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

例子:

获取倒数第三个文档。

context.third_to_last!

返回:

  • (文档)

    倒数第三个文档。

引发:



516
517
518
# File 'lib/mongoid/contextual/memory.rb', line 516

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。



360
361
362
# File 'lib/mongoid/contextual/memory.rb', line 360

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

# update_all (attributes = nil) ⇒ nil | false

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

例子:

更新所有匹配的文档。

context.update_all(name: "Smiths")

参数:

  • 属性 哈希 (默认为: nil

    每个文档的新属性。

返回:

  • ( nil | false )

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



372
373
374
# File 'lib/mongoid/contextual/memory.rb', line 372

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