Parent

Class/Module Index [+]

Quicksearch

Mongoid::Contextual::Mongo

Attributes

collection[R]

@attribute [r] collection The collection to query against. @attribute [r] criteria The criteria for the context. @attribute [r] klass The klass for the criteria. @attribute [r] query The Moped query.

criteria[R]

@attribute [r] collection The collection to query against. @attribute [r] criteria The criteria for the context. @attribute [r] klass The klass for the criteria. @attribute [r] query The Moped query.

klass[R]

@attribute [r] collection The collection to query against. @attribute [r] criteria The criteria for the context. @attribute [r] klass The klass for the criteria. @attribute [r] query The Moped query.

query[R]

@attribute [r] collection The collection to query against. @attribute [r] criteria The criteria for the context. @attribute [r] klass The klass for the criteria. @attribute [r] query The Moped query.

Public Class Methods

new(criteria) click to toggle source

Create the new Mongo context. This delegates operations to the underlying driver - in Mongoid's case Moped.

@example Create the new context.

Mongo.new(criteria)

@param [ Criteria ] criteria The criteria.

@since 3.0.0

# File lib/mongoid/contextual/mongo.rb, line 213
def initialize(criteria)
  @criteria, @klass, @cache = criteria, criteria.klass, criteria.options[:cache]
  @collection = klass.collection
  criteria.send(:merge_type_selection)
  @query = collection.find(criteria.selector)
  apply_options
end

Public Instance Methods

blank?() click to toggle source

Is the enumerable of matching documents empty?

@example Is the context empty?

context.blank?

@return [ true, false ] If the context is empty.

@since 3.0.0

# File lib/mongoid/contextual/mongo.rb, line 30
def blank?
  !exists?
end
Also aliased as: empty?
cached?() click to toggle source

Is the context cached?

@example Is the context cached?

context.cached?

@return [ true, false ] If the context is cached.

@since 3.0.0

# File lib/mongoid/contextual/mongo.rb, line 43
def cached?
  !!@cache
end
count(document = nil, &block) click to toggle source

Get the number of documents matching the query.

@example Get the number of matching documents.

context.count

@example Get the count of documents matching the provided.

context.count(document)

@example Get the count for where the provided block is true.

context.count do |doc|
  doc.likes > 1
end

@param [ Document ] document A document ot match.

@return [ Integer ] The number of matches.

@since 3.0.0

# File lib/mongoid/contextual/mongo.rb, line 65
def count(document = nil, &block)
  return super(&block) if block_given?
  return (cached? ? @count ||= query.count : query.count) unless document
  collection.find(criteria.and(_id: document.id).selector).count
end
delete() click to toggle source

Delete all documents in the database that match the selector.

@example Delete all the documents.

context.delete

@return [ nil ] Nil.

@since 3.0.0

# File lib/mongoid/contextual/mongo.rb, line 79
def delete
  self.count.tap do
    query.remove_all
  end
end
Also aliased as: delete_all
delete_all() click to toggle source
Alias for: delete
destroy() click to toggle source

Destroy all documents in the database that match the selector.

@example Destroy all the documents.

context.destroy

@return [ nil ] Nil.

@since 3.0.0

# File lib/mongoid/contextual/mongo.rb, line 94
def destroy
  destroyed = self.count
  each do |doc|
    doc.destroy
  end
  destroyed
end
Also aliased as: destroy_all
destroy_all() click to toggle source
Alias for: destroy
distinct(field) click to toggle source

Get the distinct values in the db for the provided field.

@example Get the distinct values.

context.distinct(:name)

@param [ String, Symbol ] field The name of the field.

@return [ Array<Object> ] The distinct values for the field.

@since 3.0.0

# File lib/mongoid/contextual/mongo.rb, line 113
def distinct(field)
  query.distinct(klass.database_field_name(field))
end
each(&block) click to toggle source

Iterate over the context. If provided a block, yield to a Mongoid document for each, otherwise return an enum.

@example Iterate over the context.

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

@return [ Enumerator ] The enumerator.

@since 3.0.0

# File lib/mongoid/contextual/mongo.rb, line 128
def each(&block)
  if block_given?
    selecting do
      documents_for_iteration.each do |doc|
        yield_document(doc, &block)
      end
      @cache_loaded = true
      eager_loadable? ? docs : self
    end
  else
    to_enum
  end
end
empty?() click to toggle source
Alias for: blank?
exists?() click to toggle source

Do any documents exist for the context.

@example Do any documents exist for the context.

context.exists?

@return [ true, false ] If the count is more than zero.

@since 3.0.0

# File lib/mongoid/contextual/mongo.rb, line 150
def exists?
  # Don't use count here since Mongo does not use counted b-tree indexes
  !query.dup.select(_id: 1).limit(1).entries.first.nil?
end
explain() click to toggle source

Run an explain on the criteria.

@example Explain the criteria.

Band.where(name: "Depeche Mode").explain

@return [ Hash ] The explain result.

@since 3.0.0

# File lib/mongoid/contextual/mongo.rb, line 163
def explain
  query.explain
end
find_and_modify(update, options = {}) click to toggle source

Execute the find and modify command, used for MongoDB's $findAndModify.

@example Execute the command.

context.find_and_modify({ "$inc" => { likes: 1 }}, new: true)

@param [ Hash ] update The updates. @param [ Hash ] options The command options.

@option options [ true, false ] :new Return the updated document. @option options [ true, false ] :remove Delete the first document. @option options [ true, false ] :upsert Create the document if it doesn't exist.

@return [ Document ] The result of the command.

@since 3.0.0

# File lib/mongoid/contextual/mongo.rb, line 183
def find_and_modify(update, options = {})
  if doc = FindAndModify.new(collection, criteria, update, options).result
    Factory.from_db(klass, doc)
  end
end
first() click to toggle source

Get the first document in the database for the criteria's selector.

@example Get the first document.

context.first

@return [ Document ] The first document.

@since 3.0.0

# File lib/mongoid/contextual/mongo.rb, line 197
def first
  apply_sorting
  apply_id_sorting
  with_eager_loading(query.first)
end
Also aliased as: one
last() click to toggle source

Get the last document in the database for the criteria's selector.

@example Get the last document.

context.last

@return [ Document ] The last document.

@since 3.0.0

# File lib/mongoid/contextual/mongo.rb, line 231
def last
  apply_inverse_sorting
  with_eager_loading(query.first)
end
length() click to toggle source

Get's the number of documents matching the query selector.

@example Get the length.

context.length

@return [ Integer ] The number of documents.

@since 3.0.0

# File lib/mongoid/contextual/mongo.rb, line 244
def length
  @length ||= self.count
end
Also aliased as: size
limit(value) click to toggle source

Limits the number of documents that are returned from the database.

@example Limit the documents.

context.limit(20)

@param [ Integer ] value The number of documents to return.

@return [ Mongo ] The context.

@since 3.0.0

# File lib/mongoid/contextual/mongo.rb, line 259
def limit(value)
  query.limit(value) and self
end
map_reduce(map, reduce) click to toggle source

Initiate a map/reduce operation from the context.

@example Initiate a map/reduce.

context.map_reduce(map, reduce)

@param [ String ] map The map js function. @param [ String ] reduce The reduce js function.

@return [ MapReduce ] The map/reduce lazy wrapper.

@since 3.0.0

# File lib/mongoid/contextual/mongo.rb, line 274
def map_reduce(map, reduce)
  MapReduce.new(collection, criteria, map, reduce)
end
one() click to toggle source
Alias for: first
size() click to toggle source
Alias for: length
skip(value) click to toggle source

Skips the provided number of documents.

@example Skip the documents.

context.skip(20)

@param [ Integer ] value The number of documents to skip.

@return [ Mongo ] The context.

@since 3.0.0

# File lib/mongoid/contextual/mongo.rb, line 288
def skip(value)
  query.skip(value) and self
end
sort(values = nil, &block) click to toggle source

Sorts the documents by the provided spec.

@example Sort the documents.

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

@param [ Hash ] values The sorting values as field/direction(1/-1)

pairs.

@return [ Mongo ] The context.

@since 3.0.0

# File lib/mongoid/contextual/mongo.rb, line 303
def sort(values = nil, &block)
  if block_given?
    super(&block)
  else
    query.sort(values) and self
  end
end
update(attributes = nil) click to toggle source

Update the first matching document atomically.

@example Update the first matching document.

context.update({ "$set" => { name: "Smiths" }})

@param [ Hash ] attributes The new attributes for each document.

@return [ nil, false ] False if no attributes were provided.

@since 3.0.0

# File lib/mongoid/contextual/mongo.rb, line 321
def update(attributes = nil)
  update_documents(attributes)
end
update_all(attributes = nil) click to toggle source

Update all the matching documents atomically.

@example Update all the matching documents.

context.update({ "$set" => { name: "Smiths" }})

@param [ Hash ] attributes The new attributes for each document.

@return [ nil, false ] False if no attributes were provided.

@since 3.0.0

# File lib/mongoid/contextual/mongo.rb, line 335
def update_all(attributes = nil)
  update_documents(attributes, :update_all)
end

[Validate]

Generated with the Darkfish Rdoc Generator 2.