anchor.diff:  	trunc(bson:at(n, Doc)). % Server returns count as float
   
   %% @doc Create index on collection according to given spec.
  -%%      Allow user to just supply key
  +%%      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),
  -	Index = bson:update(ns, mongo_protocol:dbcoll(Database, Coll), make_index(IndexSpec)),
  +	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.
  
positive.code: assign_id(Doc) ->
  	case bson:lookup('_id', Doc) of
  		{_Value} -> Doc;
  		{} -> bson:update('_id', mongo_sup:gen_objectid(), Doc)
  	end.
  
  %% @private
  make_index(IndexSpec) ->
  	case bson:lookup (key, IndexSpec) of
  		{Key} when is_tuple (Key) ->
  			bson:merge(IndexSpec, {key, Key, name, make_index_name(Key), unique, false, dropDups, false});
  		{_} ->
  			{key, IndexSpec, name, make_index_name(IndexSpec), unique, false, dropDups, false};
  		{} ->
  			{key, IndexSpec, name, make_index_name(IndexSpec), unique, false, dropDups, false}
  	end.
  
  %% @private
  make_index_name (KeyOrder) ->
  	AsName = fun(Label, Order, Name) ->
  		<<Name /binary, $_, (atom_to_binary (Label, utf8))/binary, $_,
  		(if
  			is_integer (Order) -> bson:utf8 (integer_to_list (Order));
  			is_atom (Order) -> atom_to_binary (Order, utf8);
  			is_binary (Order) -> Order;
  			true -> <<>>
  		end)/binary>>
  	end,
  	bson:doc_foldl(AsName, <<"i">>, KeyOrder).
  
  %% @private
  write(Request) ->
  
negative.code: %% @private
  write(Connection, Database, Request, GetLastErrorParams) ->
  	ok = mongo_connection:request(Connection, Database, Request),
  	{0, [Doc | _]} = mongo_connection:request(Connection, Database, #'query'{
  		batchsize = -1,
  		collection = '$cmd',
  		selector = bson:append({getlasterror, 1}, GetLastErrorParams)
  	}),
  	Doc.
  
  %% @private
  read(Request) ->
  	#context{connection = Connection, database = Database, read_mode = ReadMode} = erlang:get(mongo_action_context),
  	#'query'{collection = Collection, batchsize = BatchSize} = Request,
  	{Cursor, Batch} = mongo_connection:request(Connection, Database, Request#'query'{slaveok = ReadMode =:= slave_ok}),
  	mongo_cursor:create(Connection, Database, Collection, Cursor, BatchSize, Batch).
  
  %% @private
  read_one(Request) ->
  	#context{connection = Connection, database = Database, read_mode = ReadMode} = erlang:get(mongo_action_context),
  	{0, Docs} = mongo_connection:request(Connection, Database, Request#'query'{batchsize = -1, slaveok = ReadMode =:= slave_ok}),
  	case Docs of
  		[] -> {};
  		[Doc | _] -> {Doc}
  	end.
  
  