Documents
On this page: The types of documents available in TerminusDB with examples of their definitions and interactions.

Key topics

The TerminusDB document store

TerminusDB is a document store as well as a knowledge graph database. TerminusDB schemata describe how to interpret segments of graphs as self-contained documents.

TerminusDB storage structure

The underlying storage structure of TerminusDB is a labeled, directed, and edge-labeled graph. Each source and target node has a distinct name, and every edge has a name and a direction.

Graph segments as documents

Segments of the graph are documents. Documents can be extracted as JSON objects, providing a convenient data package for applications. JSON objects can be updated by submitting modified versions. The graph's entire document segment can be deleted by deleting the document. However, the full graph structure is retained, allowing sophisticated search and traversal.

Subdocuments overview

Documents can contain subdocuments. A subdocument:
  • Is owned by its containing document.
  • Is a segment of the graph which is solely pointed to by the containing document.
  • Can have any number of outgoing links to other documents or subdocuments.
See the Subdocuments section for more information.

Simple documents

Documents are described by creating a class definition in the schema. The simplest document definition contains properties with data elements only.

A simple document example

The Football roster CSV file is loaded using the Football roster class definition below. This class definition describes the JSON document that can be submitted or retrieved from the graph.
A valid Football roster JSON document definition, which corresponds to a row in the CSV file, is also shown below.

Data: Football roster CSV file

1
name, position
2
George, Centre Back
3
Doug, Full Back
4
Karen, Centre Forward
Copied!

Code: Football roster class definition

1
{ "@type" : "@context",
2
"@schema" : "terminusdb://Roster/schema#",
3
"@base" : "terminusdb://Roster/document" }
4
5
{ "@type" : "Class",
6
"@id" : "Player",
7
"name" : "xsd:string",
8
"position": "xsd:string" }
Copied!

Code: Football roster JSON document definition

1
{ "@type" : "Player",
2
"@id" : "Player/George",
3
"@base" : "terminusdb://Roster/document",
4
"name" : "George",
5
"position": "Centre Back" }
Copied!

The JSON document definition

The following table describes the properties of the JSON document definition above.

Table: Properties of the JSON document definition

Property
Value
Description
@type
Player
The type of data held - a football player
@id
Player/George
The address of the document used for retrieval, update, or deletion, or as a reference used in other documents. If the address in @id is unambiguous, for example, when used in the same collection, then @id can be used as-is, i.e., as Player/George. Otherwise, the @base property is required.
@base
terminusdb://Roster/schema#
The fully qualified address for Player/George expanding to terminusdb://Roster/document/Player/George.

Unique document @id

It is important to ensure a unique document @id. The parameters @key and @base are available in the class definition, enabling you to use a calculated @id. Refer to the Schema reference for more information.

Documents with references

TerminusDB enables references to other documents, forming a graph of documents.

Create objects from data

The simple document example introduced above is convertible to an object that refers to each player, as demonstrated in the code-snippet Player and roster classes. In this snippet, the Roster class points to a Set of Players (or Player classes.)

The Set property

The Set property in the code-snippet is a type family allowing us to state that the player property can have any number of Players attached. Set does not provide ordering or multiplicity; a Player is either connected or not connected. For ordering and multiplicity, use @type List or Array.

Code: Player and roster classes

1
{ "@type" : "@context",
2
"@schema": "terminusdb://Roster/schema#",
3
"@base" : "terminusdb://Roster/document" }
4
5
{ "@type" : "Class",
6
"@id" : "Player",
7
"name" : "xsd:string",
8
"position": "xsd:string" }
9
10
{ "@type" : "Class",
11
"@id" : "Roster",
12
"player" : {
13
"@type" : "Set",
14
"@class": "Player" } }
Copied!

Create documents from class specifications

A set of documents that meets the above specification and that represents the data in the Football roster CSV file can be defined as:

Code: CSV roster data in document form

1
{ "@type" : "Roster",
2
"@id" : "Roster/Wolves",
3
"player": [ "Player/George", "Player/Karen", "Player/Doug" ] }
4
5
{ "@type" : "Player",
6
"@id" : "Player/George",
7
"name" : "George",
8
"position": "Centre Back" }
9
10
{ "@type" : "Player",
11
"@id" : "Player/Doug",
12
"name" : "Doug",
13
"position": "Full Back" }
14
15
{ "@type" : "Player",
16
"@id" : "Player/Karen",
17
"name" : "Karen",
18
"position": "Centre Forward" }
Copied!
The Roster points to the various Player documents. When requesting the document Roster/Wolves, each player in the player array is retrieved. Each identifier can be queried in a similar way to retrieve all associated documents.

Subdocuments

A subdocument can only be pointed to by its containing document. It is information internal to the identity of a document and not intended to be shared. This designation enables deeply nested JSON documents that are self-contained and retrievable using the TerminusDB document interface.

A subdocument example

In the subdocument example below, the schema specification defines players with a subdocument of stats. Stats is declared a subdocument using the @subdocument property and the special value []. Also, it has a Random key, meaning the key is automatically generated if not provided.

Code: An example of a player stats subdocument

1
{ "@type" : "@context",
2
"@schema": "terminusdb://Roster/schema#",
3
"@base" : "terminusdb://Roster/document" }
4
5
{ "@type" : "Class",
6
"@id" : "Stats",
7
"@subdocument": [],
8
"@key" : { "@type" : "Random" },
9
"strength" : "xsd:integer",
10
"intelligence": "xsd:integer",
11
"dexterity" : "xsd:integer",
12
"charisma" : "xsd:integer",
13
"wisdom" : "xsd:integer",
14
"constitution": "xsd:integer" }
15
16
{ "@type": "Class",
17
"@id" : "Player",
18
"name" : "xsd:string",
19
"stats": "Stats" }
Copied!
In the example below, the subdocument enables sending and retrieving Stats with the Player object. It is also possible for subdocuments to point recursively to other subdocuments or documents.

Code: Stats with the player object

1
{ "@type" : "Player",
2
"@id" : "Player/Hieronymous",
3
"stats" :
4
{
5
"@type" : "Stats",
6
"strength" : 14,
7
"intelligence": 10,
8
"dexterity" : 14,
9
"charisma" : 8,
10
"wisdom" : 12,
11
"constitution": 9
12
}
13
}
Copied!

Visualizing the Graph

Documents and subdocuments, and references to other documents, provide the best of both worlds: document storage and knowledge graphs. However, visualizing what this means requires a bit of experience.

A graph visualization example

The following Contact graph diagram illustrates the boundaries around what constitutes a document and a subdocument.
An example schema representing this scenario is provided in the code-snippet Contact graph schema further below with an example of a corresponding Contact graph document definition.
With a bit of practice, designing your knowledge graphs in TerminusDB will become second nature.

Diagram: Contact graph

Code: Contact graph schema

1
{ "@type" : "@context",
2
"@schema": "terminusdb://Roster/schema#",
3
"@base" : "terminusdb://Roster/document" }
4
5
{ "@type" : "Class",
6
"@id" : "Coordinate",
7
"@subdocument": [],
8
"@key" : { "@type" : "Random" },
9
"lat" : "xsd:decimal",
10
"long" : "xsd:decimal" }
11
12
{ "@type" : "Class",
13
"@id" : "Map",
14
"coordinates": { "@type" : "Array",
15
"@class": "Coordinate" } }
16
17
{ "@type": "Class",
18
"@id" : "Country",
19
"name" : "xsd:string",
20
"map" : "Map" }
21
22
{ "@type" : "Class",
23
"@id" : "Address",
24
"@subdocument": [],
25
"@key" : { "@type" : "Random" },
26
"country" : "Country",
27
"street" : "xsd:string" }
28
29
{ "@type" : "Class",
30
"@id" : "Person",
31
"name" : "xsd:string",
32
"address": "Address",
33
"friend" : "Person" }
Copied!

Code: Contact graph document

1
{ "@type" : "Person",
2
"@id" : "Person/Joe",
3
"name" : "Joe Bloggs",
4
"address": { "@type" : "Address",
5
"@id" : "Adress/aa1264e404a5b34381abc37cad83fabd",
6
"street" : "Elm St.",
7
"country": "Country/USA" },
8
"friend" : [ "Person/Jill" ] }
9
10
{ "@type" : "Person",
11
"@id" : "Person/Jill",
12
"name" : "Jill Smith",
13
"address": { "@type" : "Address",
14
"@id" : "Adress/5fba7438dc2b23258d304bb8cd1222bd",
15
"street" : "Main St.",
16
"country": "Country/Ireland" },
17
"friend" : [ "Person/Joe" ] }
18
19
{ "@type" : "Country",
20
"@id" : "Country/USA",
21
"name" : "USA",
22
"coordinates": [ ... ] }
23
24
{ "@type" : "Country",
25
"@id" : "Country/Ireland",
26
"name" : "Ireland",
27
"coordinates": [ ... ] }
Copied!

See also

TerminusDB document interface

The TerminusDB Document Interface Reference guide.

TerminusDB schema reference

The TerminusDB Schema Reference guide.
Last modified 4mo ago