You are currently browsing legacy 3.0 version of documentation. Click here to switch to the newest 4.2 version.

We can help you with migration to the latest RavenDB

Contact Us Now
see on GitHub

Commands: Documents: Put

Put is used to insert or update a document in a database.

Syntax

curl \
	http://{serverUrl}/databases/{databaseName}/docs/{key}  \
	-X PUT \
	-d @jsonData.txt  \
	--header "anyKey:anyValue" \
    --header "If-None-Match:{etag}" 
 
curl \
	http://{serverUrl}/databases/{databaseName}/docs \
	-X POST \
	-d @jsonData.txt  \
	--header "anyKey:anyValue" \	
    --header "If-None-Match:{etag}"

Request

Method Description
POST document key isn't passed in url and is auto-generated on server side
PUT document key is passed in url
Payload
Json document data
Query parameter Required Description
key for PUT unique key under which document will be stored
Header Required Description
If-None-Match No Used to pass document Etag
Any other header No Used to pass document metadata

Response

Status code Description
200 OK - for POST
201 Created - for PUT
409 When concurrency exception occurred
Return Value Description
Key unique key under which document was stored
Etag stored document etag

Examples

Put document under key items/1 with CreatedBy and ItemOwner in metadata.

curl \
	-X PUT http://localhost:8080/docs/items/1  \
	-d "{ }"  \
	--header "CreatedBy:Adam Smith" \
	--header "ItemOwner:John Davis"
< HTTP/1.1 201 Created
{"Key":"items/1","ETag":"01000000-0000-0001-0000-000000000004"}

Put document and auto-generate key on server side.

curl -X POST http://localhost:8080/docs \
	-d "{ FirstName: 'Bob', LastName: 'Smith', Address: '5 Elm St' }"
< HTTP/1.1 200 OK
{"Key":"156b3ed5-ad89-47d9-96fa-ba11811b9fad","ETag":"01000000-0000-0001-0000-000000000004"}

Attempting to put document with invalid etag ends up with conflict.

curl -X PUT http://localhost:8080/docs/user/100  \
	-d "{ }" 
< HTTP/1.1 201 Created
{"Key":"user/100","ETag":"01000000-0000-0008-0000-00000000000A"}
 
curl -X PUT http://localhost:8080/docs/user/100 \
	-d "{ }" \
	--header "If-None-Match:01000000-0000-0008-0000-0000000000CC" 
< HTTP/1.1 409 Conflict
{
	"Url":"/docs/user/100",
	"ActualETag":"01000000-0000-0008-0000-00000000000A",
	"ExpectedETag":"01000000-0000-0008-0000-000000000010",
	"Error":"PUT attempted on document 'user/100' using a non current etag"
}