Docs

Database

The database service allows you to create structured document collections, query and filter lists of documents and manage an advanced set of read and write access.

All the data in the database service is stored in JSON format. The service also allows you to nest child documents and use advanced filters to search and query the database just like you would with a classic graph database.

By leveraging the database permission management you can assign read or write access to the database documents for a specific user, team, user role or even grant public access to all visitors of your project. You can learn more about how Appwrite handles permissions and role access control.

List Documents

GET/database/{collectionId}

Get a list of all the user documents. You can use the query params to filter your results. On managed mode, this endpoint will return a list of all of the project documents. Learn more about different API modes.

Arguments

Name Type Description
collectionId required string

Collection unique ID.

filters optional array

Array of filter strings. Each filter is constructed from a key name, comparison operator (=, !=, >, <, <=, >=) and a value. You can also use a dot (.) separator in attribute names to filter by child document attributes. Examples: 'name=John Doe' or 'category.$uid>=5bed2d152c362'

offset optional integer

Offset value. Use this value to manage pagination.

limit optional integer

Maximum number of documents to return in response. Use this value to manage pagination.

order-field optional string

Document field that results will be sorted by.

order-type optional string

Order direction. Possible values are DESC for descending order, or ASC for ascending order.

order-cast optional string

Order field type casting. Possible values are int, string, date, time or datetime. The database will attempt to cast the order field to the value you pass here. The default value is a string.

search optional string

Search query. Enter any free text search. The database will try to find a match against all document attributes and children.

first optional integer

Return only first document. Pass 1 for true or 0 for false. The default value is 0.

last optional integer

Return only last document. Pass 1 for true or 0 for false. The default value is 0.

Example Request
  • let sdk = new Appwrite();
    
    sdk
        setProject('')
        setKey('')
    ;
    
    let promise = sdk.database.listDocuments('[COLLECTION_ID]');
    
    promise.then(function (response) {
        console.log(response);
    }, function (error) {
        console.log(error);
    });
  • <?php
    
    use Appwrite\Client;
    use Appwrite\Services\Database;
    
    $client = new Client();
    
    $client
        setProject('')
        setKey('')
    ;
    
    $database = new Database($client);
    
    $result = $database->listDocuments('[COLLECTION_ID]');

Get Document

GET/database/{collectionId}/{documentId}

Get document by its unique ID. This endpoint response returns a JSON object with the document data.

Arguments

Name Type Description
collectionId required string

Collection unique ID

documentId required string

Document unique ID

Example Request
  • let sdk = new Appwrite();
    
    sdk
        setProject('')
        setKey('')
    ;
    
    let promise = sdk.database.getDocument('[COLLECTION_ID]', '[DOCUMENT_ID]');
    
    promise.then(function (response) {
        console.log(response);
    }, function (error) {
        console.log(error);
    });
  • <?php
    
    use Appwrite\Client;
    use Appwrite\Services\Database;
    
    $client = new Client();
    
    $client
        setProject('')
        setKey('')
    ;
    
    $database = new Database($client);
    
    $result = $database->getDocument('[COLLECTION_ID]', '[DOCUMENT_ID]');

Create Document

POST/database/{collectionId}

Create a new Document.

Arguments

Name Type Description
collectionId required string

Collection unique ID.

data required string

Document data as JSON string.

parentDocument optional string

Parent document unique ID. Use when you want your new document to be a child of a parent document.

parentProperty optional string

Parent document property name. Use when you want your new document to be a child of a parent document.

parentPropertyType optional string

Parent document property connection type. You can set this value to assign, append or prepend, default value is assign. Use when you want your new document to be a child of a parent document.

Example Request
  • let sdk = new Appwrite();
    
    sdk
        setProject('')
        setKey('')
    ;
    
    let promise = sdk.database.createDocument('[COLLECTION_ID]', '{}');
    
    promise.then(function (response) {
        console.log(response);
    }, function (error) {
        console.log(error);
    });
  • <?php
    
    use Appwrite\Client;
    use Appwrite\Services\Database;
    
    $client = new Client();
    
    $client
        setProject('')
        setKey('')
    ;
    
    $database = new Database($client);
    
    $result = $database->createDocument('[COLLECTION_ID]', '{}');

Update Document

PATCH/database/{collectionId}/{documentId}

Arguments

Name Type Description
collectionId required string

Collection unique ID

documentId required string

Document unique ID

data required string

Document data as JSON string

Example Request
  • let sdk = new Appwrite();
    
    sdk
        setProject('')
        setKey('')
    ;
    
    let promise = sdk.database.updateDocument('[COLLECTION_ID]', '[DOCUMENT_ID]', '{}');
    
    promise.then(function (response) {
        console.log(response);
    }, function (error) {
        console.log(error);
    });
  • <?php
    
    use Appwrite\Client;
    use Appwrite\Services\Database;
    
    $client = new Client();
    
    $client
        setProject('')
        setKey('')
    ;
    
    $database = new Database($client);
    
    $result = $database->updateDocument('[COLLECTION_ID]', '[DOCUMENT_ID]', '{}');

Delete Document

DELETE/database/{collectionId}/{documentId}

Delete document by its unique ID. This endpoint deletes only the parent documents, his attributes and relations to other documents. Child documents will not be deleted.

Arguments

Name Type Description
collectionId required string

Collection unique ID

documentId required string

Document unique ID

Example Request
  • let sdk = new Appwrite();
    
    sdk
        setProject('')
        setKey('')
    ;
    
    let promise = sdk.database.deleteDocument('[COLLECTION_ID]', '[DOCUMENT_ID]');
    
    promise.then(function (response) {
        console.log(response);
    }, function (error) {
        console.log(error);
    });
  • <?php
    
    use Appwrite\Client;
    use Appwrite\Services\Database;
    
    $client = new Client();
    
    $client
        setProject('')
        setKey('')
    ;
    
    $database = new Database($client);
    
    $result = $database->deleteDocument('[COLLECTION_ID]', '[DOCUMENT_ID]');