Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Newer
Older
100644 390 lines (305 sloc) 18.821 kb
e7bc02e edoc annotations in src comments
Tony Hannan authored
1 %@doc Top-level client interface to MongoDB
9a01905 mongo.erl is top-level interface
Tony Hannan authored
2 -module (mongo).
3
4 -export_type ([maybe/1]).
5
f59460d mongo:create_index
Tony Hannan authored
6 -export_type ([host/0, connection/0]).
cbd5489 connect with timeout
Tony Hannan authored
7 -export ([connect/1, connect/2, disconnect/1, connect_factory/1, connect_factory/2]).
b802070 is_closed. mongo: replica sets & pools
Tony Hannan authored
8 -export_type ([replset/0, rs_connection/0]).
cbd5489 connect with timeout
Tony Hannan authored
9 -export ([rs_connect/1, rs_connect/2, rs_disconnect/1, rs_connect_factory/1, rs_connect_factory/2]).
f59460d mongo:create_index
Tony Hannan authored
10
11 -export_type ([action/1, db/0, write_mode/0, read_mode/0, failure/0]).
fe00618 auth & add_user
Tony Hannan authored
12 -export ([do/5, this_db/0]).
f59460d mongo:create_index
Tony Hannan authored
13
14 -export_type ([collection/0, selector/0, projector/0, skip/0, batchsize/0, modifier/0]).
9a01905 mongo.erl is top-level interface
Tony Hannan authored
15 -export ([insert/2, insert_all/2]).
16 -export ([save/2, replace/3, repsert/3, modify/3]).
17 -export ([delete/2, delete_one/2]).
18 -export ([find_one/2, find_one/3, find_one/4]).
19 -export ([find/2, find/3, find/4, find/5]).
20 -export ([count/2, count/3]).
f59460d mongo:create_index
Tony Hannan authored
21
22 -export_type ([cursor/0]).
23 -export ([next/1, rest/1, close_cursor/1]).
24
25 -export_type ([command/0]).
9a01905 mongo.erl is top-level interface
Tony Hannan authored
26 -export ([command/1]).
27
fe00618 auth & add_user
Tony Hannan authored
28 -export_type ([username/0, password/0]).
29 -export ([auth/2]).
30
31 -export_type ([permission/0]).
32 -export ([add_user/3]).
33
5a5ff25 create_index with optional fields allowed
Tony Hannan authored
34 -export_type ([index_spec/0, key_order/0]).
35 -export ([create_index/2]).
f59460d mongo:create_index
Tony Hannan authored
36
66853bc copy_database
Tony Hannan authored
37 -export ([copy_database/3, copy_database/5]).
38
9a01905 mongo.erl is top-level interface
Tony Hannan authored
39 -include ("mongo_protocol.hrl").
40
41 -type reason() :: any().
42
b802070 is_closed. mongo: replica sets & pools
Tony Hannan authored
43 % Server %
44
3f71a62 Host can no longer be raw ip address {,,,}. Connection includes host
Tony Hannan authored
45 -type host() :: mongo_connect:host().
46 % Hostname or ip address with or without port. Port defaults to 27017 when missing.
9a01905 mongo.erl is top-level interface
Tony Hannan authored
47 % Eg. "localhost" or {"localhost", 27017}
48 -type connection() :: mongo_connect:connection().
49
50 -spec connect (host()) -> {ok, connection()} | {error, reason()}. % IO
e7bc02e edoc annotations in src comments
Tony Hannan authored
51 %@doc Connect to given MongoDB server
3f71a62 Host can no longer be raw ip address {,,,}. Connection includes host
Tony Hannan authored
52 connect (Host) -> mongo_connect:connect (Host).
9a01905 mongo.erl is top-level interface
Tony Hannan authored
53
cbd5489 connect with timeout
Tony Hannan authored
54 -spec connect (host(), timeout()) -> {ok, connection()} | {error, reason()}. % IO
116b695 Timeout on receive too
Tony Hannan authored
55 %@doc Connect to given MongoDB server. Timeout used for initial connection and every query and safe write.
cbd5489 connect with timeout
Tony Hannan authored
56 connect (Host, TimeoutMS) -> mongo_connect:connect (Host, TimeoutMS).
57
9a01905 mongo.erl is top-level interface
Tony Hannan authored
58 -spec disconnect (connection()) -> ok. % IO
e7bc02e edoc annotations in src comments
Tony Hannan authored
59 %@doc Close connection to server
9a01905 mongo.erl is top-level interface
Tony Hannan authored
60 disconnect (Conn) -> mongo_connect:close (Conn).
61
5cbf4de Update types and Readme that referred to old pool name
Tony Hannan authored
62 -spec connect_factory (host()) -> resource_pool:factory(connection()).
63 %@doc Factory for use with a connection pool. See resource_pool module.
cbd5489 connect with timeout
Tony Hannan authored
64 connect_factory (Host) -> connect_factory (Host, infinity).
65
66 -spec connect_factory (host(), timeout()) -> resource_pool:factory(connection()).
67 %@doc Factory for use with a connection pool. See resource_pool module.
68 connect_factory (Host, TimeoutMS) -> {Host, fun (H) -> connect (H, TimeoutMS) end, fun disconnect/1, fun mongo_connect:is_closed/1}.
b802070 is_closed. mongo: replica sets & pools
Tony Hannan authored
69
70 % Replica Set %
71
72 -type replset() :: mongo_replset:replset().
73 -type rs_connection() :: mongo_replset:rs_connection().
74
75 -spec rs_connect (replset()) -> rs_connection(). % IO
e7bc02e edoc annotations in src comments
Tony Hannan authored
76 %@doc Create new cache of connections to replica set members starting with seed members. No connection attempted until rs_primary or rs_secondary_ok called.
b802070 is_closed. mongo: replica sets & pools
Tony Hannan authored
77 rs_connect (Replset) -> mongo_replset:connect (Replset).
78
cbd5489 connect with timeout
Tony Hannan authored
79 -spec rs_connect (replset(), timeout()) -> rs_connection(). % IO
116b695 Timeout on receive too
Tony Hannan authored
80 %@doc Create new cache of connections to replica set members starting with seed members. No connection attempted until rs_primary or rs_secondary_ok called. Timeout used for initial connection and every query and safe write.
cbd5489 connect with timeout
Tony Hannan authored
81 rs_connect (Replset, TimeoutMS) -> mongo_replset:connect (Replset, TimeoutMS).
82
b802070 is_closed. mongo: replica sets & pools
Tony Hannan authored
83 -spec rs_disconnect (rs_connection()) -> ok. % IO
e7bc02e edoc annotations in src comments
Tony Hannan authored
84 %@doc Close cache of replset connections
b802070 is_closed. mongo: replica sets & pools
Tony Hannan authored
85 rs_disconnect (ReplsetConn) -> mongo_replset:close (ReplsetConn).
86
5cbf4de Update types and Readme that referred to old pool name
Tony Hannan authored
87 -spec rs_connect_factory (replset()) -> resource_pool:factory(rs_connection()).
88 %@doc Factory for use with a rs_connection pool. See resource_pool module.
cbd5489 connect with timeout
Tony Hannan authored
89 rs_connect_factory (ReplSet) -> rs_connect_factory (ReplSet, infinity).
90
91 -spec rs_connect_factory (replset(), timeout()) -> resource_pool:factory(rs_connection()).
92 %@doc Factory for use with a rs_connection pool. See resource_pool module.
93 rs_connect_factory (Replset, TimeoutMS) -> {Replset, fun (RS) -> RC = rs_connect (RS, TimeoutMS), {ok, RC} end, fun rs_disconnect/1, fun mongo_replset:is_closed/1}.
b802070 is_closed. mongo: replica sets & pools
Tony Hannan authored
94
95 % Action %
96
9a01905 mongo.erl is top-level interface
Tony Hannan authored
97 -type action(A) :: fun (() -> A).
d6fd289 Fix wire protocol bit flags order. Catch not_master read and write er…
Tony Hannan authored
98 % An Action does IO, reads process dict {mongo_action_context, #context{}}, and throws failure()
9a01905 mongo.erl is top-level interface
Tony Hannan authored
99
100 -type failure() ::
d6fd289 Fix wire protocol bit flags order. Catch not_master read and write er…
Tony Hannan authored
101 mongo_connect:failure() | % thrown by read and safe write
fe00618 auth & add_user
Tony Hannan authored
102 mongo_query:not_master() | % thrown by read and safe write
103 mongo_query:unauthorized() | % thrown by read and safe write
104 write_failure() | % thrown by safe write
d6fd289 Fix wire protocol bit flags order. Catch not_master read and write er…
Tony Hannan authored
105 mongo_cursor:expired(). % thrown by cursor next/rest
9a01905 mongo.erl is top-level interface
Tony Hannan authored
106
107 -record (context, {
108 write_mode :: write_mode(),
109 read_mode :: read_mode(),
110 dbconn :: mongo_connect:dbconnection() }).
111
187d967 mongo:do accepts connnection() or rs_connection()
Tony Hannan authored
112 -spec do (write_mode(), read_mode(), connection() | rs_connection(), db(), action(A)) -> {ok, A} | {failure, failure()}. % IO
e7bc02e edoc annotations in src comments
Tony Hannan authored
113 %@doc Execute mongo action under given write_mode, read_mode, connection, and db. Return action result or failure.
aa1741b fix accepting rs_connection in mongo:do
Tony Hannan authored
114 do (WriteMode, ReadMode, Connection, Database, Action) -> case connection_mode (ReadMode, Connection) of
115 {error, Reason} -> {failure, {connection_failure, Reason}};
116 {ok, Conn} ->
117 PrevContext = get (mongo_action_context),
118 put (mongo_action_context, #context {write_mode = WriteMode, read_mode = ReadMode, dbconn = {Database, Conn}}),
119 try Action() of
120 Result -> {ok, Result}
121 catch
122 throw: E = {connection_failure, _, _} -> {failure, E};
123 throw: E = not_master -> {failure, E};
fe00618 auth & add_user
Tony Hannan authored
124 throw: E = unauthorized -> {failure, E};
aa1741b fix accepting rs_connection in mongo:do
Tony Hannan authored
125 throw: E = {write_failure, _, _} -> {failure, E};
126 throw: E = {cursor_expired, _} -> {failure, E}
127 after
128 case PrevContext of undefined -> erase (mongo_action_context); _ -> put (mongo_action_context, PrevContext) end
129 end end.
130
131 -spec connection_mode (read_mode(), connection() | rs_connection()) -> {ok, connection()} | {error, reason()}. % IO
e7bc02e edoc annotations in src comments
Tony Hannan authored
132 %@doc For rs_connection return appropriate primary or secondary connection
116b695 Timeout on receive too
Tony Hannan authored
133 connection_mode (_, Conn = {connection, _, _, _}) -> {ok, Conn};
cbd5489 connect with timeout
Tony Hannan authored
134 connection_mode (master, RsConn = {rs_connection, _, _, _}) -> mongo_replset:primary (RsConn);
135 connection_mode (slave_ok, RsConn = {rs_connection, _, _, _}) -> mongo_replset:secondary_ok (RsConn).
187d967 mongo:do accepts connnection() or rs_connection()
Tony Hannan authored
136
fe00618 auth & add_user
Tony Hannan authored
137 -spec this_db () -> db(). % Action
138 %@doc Current db in context that we are querying
139 this_db () -> {Db, _} = (get (mongo_action_context)) #context.dbconn, Db.
140
9a01905 mongo.erl is top-level interface
Tony Hannan authored
141 % Write %
142
143 -type write_mode() :: unsafe | safe | {safe, mongo_query:getlasterror_request()}.
144 % Every write inside an action() will use this write mode.
145 % unsafe = asynchronous write (no reply) and hence may silently fail;
146 % safe = synchronous write, wait for reply and fail if connection or write failure;
ba810a9 Add comment on getLastError
Tony Hannan authored
147 % {safe, Params} = same as safe but with extra params for getlasterror, see its documentation at http://www.mongodb.org/display/DOCS/getLastError+Command.
9a01905 mongo.erl is top-level interface
Tony Hannan authored
148
149 -type write_failure() :: {write_failure, error_code(), bson:utf8()}.
150 -type error_code() :: integer().
151
152 -spec write (mongo_query:write()) -> ok. % Action
e7bc02e edoc annotations in src comments
Tony Hannan authored
153 %@doc Do unsafe unacknowledged fast write or safe acknowledged slower write depending on our context. When safe, throw write_failure if acknowledgment (getlasterror) reports error.
9a01905 mongo.erl is top-level interface
Tony Hannan authored
154 write (Write) ->
155 Context = get (mongo_action_context),
156 case Context #context.write_mode of
157 unsafe -> mongo_query:write (Context #context.dbconn, Write);
158 SafeMode ->
0a8cc8c Documents are now tuples instead of lists
Tony Hannan authored
159 Params = case SafeMode of safe -> {}; {safe, Param} -> Param end,
9a01905 mongo.erl is top-level interface
Tony Hannan authored
160 Ack = mongo_query:write (Context #context.dbconn, Write, Params),
161 case bson:lookup (err, Ack) of
162 {} -> ok; {null} -> ok;
d6fd289 Fix wire protocol bit flags order. Catch not_master read and write er…
Tony Hannan authored
163 {String} -> case bson:at (code, Ack) of
164 10058 -> throw (not_master);
165 Code -> throw ({write_failure, Code, String}) end end end.
9a01905 mongo.erl is top-level interface
Tony Hannan authored
166
167 -spec insert (collection(), bson:document()) -> bson:value(). % Action
e7bc02e edoc annotations in src comments
Tony Hannan authored
168 %@doc Insert document into collection. Return its '_id' value, which is auto-generated if missing.
9a01905 mongo.erl is top-level interface
Tony Hannan authored
169 insert (Coll, Doc) -> [Value] = insert_all (Coll, [Doc]), Value.
170
171 -spec insert_all (collection(), [bson:document()]) -> [bson:value()]. % Action
e7bc02e edoc annotations in src comments
Tony Hannan authored
172 %@doc Insert documents into collection. Return their '_id' values, which are auto-generated if missing.
9a01905 mongo.erl is top-level interface
Tony Hannan authored
173 insert_all (Coll, Docs) ->
174 Docs1 = lists:map (fun assign_id/1, Docs),
175 write (#insert {collection = Coll, documents = Docs1}),
176 lists:map (fun (Doc) -> bson:at ('_id', Doc) end, Docs1).
177
178 -spec assign_id (bson:document()) -> bson:document(). % IO
e7bc02e edoc annotations in src comments
Tony Hannan authored
179 %@doc If doc has no '_id' field then generate a fresh object id for it
9a01905 mongo.erl is top-level interface
Tony Hannan authored
180 assign_id (Doc) -> case bson:lookup ('_id', Doc) of
181 {_Value} -> Doc;
0a8cc8c Documents are now tuples instead of lists
Tony Hannan authored
182 {} -> bson:append ({'_id', mongodb_app:gen_objectid()}, Doc) end.
9a01905 mongo.erl is top-level interface
Tony Hannan authored
183
184 -spec save (collection(), bson:document()) -> ok. % Action
e7bc02e edoc annotations in src comments
Tony Hannan authored
185 %@doc If document has no '_id' field then insert it, otherwise update it and insert only if missing.
9a01905 mongo.erl is top-level interface
Tony Hannan authored
186 save (Coll, Doc) -> case bson:lookup ('_id', Doc) of
fe00618 auth & add_user
Tony Hannan authored
187 {} -> insert (Coll, Doc), ok;
0a8cc8c Documents are now tuples instead of lists
Tony Hannan authored
188 {Id} -> repsert (Coll, {'_id', Id}, Doc) end.
9a01905 mongo.erl is top-level interface
Tony Hannan authored
189
190 -spec replace (collection(), selector(), bson:document()) -> ok. % Action
e7bc02e edoc annotations in src comments
Tony Hannan authored
191 %@doc Replace first document selected with given document.
9a01905 mongo.erl is top-level interface
Tony Hannan authored
192 replace (Coll, Selector, Doc) -> update (false, false, Coll, Selector, Doc).
193
194 -spec repsert (collection(), selector(), bson:document()) -> ok. % Action
e7bc02e edoc annotations in src comments
Tony Hannan authored
195 %@doc Replace first document selected with given document, or insert it if selection is empty.
9a01905 mongo.erl is top-level interface
Tony Hannan authored
196 repsert (Coll, Selector, Doc) -> update (true, false, Coll, Selector, Doc).
197
198 -spec modify (collection(), selector(), modifier()) -> ok. % Action
e7bc02e edoc annotations in src comments
Tony Hannan authored
199 %@doc Update all documents selected using modifier
9a01905 mongo.erl is top-level interface
Tony Hannan authored
200 modify (Coll, Selector, Mod) -> update (false, true, Coll, Selector, Mod).
201
202 -spec update (boolean(), boolean(), collection(), selector(), bson:document()) -> ok. % Action
203 update (Upsert, MultiUpdate, Coll, Sel, Doc) ->
204 write (#update {collection = Coll, upsert = Upsert, multiupdate = MultiUpdate, selector = Sel, updater = Doc}).
205
206 -spec delete (collection(), selector()) -> ok. % Action
e7bc02e edoc annotations in src comments
Tony Hannan authored
207 %@doc Delete selected documents
9a01905 mongo.erl is top-level interface
Tony Hannan authored
208 delete (Coll, Selector) ->
209 write (#delete {collection = Coll, singleremove = false, selector = Selector}).
210
211 -spec delete_one (collection(), selector()) -> ok. % Action
e7bc02e edoc annotations in src comments
Tony Hannan authored
212 %@doc Delete first selected document.
9a01905 mongo.erl is top-level interface
Tony Hannan authored
213 delete_one (Coll, Selector) ->
214 write (#delete {collection = Coll, singleremove = true, selector = Selector}).
215
216 % Read %
217
218 -type read_mode() :: master | slave_ok.
219 % Every query inside an action() will use this mode.
220 % master = Server must be master/primary so reads are consistent (read latest writes).
b802070 is_closed. mongo: replica sets & pools
Tony Hannan authored
221 % slave_ok = Server may be slave/secondary so reads may not be consistent (may read stale data). Slaves will eventually get the latest writes, so technically this is called eventually-consistent.
9a01905 mongo.erl is top-level interface
Tony Hannan authored
222
223 slave_ok (#context {read_mode = slave_ok}) -> true;
224 slave_ok (#context {read_mode = master}) -> false.
225
226 -type maybe(A) :: {A} | {}.
227
228 -spec find_one (collection(), selector()) -> maybe (bson:document()). % Action
e7bc02e edoc annotations in src comments
Tony Hannan authored
229 %@doc Return first selected document, if any
9a01905 mongo.erl is top-level interface
Tony Hannan authored
230 find_one (Coll, Selector) -> find_one (Coll, Selector, []).
231
232 -spec find_one (collection(), selector(), projector()) -> maybe (bson:document()). % Action
e7bc02e edoc annotations in src comments
Tony Hannan authored
233 %@doc Return projection of first selected document, if any. Empty projection [] means full projection.
9a01905 mongo.erl is top-level interface
Tony Hannan authored
234 find_one (Coll, Selector, Projector) -> find_one (Coll, Selector, Projector, 0).
235
236 -spec find_one (collection(), selector(), projector(), skip()) -> maybe (bson:document()). % Action
e7bc02e edoc annotations in src comments
Tony Hannan authored
237 %@doc Return projection of Nth selected document, if any. Empty projection [] means full projection.
9a01905 mongo.erl is top-level interface
Tony Hannan authored
238 find_one (Coll, Selector, Projector, Skip) ->
239 Context = get (mongo_action_context),
240 Query = #'query' {
241 collection = Coll, selector = Selector, projector = Projector,
242 skip = Skip, slaveok = slave_ok (Context) },
243 mongo_query:find_one (Context #context.dbconn, Query).
244
245 -spec find (collection(), selector()) -> cursor(). % Action
e7bc02e edoc annotations in src comments
Tony Hannan authored
246 %@doc Return selected documents.
9a01905 mongo.erl is top-level interface
Tony Hannan authored
247 find (Coll, Selector) -> find (Coll, Selector, []).
248
249 -spec find (collection(), selector(), projector()) -> cursor(). % Action
e7bc02e edoc annotations in src comments
Tony Hannan authored
250 %@doc Return projection of selected documents. Empty projection [] means full projection.
9a01905 mongo.erl is top-level interface
Tony Hannan authored
251 find (Coll, Selector, Projector) -> find (Coll, Selector, Projector, 0).
252
253 -spec find (collection(), selector(), projector(), skip()) -> cursor(). % Action
e7bc02e edoc annotations in src comments
Tony Hannan authored
254 %@doc Return projection of selected documents starting from Nth document. Empty projection means full projection.
9a01905 mongo.erl is top-level interface
Tony Hannan authored
255 find (Coll, Selector, Projector, Skip) -> find (Coll, Selector, Projector, Skip, 0).
256
257 -spec find (collection(), selector(), projector(), skip(), batchsize()) -> cursor(). % Action
e7bc02e edoc annotations in src comments
Tony Hannan authored
258 %@doc Return projection of selected documents starting from Nth document in batches of batchsize. 0 batchsize means default batch size. Negative batch size means one batch only. Empty projection means full projection.
9a01905 mongo.erl is top-level interface
Tony Hannan authored
259 find (Coll, Selector, Projector, Skip, BatchSize) ->
260 Context = get (mongo_action_context),
261 Query = #'query' {
262 collection = Coll, selector = Selector, projector = Projector,
263 skip = Skip, batchsize = BatchSize, slaveok = slave_ok (Context) },
264 mongo_query:find (Context #context.dbconn, Query).
265
266 -type cursor() :: mongo_cursor:cursor().
267
268 -spec next (cursor()) -> maybe (bson:document()). % IO throws mongo_connect:failure() & mongo_cursor:expired() (this is a subtype of Action)
e7bc02e edoc annotations in src comments
Tony Hannan authored
269 %@doc Return next document in query result cursor, if any.
9a01905 mongo.erl is top-level interface
Tony Hannan authored
270 next (Cursor) -> mongo_cursor:next (Cursor).
271
272 -spec rest (cursor()) -> [bson:document()]. % IO throws mongo_connect:failure() & mongo_cursor:expired() (this is a subtype of Action)
e7bc02e edoc annotations in src comments
Tony Hannan authored
273 %@doc Return remaining documents in query result cursor.
9a01905 mongo.erl is top-level interface
Tony Hannan authored
274 rest (Cursor) -> mongo_cursor:rest (Cursor).
275
276 -spec close_cursor (cursor()) -> ok. % IO (IO is a subtype of Action)
e7bc02e edoc annotations in src comments
Tony Hannan authored
277 %@doc Close cursor
9a01905 mongo.erl is top-level interface
Tony Hannan authored
278 close_cursor (Cursor) -> mongo_cursor:close (Cursor).
279
280 -spec count (collection(), selector()) -> integer(). % Action
e7bc02e edoc annotations in src comments
Tony Hannan authored
281 %@doc Count selected documents
9a01905 mongo.erl is top-level interface
Tony Hannan authored
282 count (Coll, Selector) -> count (Coll, Selector, 0).
283
284 -spec count (collection(), selector(), integer()) -> integer(). % Action
e7bc02e edoc annotations in src comments
Tony Hannan authored
285 %@doc Count selected documents up to given max number; 0 means no max. Ie. stops counting when max is reached to save processing time.
9a01905 mongo.erl is top-level interface
Tony Hannan authored
286 count (Coll, Selector, Limit) ->
0a8cc8c Documents are now tuples instead of lists
Tony Hannan authored
287 CollStr = atom_to_binary (Coll, utf8),
288 Command = if
289 Limit =< 0 -> {count, CollStr, 'query', Selector};
290 true -> {count, CollStr, 'query', Selector, limit, Limit} end,
291 Doc = command (Command),
9a01905 mongo.erl is top-level interface
Tony Hannan authored
292 trunc (bson:at (n, Doc)). % Server returns count as float
293
294 % Command %
295
296 -type command() :: mongo_query:command().
297
298 -spec command (command()) -> bson:document(). % Action
e7bc02e edoc annotations in src comments
Tony Hannan authored
299 %@doc Execute given MongoDB command and return its result.
9a01905 mongo.erl is top-level interface
Tony Hannan authored
300 command (Command) ->
301 Context = get (mongo_action_context),
302 mongo_query:command (Context #context.dbconn, Command, slave_ok (Context)).
f59460d mongo:create_index
Tony Hannan authored
303
fe00618 auth & add_user
Tony Hannan authored
304 % Authentication %
305
306 -type username() :: bson:utf8().
307 -type password() :: bson:utf8().
308 -type nonce() :: bson:utf8().
309
310 -spec auth (username(), password()) -> boolean(). % Action
311 %@doc Authenticate with the database (if server is running in secure mode). Return whether authentication was successful or not. Reauthentication is required for every new pipe.
312 auth (Username, Password) ->
66853bc copy_database
Tony Hannan authored
313 Nonce = bson:at (nonce, command ({getnonce, 1})),
314 try command ({authenticate, 1, user, Username, nonce, Nonce, key, pw_key (Nonce, Username, Password)})
fe00618 auth & add_user
Tony Hannan authored
315 of _ -> true
316 catch error:{bad_command, _} -> false end.
317
318 -spec pw_key (nonce(), username(), password()) -> bson:utf8().
319 pw_key (Nonce, Username, Password) -> bson:utf8 (binary_to_hexstr (crypto:md5 ([Nonce, Username, pw_hash (Username, Password)]))).
320
321 -spec pw_hash (username(), password()) -> bson:utf8().
322 pw_hash (Username, Password) -> bson:utf8 (binary_to_hexstr (crypto:md5 ([Username, <<":mongo:">>, Password]))).
323
324 -spec binary_to_hexstr (binary()) -> string().
325 binary_to_hexstr (Bin) ->
326 lists:flatten ([io_lib:format ("~2.16.0b", [X]) || X <- binary_to_list (Bin)]).
327
328 -type permission() :: read_write | read_only.
329
330 -spec add_user (permission(), username(), password()) -> ok. % Action
331 %@doc Add user with given access rights (permission)
332 add_user (Permission, Username, Password) ->
333 User = case find_one (system.users, {user, Username}) of {} -> {user, Username}; {Doc} -> Doc end,
334 Rec = {readOnly, case Permission of read_only -> true; read_write -> false end, pwd, pw_hash (Username, Password)},
335 save (system.users, bson:merge (Rec, User)).
336
337 % Index %
f59460d mongo:create_index
Tony Hannan authored
338
5a5ff25 create_index with optional fields allowed
Tony Hannan authored
339 -type index_spec() :: bson:document().
340 % The following fields are required:
341 % key : key_order()
342 % The following fields are optional:
343 % name : bson:utf8()
344 % unique : boolean()
345 % dropDups : boolean()
346 % Additional fields are allowed specific to the index, for example, when creating a Geo index you may also supply
347 % min & max fields. See http://www.mongodb.org/display/DOCS/Geospatial+Indexing for details.
f59460d mongo:create_index
Tony Hannan authored
348
5a5ff25 create_index with optional fields allowed
Tony Hannan authored
349 -type key_order() :: bson:document().
350 % Fields to index on and whether ascending (1) or descending (-1) or Geo (<<"2d">>). Eg. {x,1, y,-1} or {loc, <<"2d">>}
f59460d mongo:create_index
Tony Hannan authored
351
5a5ff25 create_index with optional fields allowed
Tony Hannan authored
352 -spec create_index (collection(), index_spec() | key_order()) -> ok. % Action
353 %@doc Create index on collection according to given spec. Allow user to just supply key
354 create_index (Coll, IndexSpec) ->
355 Db = this_db (),
356 Index = bson:append ({ns, mongo_protocol:dbcoll (Db, Coll)}, fillout_indexspec (IndexSpec)),
357 insert ('system.indexes', Index).
f59460d mongo:create_index
Tony Hannan authored
358
5a5ff25 create_index with optional fields allowed
Tony Hannan authored
359 -spec fillout_indexspec (index_spec() | key_order()) -> index_spec().
4e8ae2a fix create_index to allow user key field named 'key'
Tony Hannan authored
360 % Fill in missing optonal fields with defaults. Allow user to just supply key_order
5a5ff25 create_index with optional fields allowed
Tony Hannan authored
361 fillout_indexspec (IndexSpec) -> case bson:lookup (key, IndexSpec) of
4e8ae2a fix create_index to allow user key field named 'key'
Tony Hannan authored
362 {Key} when is_tuple (Key) -> bson:merge (IndexSpec, {key, Key, name, gen_index_name (Key), unique, false, dropDups, false});
363 {_} -> {key, IndexSpec, name, gen_index_name (IndexSpec), unique, false, dropDups, false}; % 'key' happens to be a user field
364 {} -> {key, IndexSpec, name, gen_index_name (IndexSpec), unique, false, dropDups, false} end.
f59460d mongo:create_index
Tony Hannan authored
365
366 -spec gen_index_name (key_order()) -> bson:utf8().
367 gen_index_name (KeyOrder) ->
368 AsName = fun (Label, Order, Name) -> <<
6cdf847 fix gen_index_name
Tony Hannan authored
369 Name /binary, $_,
370 (atom_to_binary (Label, utf8)) /binary, $_,
371 (if
372 is_integer (Order) -> bson:utf8 (integer_to_list (Order));
373 is_atom (Order) -> atom_to_binary (Order, utf8);
374 is_binary (Order) -> Order;
375 true -> <<>> end) /binary >> end,
376 bson:doc_foldl (AsName, <<"i">>, KeyOrder).
f59460d mongo:create_index
Tony Hannan authored
377
66853bc copy_database
Tony Hannan authored
378 % Admin
379
380 -spec copy_database (db(), host(), db()) -> bson:document(). % Action
381 % Copy database from given host to the server I am connected to. Must be connected to 'admin' database.
382 copy_database (FromDb, FromHost, ToDb) ->
383 command ({copydb, 1, fromhost, mongo_connect:show_host (FromHost), fromdb, atom_to_binary (FromDb, utf8), todb, atom_to_binary (ToDb, utf8)}).
384
385 -spec copy_database (db(), host(), db(), username(), password()) -> bson:document(). % Action
386 % Copy database from given host, authenticating with given username and password, to the server I am connected to. Must be connected to 'admin' database.
387 copy_database (FromDb, FromHost, ToDb, Username, Password) ->
388 Nonce = bson:at (nonce, command ({copydbgetnonce, 1, fromhost, mongo_connect:show_host (FromHost)})),
389 command ({copydb, 1, fromhost, mongo_connect:show_host (FromHost), fromdb, atom_to_binary (FromDb, utf8), todb, atom_to_binary (ToDb, utf8), username, Username, nonce, Nonce, key, pw_key (Nonce, Username, Password)}).
Something went wrong with that request. Please try again.