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, options = {}) ⇒ 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.

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

    The collection options.

Options Hash (options):

  • :capped (true, false)

    If the collection is capped.

  • :size (Integer)

    The capped collection size.

  • :max (Integer)

    The maximum number of docs in the capped collection.



77
78
79
# File 'lib/mongoid/collection.rb', line 77

def initialize(klass, name, options = {})
  @klass, @name, @options = klass, name, options || {}
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

#klassObject (readonly)

Returns the value of attribute klass.



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

def klass
  @klass
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

#driverMongo::Collection

Get the unwrapped driver collection for this mongoid collection.

Examples:

Get the driver collection.

collection.driver

Returns:

  • (Mongo::Collection)

    The driver collection.

Since:

  • 2.2.0



28
29
30
# File 'lib/mongoid/collection.rb', line 28

def driver
  master.collection
end

#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:



41
42
43
44
45
46
47
48
# File 'lib/mongoid/collection.rb', line 41

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.



59
60
61
# File 'lib/mongoid/collection.rb', line 59

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



93
94
95
96
97
98
99
100
# File 'lib/mongoid/collection.rb', line 93

def insert(documents, options = {})
  consumer = Threaded.insert
  if consumer
    consumer.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:



112
113
114
# File 'lib/mongoid/collection.rb', line 112

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.



124
125
126
127
128
# File 'lib/mongoid/collection.rb', line 124

def master(options = {})
  options.delete(:cache)
  db = Mongoid.databases[klass.database] || Mongoid.master
  @master ||= Collections::Master.new(db, @name, @options)
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



144
145
146
147
148
149
150
151
# File 'lib/mongoid/collection.rb', line 144

def update(selector, document, options = {})
  updater = Threaded.update_consumer(klass)
  if updater
    updater.consume(selector, document, options)
  else
    master(options).update(selector, document, options)
  end
end