anchor.diff:  	command/1,
   	create_index/2
   ]).
  -
  -%-export ([auth/2]).
  -%-export ([add_user/3]).
  -%-export ([create_index/2]).
  -%-export ([copy_database/3, copy_database/5]).
  +% TODO: add auth/2
   
   -include("mongo_protocol.hrl").
   
  
positive.code: %% @private
  write(Connection, Database, Request, GetLastErrorParams) ->
  	ok = mongo_connection:request(Connection, Database, Request),
  	Reply = mongo_connection:request(Connection, Database, #'query'{
  		batchsize = -1,
  		collection = '$cmd',
  		selector = bson:append({getlasterror, 1}, GetLastErrorParams)
  	}),
  	{0, [Doc | _]} = process_reply(Reply),
  	Doc.
  
  %% @private
  read(Request) ->
  	#context{connection = Connection, database = Database, read_mode = ReadMode} = erlang:get(mongo_action_context),
  	#'query'{collection = Collection, batchsize = BatchSize} = Request,
  	Reply = mongo_connection:request(Connection, Database, Request#'query'{slaveok = ReadMode =:= slave_ok}),
  	mongo_cursor:create(Connection, Database, Collection, BatchSize, process_reply(Reply)).
  
  %% @private
  read_one(Request) ->
  	#context{connection = Connection, database = Database, read_mode = ReadMode} = erlang:get(mongo_action_context),
  	Reply = mongo_connection:request(Connection, Database, Request#'query'{batchsize = -1, slaveok = ReadMode =:= slave_ok}),
  	{0, Docs} = process_reply(Reply),
  	case Docs of
  		[] -> {};
  		[Doc | _] -> {Doc}
  	end.
  
  
  %% @private
  process_reply(#reply{cursornotfound = false, queryerror = false} = Reply) ->
  	{Reply#reply.cursorid, Reply#reply.documents};
  process_reply(#reply{cursornotfound = false, queryerror = true} = Reply) ->
  	[Doc | _] = Reply#reply.documents,
  	process_reply_error(bson:at(code, Doc), Doc).
  
  %% @private
  process_reply_error(13435, _) ->
  	erlang:error(not_master);
  process_reply_error(10057, _) ->
  	erlang:error(unauthorized);
  process_reply_error(_, Doc) ->
  	erlang:error({bad_query, Doc}).
  
  
  
  
  % Admin
  
  %-spec copy_database (db(), host(), db()) -> bson:document(). % Action
  %% Copy database from given host to the server I am connected to. Must be connected to 'admin' database.
  %copy_database (FromDb, FromHost, ToDb) ->
  %	command ({copydb, 1, fromhost, mongo_connect:show_host (FromHost), fromdb, atom_to_binary (FromDb, utf8), todb, atom_to_binary (ToDb, utf8)}).
  %
  %-spec copy_database (db(), host(), db(), username(), password()) -> bson:document(). % Action
  %% Copy database from given host, authenticating with given username and password, to the server I am connected to. Must be connected to 'admin' database.
  %copy_database (FromDb, FromHost, ToDb, Username, Password) ->
  %	Nonce = bson:at (nonce, command ({copydbgetnonce, 1, fromhost, mongo_connect:show_host (FromHost)})),
  %	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)}).
  
negative.code: 	trunc(bson:at(n, Doc)). % Server returns count as float
  
  %% @doc Create index on collection according to given spec.
  %%      The key specification is a bson documents with the following fields:
  %%      key      :: bson document, for e.g. {field, 1, other, -1, location, 2d}, <strong>required</strong>
  %%      name     :: bson:utf8()
  %%      unique   :: boolean()
  %%      dropDups :: boolean()
  -spec create_index (collection(), bson:document()) -> ok.
  create_index(Coll, IndexSpec) ->
  	#context{database = Database} = erlang:get(mongo_action_context),
  	Key = bson:at(key, IndexSpec),
  	Defaults = {name, gen_index_name(Key), unique, false, dropDups, false},
  	Index = bson:update(ns, mongo_protocol:dbcoll(Database, Coll), bson:merge(IndexSpec, Defaults)),
  	insert('system.indexes', Index).
  
  %% @doc Execute given MongoDB command and return its result.
  