Document Access with Edge Server
You can create, read, update, and delete documents in a keyspace using the REST API’s document operations.
Scopes and Collections
Within a database, documents are organized using scopes and collections. A database contains one or more scopes, and each scope may contain one or more collections. All documents are stored within a collection.
Each database contains a default scope called _default, which contains a default collection, also called _default.
A database may contain other named, non-default scopes and collections as required.
For more information about scopes and collections, see Manage Scopes and Collections.
Keyspace References
When working with documents, you must specify the database, scope and collection where the documents are stored. To do this, you must provide a keyspace reference to the REST API.
-
For a named scope and collection, the keyspace reference consists of the database name, followed by the scope name and the collection name, separated by periods (
.). For example,travel-sample.inventory.airport. -
For a named collection within the default scope, you can omit the scope name from the keyspace reference. For example,
travel-sample.my_recordsrefers to themy_recordscollection within the default scope in thetravel-sampledatabase. -
For the default scope and collection, you can omit both the scope name and the collection name from the keyspace reference. For example,
travel-samplerefers to the default collection within the default scope in thetravel-sampledatabase.
Document Structure
In Edge Server, the content of each document is a JSON object. Each document has metadata, which includes the document ID and revision number. Documents can have subdocuments and binary attachments.
Examples
In the command line examples:
-
$USERand$PASSWORDare the credentials for Couchbase Edge Server. -
$CERTis the name and path to a TLS certificate file, for examplecert.cer. -
$HOSTand$PORTare the host and port to connect to Couchbase Edge Server, for examplelocalhost:59840. -
$DOCIDis the ID of a document, for example~SCH2oNtKFMBdcO-_sUhBmn. -
$REVis a revision number, for example1-22855783cf597c31c37ec3815d8027f3706ef6f9.
| The following cURL examples use a self-signed TLS server certificate. In production, use a TLS certificate from a well-known certificate authority. |
Create a Document
The following request creates a new document in the specified keyspace.
curl --user $USER:$PASSWORD --cacert $CERT \
"https://$HOST:$PORT/travel-sample.inventory.airport" \
--json '{
"type": "airport",
"country": "United Kingdom",
"icao": "EGOV",
"airportname": "Anglesey Airport",
"city": "Valley",
"faa": "VLY",
"tz": "Europe/London"
}'
The response is a JSON object similar to the following. In this case, Edge Server generates an ID for the document automatically.
{
"ok": true,
"id": "~SCH2oNtKFMBdcO-_sUhBmn",
"rev": "1-22855783cf597c31c37ec3815d8027f3706ef6f9"
}
You can use the generated document ID to read, update, or delete the same document. You must use the document revision number for concurrency control.
Read a Document
The following request gets the specified document in the specified keyspace.
curl --user $USER:$PASSWORD --cacert $CERT \
"https://$HOST:$PORT/travel-sample.inventory.airport/$DOCID"
The response is a JSON object similar to the following. The response contains the document ID and revision number, as well as the document content.
{
"_id": "~SCH2oNtKFMBdcO-_sUhBmn",
"_rev": "1-22855783cf597c31c37ec3815d8027f3706ef6f9",
"type": "airport",
"country": "United Kingdom",
"icao": "EGOV",
"airportname": "Anglesey Airport",
"city": "Valley",
"faa": "VLY",
"tz": "Europe/London"
}
Update a Document
The following request checks whether the specified document within the specified keyspace has the expected revision number.
If so, it adds a geo subdocument to the document.
This ensures that you’re applying your update to the latest revision of the document, in cases where the document may be updated by other processes concurrently.
curl -XPUT --user $USER:$PASSWORD --cacert $CERT \
"https://$HOST:$PORT/travel-sample.inventory.airport/$DOCID/geo?rev=$REV" \
--json '{
"lat": 53.248056,
"lon": -4.535278,
"alt": 37
}'
If the revision number of the document matches, the response is a JSON object similar to the following. The response contains the new revision number.
{
"ok": true,
"id": "~SCH2oNtKFMBdcO-_sUhBmn",
"rev": "2-51cb776222e4a366e8961e1c9fc09edddc5b0fa1"
}
If the revision number does not match, the request returns a 409 error.
If this happens, get the latest revision number for the document and try again.
Set a Document Expiration
When a document expires, Edge Server purges it from the database and leaves no tombstone.
| Expiration only removes the document from Edge Server. Edge Server does not replicate the purge, so if you replicate with App Services or Sync Gateway, the document stays on the remote database. If the remote document changes later, Edge Server pulls the new revision and the document reappears. To remove a document everywhere, delete it instead. A delete creates a tombstone, which replicates. |
To set an expiration, add one of the following query parameters when you create a document or update a whole document.
| Parameter | Type | Description |
|---|---|---|
|
Integer |
The number of seconds from the time of the request until the document expires. |
|
Integer |
The time when the document expires, as a Unix timestamp in seconds.
For example, |
If you set both parameters, Edge Server uses whichever expiration time is earlier.
If you update a document without ttl or expires, the document keeps its existing expiration.
You set expiration with query parameters, not with fields in the request body.
Edge Server doesn’t read fields such as _exp or expiration from the body to set an expiration.
|
For more information, see Edge Server Public REST API.
The following request creates a new document in the specified keyspace, and sets the document to expire one hour after the request.
curl --user $USER:$PASSWORD --cacert $CERT \
"https://$HOST:$PORT/travel-sample.inventory.airport?ttl=3600" \
--json '{
"type": "airport",
"country": "United Kingdom",
"icao": "EGOV",
"airportname": "Anglesey Airport",
"city": "Valley",
"faa": "VLY",
"tz": "Europe/London"
}'
The response is a JSON object similar to the following.
The expires property shows the expiration time as a Unix timestamp in seconds.
{
"ok": true,
"id": "~SCH2oNtKFMBdcO-_sUhBmn",
"rev": "1-22855783cf597c31c37ec3815d8027f3706ef6f9",
"expires": 1793451600
}
The following request checks whether the specified document within the specified keyspace has the expected revision number. If so, it replaces the document content and sets the document to expire at 00:00 UTC on January 1, 2027.
curl -XPUT --user $USER:$PASSWORD --cacert $CERT \
"https://$HOST:$PORT/travel-sample.inventory.airport/$DOCID?rev=$REV&expires=1798761600" \
--json '{
"type": "airport",
"country": "United Kingdom",
"icao": "EGOV",
"airportname": "Anglesey Airport",
"city": "Valley",
"faa": "VLY",
"tz": "Europe/London"
}'
If the revision number of the document matches, the response is a JSON object similar to the following.
{
"ok": true,
"id": "~SCH2oNtKFMBdcO-_sUhBmn",
"rev": "2-51cb776222e4a366e8961e1c9fc09edddc5b0fa1",
"expires": 1798761600
}
Delete a Document
The following request checks whether the specified document within the specified keyspace has the expected revision number. If so, it deletes the document.
This ensures that you’re deleting the latest revision of the document, in cases where the document may be updated by other processes concurrently.
curl -XDELETE --user $USER:$PASSWORD --cacert $CERT \
"https://$HOST:$PORT/travel-sample.inventory.airport/$DOCID?rev=$REV"
If the revision number of the document matches, the document is deleted. Edge Server returns a revision number for the deletion itself.
{
"ok": true,
"id": "~SCH2oNtKFMBdcO-_sUhBmn",
"rev": "3-b76bfea54ebb8e0337998d1ab591d9b19e238551"
}
If the revision number does not match, the request returns a 409 error.
If this happens, get the latest revision number for the document and try again.