Get Documents

Open inAnthropic

Prerequisites

  • TerminusDB running locally (install guide)
  • A database with existing documents

What you'll achieve By the end of this guide, you will have retrieved documents from your TerminusDB database using the JavaScript client, the Python client, or the HTTP API.

Example database

Connect to TerminusDB first.

Get a single document

Retrieve a document by its ID:

Example: TypeScript
const getDoc = async () => {
  const doc = await client.getDocument({ id: "Player/Doug" })
  console.log("Player/Doug:", doc)
}

Returns:

Example: JSON
{
  "@id"   : "Player/Doug",
  "@type" : "Player",
  "name"    : "Doug",
  "position": "Full Back"
}

Get all documents

Retrieve every document in the database:

Example: TypeScript
const getDocs = async () => {
  const documents = await client.getDocument({ as_list: "true" })
  console.log("All documents:", documents)
}

Returns:

Example: JSON
[
  {
    "@id"   : "Player/Doug",
    "@type" : "Player",
    "name"    : "Doug",
    "position": "Full Back"
  },
  {
    "@id"   : "Player/George",
    "@type" : "Player",
    "name"    : "George",
    "position": "Center Back"
  },
  {
    "@id"   : "Player/Karen",
    "@type" : "Player",
    "name"    : "Karen",
    "position": "Center Forward"
  }
]

Counting documents

Available as of the 12.1 release candidates. Pass counted=true on the document listing endpoint to get the number of documents the listing would yield, without streaming them. The count includes subclasses when restricted by type, and follows the listing rules for subdocuments.

curl -u admin:root \
  "http://localhost:6363/api/document/admin/tdb-example-mydb/local/branch/main?type=Player&counted=true"
Example: JSON
{ "@type": "api:DocumentCountResponse", "api:count": 2 }

counted cannot be combined with id, ids or query; the server answers api:CountedNotSupportedForSelector.

Next steps

You've retrieved documents from TerminusDB. Next, you might want to:

Was this helpful?