mongo v0.5.4 Mongo.Collection

Module holding operations that can be performed on a collection (find, count...)

Usage:

iex> Mongo.Helpers.test_collection("anycoll") |> Mongo.Collection.count
{:ok, 6}

count() or count!()

The first returns {:ok, value}, the second returns simply value when the call is sucessful. In case of error, the first returns %Mongo.Error{} the second raises a Mongo.Bang exception.

iex> collection = Mongo.Helpers.test_collection("anycoll")
...> {:ok, 6} = collection |> Mongo.Collection.count
...> 6 === collection |> Mongo.Collection.count!
true

iex> collection = Mongo.Helpers.test_collection("anycoll")
...> {:ok, 2} = collection |> Mongo.Collection.count(a: ['$in': [1,3]])
...> %Mongo.Error{} = collection |> Mongo.Collection.count(a: ['$in': 1]) # $in should take a list, so this triggers an error
...> collection |> Mongo.Collection.count!(a: ['$in': 1])
** (Mongo.Bang) :"cmd error"

Summary

Functions

Calculates aggregate values for the data in the collection (see db.collection.aggregate)

Count documents in the collection

See count/1

Removes an existing document or documents in the collection (see db.collection.remove)

Finds the distinct values for a specified field across a single collection (see db.collection.distinct)

Drops the collection

See drop/1

Remove a Specific Index col = Mongo.connect! |> Mongo.db("AFVortexShort2358") |> Mongo.Db.collection("test.test") col |> Mongo.Collection.dropIndex(%{time: 1})

Remove All Indexes

Creates a %Mongo.Find{} for a given collection, query and projection

Gets a list of All Indexes

Insert a list of documents into the collection

Insert one document into the collection returns the document it received

See insert_one/2

New collection

Adds options to the collection overwriting database options

Gets read default options

Modifies an existing document or documents in the collection

Gets write default options

Functions

aggregate(collection, pipeline)

Calculates aggregate values for the data in the collection (see db.collection.aggregate)

iex> collection = Mongo.Helpers.test_collection("anycoll")
...> collection |> Mongo.Collection.aggregate([
...>    %{'$skip': 1},
...>    %{'$limit': 5},
...>    %{'$project': %{'_id': false, value: true}} ])
[%{value: 1}, %{value: 1}, %{value: 1}, %{value: 1}, %{value: 3}]
aggregate!(pipeline, collection)

See aggregate/2

count(collection, query \\ %{}, skip_limit \\ %{})

Count documents in the collection

If query is not specify, it counts all document collection. skip_limit is a map that specify Mongodb otions skip and limit

count!(collection)

See count/1

count!(collection, query)

See count/2

count!(collection, query, skip_limit)

See count/3

createIndex(collection, name, key, unique \\ false, options \\ %{})

Creates an index for the collection

delete(collection, query, justOne \\ false)

Removes an existing document or documents in the collection (see db.collection.remove)

iex> collection = Mongo.connect! |> Mongo.db("test") |> Mongo.Db.collection("anycoll")
...> collection |> Mongo.Collection.delete(%{b: 789})
:ok
distinct(collection, key, query \\ %{})

Finds the distinct values for a specified field across a single collection (see db.collection.distinct)

iex> collection = Mongo.connect! |> Mongo.db("test") |> Mongo.Db.collection("anycoll")
...> collection |> Mongo.Collection.distinct!("value", %{value: %{"$lt": 3}})
[0, 1]
distinct!(key, collection)

See distinct/2

distinct!(key, query, collection)

See distinct/3

drop(collection)

Drops the collection

returns :ok or a string containing the error message

drop!(collection)

See drop/1

dropIndex(collection, key)

Remove a Specific Index col = Mongo.connect! |> Mongo.db("AFVortexShort2358") |> Mongo.Db.collection("test.test") col |> Mongo.Collection.dropIndex(%{time: 1})

dropIndexes(collection)

Remove All Indexes

find(collection, criteria \\ %{}, projection \\ %{})

Creates a %Mongo.Find{} for a given collection, query and projection

See Mongo.Find for details.

getIndexes(collection)

Gets a list of All Indexes

group(collection, key, reduce \\ "function(k, vs){return Array.sum(vs)}", initial \\ %{}, params \\ %{})

Groups documents in the collection by the specified key

iex> collection = Mongo.connect! |> Mongo.db("test") |> Mongo.Db.collection("anycoll")
...> collection |> Mongo.Collection.group!(%{a: true}) |> is_list
true

[%{a: 0.0}, %{a: 1.0}, %{a: 2.0}, ...]
group!(key, collection)

See group/2

group!(key, reduce, collection)

See group/3

group!(key, reduce, initial, collection)

See group/4

group!(key, reduce, initial, params, collection)

See group/5

insert(docs, collection)

Insert a list of documents into the collection

iex> collection = Mongo.connect! |> Mongo.db("test") |> Mongo.Db.collection("anycoll")
...> [%{a: 23}, %{a: 24, b: 1}] |> Mongo.Collection.insert(collection) |> elem(1)
[%{a: 23}, %{a: 24, b: 1}]

You can chain it with Mongo.assign_id/1 when you need ids for further processing. If you don't Mongodb will assign ids automatically.

iex> collection = Mongo.connect! |> Mongo.db("test") |> Mongo.Db.collection("anycoll")
...> [%{a: 23}, %{a: 24, b: 1}] |> Mongo.assign_id |> Mongo.Collection.insert(collection) |> elem(1) |> Enum.at(0) |> Map.has_key?(:"_id")
true

Mongo.Collection.insert returns the list of documents it received.

insert!(docs, collection)

See insert/2

insert_one(doc, collection)

Insert one document into the collection returns the document it received.

iex> collection = Mongo.connect! |> Mongo.db("test") |> Mongo.Db.collection("anycoll")
...> %{a: 23} |> Mongo.Collection.insert_one(collection) |> elem(1)
%{a: 23}
insert_one!(doc, collection)

See insert_one/2

mr(collection, map, reduce \\ "function(k, vs){return Array.sum(vs)}", out \\ %{inline: true}, params \\ %{})

Provides a wrapper around the mapReduce command

Returns :ok or an array of documents (with option :inline active - set by default).

iex> collection = Mongo.connect! |> Mongo.db("test") |> Mongo.Db.collection("anycoll")
...> Mongo.Collection.mr!(collection, "function(d){emit(this._id, this.value*2)}", "function(k, vs){return Array.sum(vs)}") |> is_list
true

%{_id: Bson.ObjectId.from_string("542aa3fab9742bc0d5eaa12d"), value: 0.0}

iex> collection = Mongo.connect! |> Mongo.db("test") |> Mongo.Db.collection("anycoll")
...> Mongo.Collection.mr!(collection, "function(d){emit('z', 3*this.value)}", "function(k, vs){return Array.sum(vs)}", "mrcoll")
:ok
mr!(map, collection)

See mr/2

mr!(map, reduce, collection)

See mr/3

mr!(map, reduce, out, collection)

See mr/4

mr!(map, reduce, out, more, collection)

See mr/5

new(db, name)

New collection

opts(collection, new_opts)

Adds options to the collection overwriting database options

new_opts must be a map with zero or more pairs represeting one of these options:

  • read: :awaitdata, :nocursortimeout, :slaveok, :tailablecursor
  • write concern: :wc
  • socket: :mode, :timeout
read_opts(collection)

Gets read default options

update(collection, query, update, upsert \\ false, multi \\ false)

Modifies an existing document or documents in the collection

iex> collection = Mongo.connect! |> Mongo.db("test") |> Mongo.Db.collection("anycoll")
...> collection |> Mongo.Collection.update(%{a: 456}, %{a: 123, b: 789})
:ok
write_opts(collection)

Gets write default options