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"
Source

Summary

aggregate!(pipeline, collection)

See aggregate/2

aggregate(collection, pipeline)

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

count!(collection)

See count/1

count!(collection, query)

See count/2

count!(collection, query, skip_limit)

See count/3

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

Count documents in the collection

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)

distinct!(key, collection)

See distinct/2

distinct!(key, query, collection)

See distinct/3

distinct(collection, key, query \\ %{})

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

drop!(collection)

See drop/1

drop(collection)

Drops the collection

dropIndex(collection, key)

Remove a Specific Index col = Mongo.connect! |> Mongo.db(“AF_VortexShort_2358”) |> 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

getIndexes(collection)

Gets a list of All Indexes

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

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

Groups documents in the collection by the specified key

insert!(docs, collection)

See insert/2

insert(docs, collection)

Insert a list of documents into the collection

insert_one!(doc, collection)

See insert_one/2

insert_one(doc, collection)

Insert one document into the collection returns the document it received

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

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

Provides a wrapper around the mapReduce command

new(db, name)

New collection

opts(collection, new_opts)

Adds options to the collection overwriting database options

read_opts(collection)

Gets read default options

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

Modifies an existing document or documents in the collection

write_opts(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}]
Source
aggregate!(pipeline, collection)

See aggregate/2

Source
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

Source
count!(collection)

See count/1

Source
count!(collection, query)

See count/2

Source
count!(collection, query, skip_limit)

See count/3

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

Creates an index for the collection

Source
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
Source
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]
Source
distinct!(key, collection)

See distinct/2

Source
distinct!(key, query, collection)

See distinct/3

Source
drop(collection)

Drops the collection

returns :ok or a string containing the error message

Source
drop!(collection)

See drop/1

Source
dropIndex(collection, key)

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

Source
dropIndexes(collection)

Remove All Indexes

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

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

See Mongo.Find for details.

Source
getIndexes(collection)

Gets a list of All Indexes

Source
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}, ...]
Source
group!(key, collection)

See group/2

Source
group!(key, reduce, collection)

See group/3

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

See group/4

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

See group/5

Source
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.

Source
insert!(docs, collection)

See insert/2

Source
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}
Source
insert_one!(doc, collection)

See insert_one/2

Source
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
Source
mr!(map, collection)

See mr/2

Source
mr!(map, reduce, collection)

See mr/3

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

See mr/4

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

See mr/5

Source
new(db, name)

New collection

Source
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
Source
read_opts(collection)

Gets read default options

Source
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
Source
write_opts(collection)

Gets write default options

Source