Class Moped::Collection
In: lib/moped/collection.rb
Parent: Object

The class for interacting with a MongoDB collection.

@since 1.0.0

Methods

aggregate   capped?   drop   find   indexes   insert   new   rename   session   where   write_concern  

Included Modules

Readable Retryable

Attributes

database  [R]  @!attribute database
  @return [ Database ] The database for the collection.

@!attribute name

  @return [ String ] The name of the collection.
name  [R]  @!attribute database
  @return [ Database ] The database for the collection.

@!attribute name

  @return [ String ] The name of the collection.

Public Class methods

Initialize the new collection.

@example Initialize the collection.

  Collection.new(database, :artists)

@param [ Database ] database The collection‘s database. @param [ String, Symbol] name The collection name.

@since 1.0.0

Public Instance methods

Call aggregate function over the collection.

@example Execute an aggregation.

  session[:users].aggregate({
    "$group" => {
      "_id" => "$city",
      "totalpop" => { "$sum" => "$pop" }
    }
  })

@param [ Hash, Array<Hash> ] documents representing the aggregate

  function to execute

@return [ Hash ] containing the result of aggregation

@since 1.3.0

Return whether or not this collection is a capped collection.

@example Is the collection capped?

  collection.capped?

@return [ true, false ] If the collection is capped.

@since 1.4.0

Drop the collection.

@example Drop the collection.

  collection.drop

@return [ Hash ] The command information.

@since 1.0.0

Build a query for this collection.

@example Build a query based on the provided selector.

  collection.find(name: "Placebo")

@param [ Hash ] selector The query selector.

@return [ Query ] The generated query.

@since 1.0.0

Access information about this collection‘s indexes.

@example Get the index information.

  collection.indexes

@return [ Indexes ] The index information.

@since 1.0.0

Insert one or more documents into the collection.

@example Insert a single document.

  db[:people].insert(name: "John")

@example Insert multiple documents in batch.

  db[:people].insert([{name: "John"}, {name: "Joe"}])

@param [ Hash, Array<Hash> ] documents The document(s) to insert. @param [ Array ] flags The flags, valid values are :continue_on_error.

@option options [Array] :continue_on_error Whether to continue on error.

@return [ nil ] nil.

@since 1.0.0

Rename the collection

@example Rename the collection to ‘foo‘

  collection.rename('foo')

@return [ Hash ] The command information.

@since 2.0.0

Get the session for the collection.

@example Get the session for the collection.

  collection.session

@return [ Session ] The session for the collection.

@since 2.0.0

where(selector = {})

Alias for find

[Validate]