Mongo.Server

Manage the connection to a mongodb server

Source

Summary

active?(mongo)

Returns true if connection mode is active

assign_id(docs, client_prefix \\ gen_client_prefix())

Assigns radom ids to a list of documents when :_id is missing

close(mongo)

Closes the connection

cmd(mongo, cmd)

Executes an admin command to the server

cmd_sync(mongo, command)

Sends a command message requesting imediate response

connect()

connects to local mongodb server by defaults to {“127.0.0.1”, 27017}

connect(opts)

connects to a mongodb server specifying options

connect(host, port)

connects to a mongodb server

db_opts(mongo)

Gets mongo connection default options

opts(mongo, new_opts)

Adds options to an existing mongo server connection

ping(mongo)

Pings the server

response(mongo, decoder \\ :erlang.make_fun(Mongo.Response, :bson_decode, 1))

Retreives a repsonce from the MongoDB server (only for passive mode)

send(mongo, payload, reqid \\ gen_reqid())

Sends a message to MongoDB

Functions

active?(mongo)

Returns true if connection mode is active

Source
assign_id(docs, client_prefix \\ gen_client_prefix())

Assigns radom ids to a list of documents when :_id is missing

iex> [%{a: 1}] |> Mongo.Server.assign_id |> Enum.at(0) |> Map.keys
[:"_id", :a]

#a prefix to ids can be set manually like this
iex> prefix = case [%{a: 1}] |> Mongo.Server.assign_id(256*256-1) |> Enum.at(0) |> Map.get(:"_id") do
...>   %Bson.ObjectId{oid: <<prefix::16, _::binary>>} -> prefix
...>   error -> error
...> end
...> prefix
256*256-1

#by default prefix are set at connection time and remains identical for the entire connection
iex> mongo = Mongo.connect!
...> prefix = case [%{a: 1}] |> Mongo.Server.assign_id(mongo) |> Enum.at(0) |> Map.get(:"_id") do
...>   %Bson.ObjectId{oid: <<prefix::16, _::binary>>} -> prefix
...>   error -> error
...> end
...> prefix == mongo.id_prefix
true
Source
close(mongo)

Closes the connection

Source
cmd(mongo, cmd)

Executes an admin command to the server

iex> mongo = Mongo.connect! # Returns a exception when connection fails iex> case Mongo.connect do …> {:ok, mongo } -> :ok …> error -> error …> end :ok

Source
cmd_sync(mongo, command)

Sends a command message requesting imediate response

Source
connect()

connects to local mongodb server by defaults to {“127.0.0.1”, 27017}

This can be overwritten by the environment variable :host, ie:

[
  {mongo,
    [
      {host, {"127.0.0.1", 27017}}
    ]}
].
Source
connect(opts)

connects to a mongodb server specifying options

Opts must be a Map

Source
connect(host, port)

connects to a mongodb server

Source
db_opts(mongo)

Gets mongo connection default options

Source
opts(mongo, new_opts)

Adds options to an existing mongo server connection

new_opts must be a map with zero or more of the following keys:

  • read: :awaitdata, :nocursortimeout, :slaveok, :tailablecursor
  • write concern: :wc
  • socket: :mode, :timeout
Source
ping(mongo)

Pings the server

iex> Mongo.connect! |> Mongo.Server.ping :ok

Source
response(mongo, decoder \\ :erlang.make_fun(Mongo.Response, :bson_decode, 1))

Retreives a repsonce from the MongoDB server (only for passive mode)

Source
send(mongo, payload, reqid \\ gen_reqid())

Sends a message to MongoDB

Source