Creating a document
The steps that are shown here demonstrate how to create a document.
-
Send a
POST
request with the document's JSON content to create a document. -
Run the following command:
https://$ACCOUNT.cloudant.com/$DATABASE
.
Creating a document by using HTTP
The following steps show you how to create a document by using HTTP.
-
Send a
PUT
request with the document's JSON content by using HTTP to create a document. -
Run the following command:
PUT /$DATABASE/$DOC_ID HTTP/1.1
Content-Type: application/json
Examples of how to create a document
See an example of creating an IBM Cloudant document for this JSON in a nonpartitioned database:
{
"type": "event",
"userid": "abc123",
"eventType": "addedToBasket",
"productId": "1000042",
"date": "2019-01-28T10:44:22.000Z"
}
You can customize creating a document for the programming language that you want to use by selecting the language in the code examples.
See an example of creating a document:
curl -H "Authorization: Bearer $API_BEARER_TOKEN" -X PUT "$SERVICE_URL/events/0007241142412418284" -H "Content-Type: application/json" --data '{ "type": "event", "userid": "abc123", "eventType": "addedToBasket", "productId": "1000042", "date": "2019-01-28T10:44:22.000Z }'
import com.ibm.cloud.cloudant.v1.Cloudant;
import com.ibm.cloud.cloudant.v1.model.Document;
import com.ibm.cloud.cloudant.v1.model.DocumentResult;
import com.ibm.cloud.cloudant.v1.model.PutDocumentOptions;
Cloudant service = Cloudant.newInstance();
Document eventDoc = new Document();
eventDoc.put("type", "event");
eventDoc.put("userid", "abc123");
eventDoc.put("eventType", "addedToBasket");
eventDoc.put("productId", "1000042");
eventDoc.put("date", "2019-01-28T10:44:22.000Z");
PutDocumentOptions documentOptions =
new PutDocumentOptions.Builder()
.db("events")
.docId("0007241142412418284")
.document(eventDoc)
.build();
DocumentResult response =
service.putDocument(documentOptions).execute()
.getResult();
System.out.println(response);
const { CloudantV1 } = require('@ibm-cloud/cloudant');
const service = CloudantV1.newInstance({});
const eventDoc = {
type: 'event',
userid: 'abc123',
eventType: 'addedToBasket',
productId: '1000042',
date: '2019-01-28T10:44:22.000Z'
};
service.putDocument({
db: 'events',
docId: '0007241142412418284',
document: eventDoc
}).then(response => {
console.log(response.result);
});
from ibmcloudant.cloudant_v1 import Document, CloudantV1
service = CloudantV1.new_instance()
event_doc = Document(
type='event',
userid='abc123',
eventType='addedToBasket',
productId='1000042',
date='2019-01-28T10:44:22.000Z'
)
response = service.put_document(
db='events',
doc_id='0007241142412418284',
document=event_doc
).get_result()
print(response)
eventDoc := cloudantv1.Document{}
eventDoc.SetProperty("type", "event")
eventDoc.SetProperty("userid", "abc123")
eventDoc.SetProperty("eventType", "addedToBasket")
eventDoc.SetProperty("productId", "1000042")
eventDoc.SetProperty("date", "2019-01-28T10:44:22.000Z")
putDocumentOptions := service.NewPutDocumentOptions(
"events",
"0007241142412418284",
)
putDocumentOptions.SetDocument(&eventDoc)
documentResult, response, err := service.PutDocument(putDocumentOptions)
if err != nil {
panic(err)
}
b, _ := json.MarshalIndent(documentResult, "", " ")
fmt.Println(string(b))
The previous Go example requires the following import block:
import (
"encoding/json"
"fmt"
"github.com/IBM/cloudant-go-sdk/cloudantv1"
)
All Go examples require the service
object to be initialized. For more information, see the API documentation's Authentication section for examples.
The response is a JSON document that contains the ID of the created document, the revision string, and "ok": true
.
If you didn't provide an _id
field, IBM Cloudant generates one automatically as a
UUID.
A failure to create the document results in a response that contains a description of the error.
See an example response after successfully creating a document:
{
"id": "exampleid",
"ok": true,
"rev": "2-056f5f44046ecafc08a2bc2b9c229e20"
}
If the write quorum can't be met during an attempt to create a document, a 202
response is returned.