Class Mongoid::Collection
In: lib/mongoid/collection.rb
Parent: Object

This class is the Mongoid wrapper to the Mongo Ruby driver‘s collection object.

Methods

driver   find   find_one   insert   map_reduce   mapreduce   master   new   update  

Attributes

counter  [R] 
klass  [R] 
name  [R] 

Public Class methods

Initialize a new Mongoid::Collection, setting up the master, slave, and name attributes. Masters will be used for writes, slaves for reads.

@example Create the new collection.

  Collection.new(masters, slaves, "test")

@param [ Class ] klass The class the collection is for. @param [ String ] name The name of the collection. @param [ Hash ] options The collection options.

@option options [ true, false ] :capped If the collection is capped. @option options [ Integer ] :size The capped collection size. @option options [ Integer ] :max The maximum number of docs in the

  capped collection.

Public Instance methods

Get the unwrapped driver collection for this mongoid collection.

@example Get the driver collection.

  collection.driver

@return [ Mongo::Collection ] The driver collection.

@since 2.2.0

Find documents from the database given a selector and options.

@example Find documents in the collection.

  collection.find({ :test => "value" })

@param [ Hash ] selector The query selector. @param [ Hash ] options The options to pass to the db.

@return [ Cursor ] The results.

Find the first document from the database given a selector and options.

@example Find one document.

  collection.find_one({ :test => "value" })

@param [ Hash ] selector The query selector. @param [ Hash ] options The options to pass to the db.

@return [ Document, nil ] A matching document or nil if none found.

Inserts one or more documents in the collection.

@example Insert documents.

  collection.insert(
    { "field" => "value" },
    :safe => true
  )

@param [ Hash, Array<Hash> ] documents A single document or multiples. @param [ Hash ] options The options.

@since 2.0.2, batch-relational-insert

Perform a map/reduce on the documents.

@example Perform the map/reduce.

  collection.map_reduce(map, reduce)

@param [ String ] map The map javascript function. @param [ String ] reduce The reduce javascript function. @param [ Hash ] options The options to pass to the db.

@return [ Cursor ] The results.

mapreduce(map, reduce, options = {})

Alias for map_reduce

Return the object responsible for writes to the database. This will always return a collection associated with the Master DB.

@example Get the master connection.

  collection.master

@return [ Master ] The master connection.

Updates one or more documents in the collection.

@example Update documents.

  collection.update(
    { "_id" => BSON::OjectId.new },
    { "$push" => { "addresses" => { "_id" => "street" } } },
    :safe => true
  )

@param [ Hash ] selector The document selector. @param [ Hash ] document The modifier. @param [ Hash ] options The options.

@since 2.0.0

[Validate]