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)
iex> collection = Mongo.connect! |> Mongo.db("test") |> Mongo.Db.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}]
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
Creates an index for the collection
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
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]
Drops the collection
returns :ok
or a string containing the error message
Creates a %Mongo.Find{}
for a given collection, query and projection
See Mongo.Find
for details.
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}, ...]
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 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}
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
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
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