Class: Mongoid::Collection

Inherits:
Object show all
Defined in:
lib/mongoid/collection.rb

Overview

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(klass, name) ⇒ Collection

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

Examples:

Create the new collection.

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

Parameters:

  • klass (Class)

    The class the collection is for.

  • name (String)

    The name of the collection.



61
62
63
# File 'lib/mongoid/collection.rb', line 61

def initialize(klass, name)
  @klass, @name = klass, name
end

Instance Attribute Details

#counterObject (readonly)

Returns the value of attribute counter.



11
12
13
# File 'lib/mongoid/collection.rb', line 11

def counter
  @counter
end

#nameObject (readonly)

Returns the value of attribute name.



11
12
13
# File 'lib/mongoid/collection.rb', line 11

def name
  @name
end

Instance Method Details

#find(selector = {}, options = {}) ⇒ Cursor

Find documents from the database given a selector and options.

Examples:

Find documents in the collection.

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

Parameters:

  • selector (Hash) (defaults to: {})

    The query selector.

  • options (Hash) (defaults to: {})

    The options to pass to the db.

Returns:



31
32
33
34
35
36
37
38
# File 'lib/mongoid/collection.rb', line 31

def find(selector = {}, options = {})
  cursor = Mongoid::Cursor.new(@klass, self, master(options).find(selector, options))
  if block_given?
    yield cursor; cursor.close
  else
    cursor
  end
end

#find_one(selector = {}, options = {}) ⇒ Document?

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

Examples:

Find one document.

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

Parameters:

  • selector (Hash) (defaults to: {})

    The query selector.

  • options (Hash) (defaults to: {})

    The options to pass to the db.

Returns:

  • (Document, nil)

    A matching document or nil if none found.



49
50
51
# File 'lib/mongoid/collection.rb', line 49

def find_one(selector = {}, options = {})
  master(options).find_one(selector, options)
end

#insert(documents, options = {}) ⇒ Object

Inserts one or more documents in the collection.

Examples:

Insert documents.

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

Parameters:

  • documents (Hash, Array<Hash>)

    A single document or multiples.

  • options (Hash) (defaults to: {})

    The options.

Since:

  • 2.0.2, batch-relational-insert



77
78
79
80
81
82
83
84
# File 'lib/mongoid/collection.rb', line 77

def insert(documents, options = {})
  inserter = Thread.current[:mongoid_batch_insert]
  if inserter
    inserter.consume(documents, options)
  else
    master(options).insert(documents, options)
  end
end

#map_reduce(map, reduce, options = {}) ⇒ Cursor Also known as: mapreduce

Perform a map/reduce on the documents.

Examples:

Perform the map/reduce.

collection.map_reduce(map, reduce)

Parameters:

  • map (String)

    The map javascript function.

  • reduce (String)

    The reduce javascript function.

  • options (Hash) (defaults to: {})

    The options to pass to the db.

Returns:



96
97
98
# File 'lib/mongoid/collection.rb', line 96

def map_reduce(map, reduce, options = {})
  master(options).map_reduce(map, reduce, options)
end

#master(options = {}) ⇒ Master

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

Examples:

Get the master connection.

collection.master

Returns:

  • (Master)

    The master connection.



108
109
110
111
112
# File 'lib/mongoid/collection.rb', line 108

def master(options = {})
  options.delete(:cache)
  db = Mongoid.databases[@klass.database] || Mongoid.master
  @master ||= Collections::Master.new(db, @name)
end

#update(selector, document, options = {}) ⇒ Object

Updates one or more documents in the collection.

Examples:

Update documents.

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

Parameters:

  • selector (Hash)

    The document selector.

  • document (Hash)

    The modifier.

  • options (Hash) (defaults to: {})

    The options.

Since:

  • 2.0.0



128
129
130
131
132
133
134
135
# File 'lib/mongoid/collection.rb', line 128

def update(selector, document, options = {})
  updater = Thread.current[:mongoid_atomic_update]
  if updater
    updater.consume(selector, document, options)
  else
    master(options).update(selector, document, options)
  end
end