Mongo.Server
Manage the connection to a mongodb server
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 |
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
Returns true if connection mode is active
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
Closes the connection
Executes an admin command to the server
Sends a command message requesting imediate response
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}}
]}
].
connects to a mongodb server specifying options
Opts must be a Map
connects to a mongodb server
Gets mongo connection default options
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
Pings the server
iex> Mongo.connect! |> Mongo.Server.ping :ok
Retreives a repsonce from the MongoDB server (only for passive mode)
Sends a message to MongoDB