Create new documents
Once you have created collections, you can start adding documents into them.
Display database setup
This page assumes your database contains Book and Person collections:
type Person {
name: String!
authoredBooks: [Book]
}
type Book {
title: String!
genre: String
plot: String
rating: Float
author: Person
}
Create one new document
- CLI
- HTTP API
- GraphQL API
To create documents of type <type>, use the mutation add_<type> via the CLI command defradb client query. For example, to create a document in the Book collection, use add_Book.
Every add_<type> mutation must return some of the inserted information. Because GraphQL queries only return the exact fields requested, you have to provide a list of return fields (there is no equivalent of the SQL SELECT * syntax).
defradb client query '
mutation {
add_Book(input: {
title: "Infinite Jest",
genre: "Fiction",
plot: "A gargantuan, mind-altering tragi-comedy about the Pursuit of Happiness in America.",
rating: 4.25
}) {
_docID
title
}
}
'
{
"data": {
"add_Book": [
{
"_docID": "bae-ad13cbb3-0970-5e1d-8096-620f7bd27d26",
"title": "Infinite Jest"
}
]
}
}
To create documents of type <type>, submit a POST request to the HTTP endpoint /api/v1/collections/<type>. For example, submit a request to /api/v1/collections/Book. The request body should contain the documents information in JSON format.
POST http://localhost:9181/api/v1/collections/Book HTTP/2
accept: application/json
content-type: application/json
{
"title": "Infinite Jest",
"genre": "Fiction",
"plot": "A gargantuan, mind-altering tragi-comedy about the Pursuit of Happiness in America.",
"rating": 4.25
}
The HTTP API doesn't support creating and returning documents in the same request. You need to query for documents separately.
To create documents of type <type>, use the mutation add_<type>. For example, to create a document in the Book collection, use add_Book.
Every add_<type> mutation must return some of the inserted information. Because GraphQL queries only return the exact fields requested, you have to provide a list of return fields (there is no equivalent of the SQL SELECT * syntax).
mutation {
add_Book(input: {
title: "Infinite Jest",
genre: "Fiction",
plot: "A gargantuan, mind-altering tragi-comedy about the Pursuit of Happiness in America.",
rating: 4.25
}) {
_docID
title
}
}
{
"data": {
"add_Book": [
{
"_docID": "bae-ad13cbb3-0970-5e1d-8096-620f7bd27d26",
"title": "Infinite Jest"
}
]
}
}
The document ID
The field _docID is the document's unique identifier, determined by the collection it belongs to and the data it is initialized with. The data in the document might change over time, but its docID will stay the same.
The _docID field is automatically indexed.
Create documents with relationships
To add documents with relationships to each other, you need two queries: one to create the documents, and another to link them together via their docIDs.
For example, to create a book and link it to an author, first create the documents and return their docIDs:
mutation {
add_Person(input: {
name: "Victor Hugo"
}) {
_docID
name
}
add_Book(input: {
title: "Les Misérables",
genre: "Fiction",
plot: "Victor Hugo's tale of injustice, heroism and love follows the fortunes of Jean Valjean, an escaped convict determined to put his criminal past behind him.",
rating: 4.21
}) {
_docID
title
}
}
{
"data": {
"add_Book": [
{
"_docID": "bae-e1dcefa0-46ca-5ae5-bc0a-859f2e7e1259",
"title": "Les Misérables"
}
],
"add_Person": [
{
"_docID": "bae-c169e917-df52-5603-9224-39c1757f1b04",
"name": "Victor Hugo"
}
]
}
}
To link them, use an update mutation to set the field _authorID on Book with the docID from the desired Person:
mutation {
update_Book(
docID: "bae-e1dcefa0-46ca-5ae5-bc0a-859f2e7e1259",
input: { # properties to be altered
_authorID: "bae-c169e917-df52-5603-9224-39c1757f1b04"
}
) {
_docID
_authorID
title
}
}
{
"data": {
"update_Book": [
{
"_authorID": "bae-c169e917-df52-5603-9224-39c1757f1b04",
"_docID": "bae-e1dcefa0-46ca-5ae5-bc0a-859f2e7e1259",
"title": "Les Misérables"
}
]
}
}
Create multiple documents at once
- CLI
- HTTP API
- GraphQL API
You can create multiple documents in the same request by concatenating several add_<type> statements.
To avoid clashes, you need to alias the results (b1 and b2 in the example). The aliases are used as keys in the result JSON.
defradb client query '
mutation {
b1:add_Book(input: {
title: "1984",
genre: "Fiction",
plot: "A masterpiece of rebellion and imprisonment where war is peace, freedom is slavery, and Big Brother is watching.",
rating: 4.20
}) {
_docID
title
}
b2:add_Book(input: {
title: "Lord of the Flies",
genre: "Fiction",
plot: "At the dawn of the next world war, a plane crashes on an uncharted island, stranding a group of schoolboys.",
rating: 3.70
}) {
_docID
title
}
}
'
{
"data": {
"b1": [
{
"_docID": "bae-87c50a88-e1de-5df9-9659-cfe52d10dc7a",
"title": "1984"
}
],
"b2": [
{
"_docID": "bae-7e637e18-7c15-5ed9-a5bf-aea644e5fcb6",
"title": "Lord of the Flies"
}
]
}
}
You can create multiple documents in the same request by providing a JSON list object to the /api/v1/collections/<type> endpoint.
POST http://localhost:9181/api/v1/collections/Book HTTP/2
accept: application/json
content-type: application/json
[
{
"title": "1984",
"genre": "Fiction",
"plot": "A masterpiece of rebellion and imprisonment where war is peace, freedom is slavery, and Big Brother is watching.",
"rating": 4.20
},
{
"title": "Lord of the Flies",
"genre": "Fiction",
"plot": "At the dawn of the next world war, a plane crashes on an uncharted island, stranding a group of schoolboys.",
"rating": 3.70
}
]
You can create multiple documents in the same request by concatenating several add_<type> statements.
To avoid clashes, you need to alias the results (b1 and b2 in the example). The aliases are used as keys in the result JSON.
mutation {
b1:add_Book(input: {
title: "1984",
genre: "Fiction",
plot: "A masterpiece of rebellion and imprisonment where war is peace, freedom is slavery, and Big Brother is watching.",
rating: 4.20
}) {
_docID
title
}
b2:add_Book(input: {
title: "Lord of the Flies",
genre: "Fiction",
plot: "At the dawn of the next world war, a plane crashes on an uncharted island, stranding a group of schoolboys.",
rating: 3.70
}) {
_docID
title
}
}
{
"data": {
"b1": [
{
"_docID": "bae-87c50a88-e1de-5df9-9659-cfe52d10dc7a",
"title": "1984"
}
],
"b2": [
{
"_docID": "bae-7e637e18-7c15-5ed9-a5bf-aea644e5fcb6",
"title": "Lord of the Flies"
}
]
}
}
JSON fields
JSON fields expect the value to be an unserialized object, not its string representation.
mutation {
add_jsonBlob(input: {
jsonField: {
i: {
love: "eating my family and commas"
}
}
}) { jsonField }
}
mutation {
add_jsonBlob(input: {
jsonField: "{\"i\": {\"love\": \"eating my family and commas\"}}"
}) { jsonField }
}