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↑
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 |
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
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}]
See aggregate/2
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
See count/1
See count/2
See count/3
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]
See distinct/2
See distinct/3
Drops the collection
returns :ok
or a string containing the error message
See drop/1
Remove a Specific Index col = Mongo.connect! |> Mongo.db(“AF_VortexShort_2358”) |> 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
See Mongo.Find
for details.
Gets a list of All Indexes
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}, ...]
See group/2
See group/3
See group/4
See group/5
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.
See insert/2
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}
See insert_one/2
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
See mr/2
See mr/3
See mr/4
See mr/5
New collection
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
Gets read default options
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
Gets write default options