A MongoDB driver in Elixir.
Document are exchanged using Maps
.
Example preparing access to the anycoll
collection in the test
db :
# Connect the mongo server (by default port 27017 at 127.0.0.1)
mongo = Mongo.connect!
# Select the db to access
db = mongo.db("test")
# Select the db to access
anycoll = db.collection("anycoll")
Examples accessing the anycoll
collection via CRUD operations :
# Return the list of all docs in the collection (list of Maps)
anycoll.find.toArray
anycoll.find.stream |> Enum.to_list # Same as above
anycoll.find.skip(1).toArray # Same as above but skip first doc
# Insert a list of two docs into the collection
[%{a: 23}, %{a: 24, b: 1}] |> anycoll.insert
# Updates the doc matching "a" == 456 with new values
anycoll.update(%{a: 456}, %{a: 123, b: 789})
# Delete the document matching "b" == 789
anycoll.delete(%{b: 789})
Example of aggregate operation applied to the anycoll
collection :
# Return docs with "value" > 0
anycoll.count(%{value: %{'$gt': 0}})
# Return distinct "value" for docs with "value" > 0
anycoll.distinct("value", %{value: %{"$gt": 1}})
# Apply a map-reduce to the collection specifying the map function then the apply function
anycoll.mr("function(d){emit(this._id, this.value*2)}", "function(k, vs){return Array.sum(vs)}")
# Groups documents in a collection by the specified keys
anycoll.group(a: true)
# Aggregate operation
anycoll.aggregate([
%{'$skip': 1}, # skip the first doc
%{'$limit': 5}, # take five
%{'$project': %{'_id': false, value: true}} # project : select only "_id" and "value"
])
# Drop a collection
db.collection("anycoll").drop
# Authenticate against the db
db.auth("testuser", "123")`
# Retrieve the last error
db.getLastError
- MongoDB needs a Bson encoder/decoder, this project uses the elixir-bson encoder/decoder. See elixir-bson source repo and its documentation