Trabajando con datos
Insertar, leer, actualizar y borrar documentos.
Antes de empezar: esta guía usa la colección customers tal como la deja Primeros pasos, con Ada y Grace, y sus variables de entorno. Añade dos clientes temporales marcados con "trial": true y los borra al final, así que customers queda como estaba.
Insertar documentos
Las inserciones usan JSON Lines (JSONL): un objeto JSON por línea. Una petición puede llevar un documento o millones.
curl -X POST "https://inceptiondb.hola.cloud/v1/databases/$INCEPTIONDB_DATABASE/collections/customers:insert" \
-H "Api-Key: $INCEPTIONDB_API_KEY" \
-H "Api-Secret: $INCEPTIONDB_API_SECRET" \
-H "Content-Type: application/x-ndjson" \
--data-binary '{"id":"linus","name":"Linus Torvalds","plan":"free","country":"FI","mrr":0,"trial":true}
{"id":"barbara","name":"Barbara Liskov","plan":"team","country":"US","mrr":300,"trial":true}'POST /v1/databases/$INCEPTIONDB_DATABASE/collections/customers:insert HTTP/1.1
Host: inceptiondb.hola.cloud
Api-Key: $INCEPTIONDB_API_KEY
Api-Secret: $INCEPTIONDB_API_SECRET
Content-Type: application/x-ndjson
{"id":"linus","name":"Linus Torvalds","plan":"free","country":"FI","mrr":0,"trial":true}
{"id":"barbara","name":"Barbara Liskov","plan":"team","country":"US","mrr":300,"trial":true}// Go 1.21+, standard library only. Save as main.go and run: go run main.go
package main
import (
"fmt"
"io"
"net/http"
"os"
"strings"
)
func main() {
body := "{\"id\":\"linus\",\"name\":\"Linus Torvalds\",\"plan\":\"free\",\"country\":\"FI\",\"mrr\":0,\"trial\":true}\n{\"id\":\"barbara\",\"name\":\"Barbara Liskov\",\"plan\":\"team\",\"country\":\"US\",\"mrr\":300,\"trial\":true}"
req, err := http.NewRequest("POST", "https://inceptiondb.hola.cloud/v1/databases/" + os.Getenv("INCEPTIONDB_DATABASE") + "/collections/customers:insert", strings.NewReader(body))
if err != nil {
panic(err)
}
req.Header.Set("Api-Key", os.Getenv("INCEPTIONDB_API_KEY"))
req.Header.Set("Api-Secret", os.Getenv("INCEPTIONDB_API_SECRET"))
req.Header.Set("Content-Type", "application/x-ndjson")
resp, err := http.DefaultClient.Do(req)
if err != nil {
panic(err)
}
defer resp.Body.Close()
responseBody, err := io.ReadAll(resp.Body)
if err != nil {
panic(err)
}
fmt.Println(resp.Status)
fmt.Println(string(responseBody))
}
<?php
// PHP 8+ with the curl extension. Save as example.php and run: php example.php
$body = '{"id":"linus","name":"Linus Torvalds","plan":"free","country":"FI","mrr":0,"trial":true}\n{"id":"barbara","name":"Barbara Liskov","plan":"team","country":"US","mrr":300,"trial":true}';
$ch = curl_init();
curl_setopt_array($ch, [
CURLOPT_URL => 'https://inceptiondb.hola.cloud/v1/databases/' . getenv('INCEPTIONDB_DATABASE') . '/collections/customers:insert',
CURLOPT_RETURNTRANSFER => true,
CURLOPT_CUSTOMREQUEST => 'POST',
CURLOPT_POSTFIELDS => $body,
CURLOPT_HTTPHEADER => [
'Api-Key: ' . getenv('INCEPTIONDB_API_KEY'),
'Api-Secret: ' . getenv('INCEPTIONDB_API_SECRET'),
'Content-Type: application/x-ndjson',
],
]);
$response = curl_exec($ch);
if ($response === false) {
throw new Exception(curl_error($ch));
}
echo curl_getinfo($ch, CURLINFO_RESPONSE_CODE), "\n", $response, "\n";
curl_close($ch);
# Python 3.8+. Install: pip install requests. Save as example.py and run: python example.py
import os
import requests
headers = {
"Api-Key": os.environ["INCEPTIONDB_API_KEY"],
"Api-Secret": os.environ["INCEPTIONDB_API_SECRET"],
"Content-Type": "application/x-ndjson",
}
body = "{\"id\":\"linus\",\"name\":\"Linus Torvalds\",\"plan\":\"free\",\"country\":\"FI\",\"mrr\":0,\"trial\":true}\n{\"id\":\"barbara\",\"name\":\"Barbara Liskov\",\"plan\":\"team\",\"country\":\"US\",\"mrr\":300,\"trial\":true}"
response = requests.request(
"POST",
"https://inceptiondb.hola.cloud/v1/databases/" + os.environ["INCEPTIONDB_DATABASE"] + "/collections/customers:insert",
headers=headers,
data=body
)
print(response.status_code)
print(response.text)
// Node.js 18+. Save as example.mjs and run: node example.mjs
const body = "{\"id\":\"linus\",\"name\":\"Linus Torvalds\",\"plan\":\"free\",\"country\":\"FI\",\"mrr\":0,\"trial\":true}\n{\"id\":\"barbara\",\"name\":\"Barbara Liskov\",\"plan\":\"team\",\"country\":\"US\",\"mrr\":300,\"trial\":true}";
const response = await fetch("https://inceptiondb.hola.cloud/v1/databases/" + process.env.INCEPTIONDB_DATABASE + "/collections/customers:insert", {
method: "POST",
headers: {
"Api-Key": process.env.INCEPTIONDB_API_KEY,
"Api-Secret": process.env.INCEPTIONDB_API_SECRET,
"Content-Type": "application/x-ndjson"
},
body
});
console.log(response.status);
console.log(await response.text());
// Java 17+, standard library only. Save as Main.java and run: java Main.java
import java.net.URI;
import java.net.http.HttpClient;
import java.net.http.HttpRequest;
import java.net.http.HttpResponse;
public class Main {
public static void main(String[] args) throws Exception {
var body = """
{"id":"linus","name":"Linus Torvalds","plan":"free","country":"FI","mrr":0,"trial":true}
{"id":"barbara","name":"Barbara Liskov","plan":"team","country":"US","mrr":300,"trial":true}
""";
var request = HttpRequest.newBuilder()
.uri(URI.create("https://inceptiondb.hola.cloud/v1/databases/" + System.getenv("INCEPTIONDB_DATABASE") + "/collections/customers:insert"))
.method("POST", HttpRequest.BodyPublishers.ofString(body))
.header("Api-Key", System.getenv("INCEPTIONDB_API_KEY"))
.header("Api-Secret", System.getenv("INCEPTIONDB_API_SECRET"))
.header("Content-Type", "application/x-ndjson")
.build();
var response = HttpClient.newHttpClient().send(request, HttpResponse.BodyHandlers.ofString());
System.out.println(response.statusCode());
System.out.println(response.body());
}
}La respuesta (201 Created) contiene los documentos guardados, con los valores por defecto de la colección ya aplicados:
1{"id":"linus","name":"Linus Torvalds","plan":"free","country":"FI","mrr":0,"trial":true}
2{"id":"barbara","name":"Barbara Liskov","plan":"team","country":"US","mrr":300,"trial":true}
Un array JSON no es JSONL. Este cuerpo se rechaza con 400 Bad Request y el mensaje document must be a JSON object:
1[{"id":"linus"},{"id":"barbara"}]
Los mismos dos documentos en JSONL:
1{"id":"linus"}
2{"id":"barbara"}
Conviene saber:
- Sin
id, el documento recibe uno de los valores por defecto de la colección (un UUID aleatorio salvo que lo cambies). - InceptionDB no comprueba que los ids sean únicos: insertar dos veces el mismo
idguarda dos documentos. Para evitarlo, crea un índicemapsobreid. Entonces un id repetido responde409 Conflict, y las lecturas por id son instantáneas. - Los documentos se guardan en orden y la inserción se detiene en el primer documento rechazado. Los anteriores quedan guardados.
- Para cargar un archivo, usa
--data-binary @customers.jsonl.-dquitaría los saltos de línea. - Cada documento debe ser un objeto JSON sin nombres de campo repetidos. Si no, la respuesta es
400 Bad Request. - Si un índice único ya tiene el valor, la respuesta es
409 Conflict. - Insertar en una colección que no existe la crea. Comprueba el nombre de la colección en la URL.
Leer un documento por id
curl "https://inceptiondb.hola.cloud/v1/databases/$INCEPTIONDB_DATABASE/collections/customers/documents/linus" \
-H "Api-Key: $INCEPTIONDB_API_KEY" \
-H "Api-Secret: $INCEPTIONDB_API_SECRET"GET /v1/databases/$INCEPTIONDB_DATABASE/collections/customers/documents/linus HTTP/1.1
Host: inceptiondb.hola.cloud
Api-Key: $INCEPTIONDB_API_KEY
Api-Secret: $INCEPTIONDB_API_SECRET
// Go 1.21+, standard library only. Save as main.go and run: go run main.go
package main
import (
"fmt"
"io"
"net/http"
"os"
)
func main() {
req, err := http.NewRequest("GET", "https://inceptiondb.hola.cloud/v1/databases/" + os.Getenv("INCEPTIONDB_DATABASE") + "/collections/customers/documents/linus", nil)
if err != nil {
panic(err)
}
req.Header.Set("Api-Key", os.Getenv("INCEPTIONDB_API_KEY"))
req.Header.Set("Api-Secret", os.Getenv("INCEPTIONDB_API_SECRET"))
resp, err := http.DefaultClient.Do(req)
if err != nil {
panic(err)
}
defer resp.Body.Close()
responseBody, err := io.ReadAll(resp.Body)
if err != nil {
panic(err)
}
fmt.Println(resp.Status)
fmt.Println(string(responseBody))
}
<?php
// PHP 8+ with the curl extension. Save as example.php and run: php example.php
$ch = curl_init();
curl_setopt_array($ch, [
CURLOPT_URL => 'https://inceptiondb.hola.cloud/v1/databases/' . getenv('INCEPTIONDB_DATABASE') . '/collections/customers/documents/linus',
CURLOPT_RETURNTRANSFER => true,
CURLOPT_CUSTOMREQUEST => 'GET',
CURLOPT_HTTPHEADER => [
'Api-Key: ' . getenv('INCEPTIONDB_API_KEY'),
'Api-Secret: ' . getenv('INCEPTIONDB_API_SECRET'),
],
]);
$response = curl_exec($ch);
if ($response === false) {
throw new Exception(curl_error($ch));
}
echo curl_getinfo($ch, CURLINFO_RESPONSE_CODE), "\n", $response, "\n";
curl_close($ch);
# Python 3.8+. Install: pip install requests. Save as example.py and run: python example.py
import os
import requests
headers = {
"Api-Key": os.environ["INCEPTIONDB_API_KEY"],
"Api-Secret": os.environ["INCEPTIONDB_API_SECRET"],
}
response = requests.request(
"GET",
"https://inceptiondb.hola.cloud/v1/databases/" + os.environ["INCEPTIONDB_DATABASE"] + "/collections/customers/documents/linus",
headers=headers
)
print(response.status_code)
print(response.text)
// Node.js 18+. Save as example.mjs and run: node example.mjs
const response = await fetch("https://inceptiondb.hola.cloud/v1/databases/" + process.env.INCEPTIONDB_DATABASE + "/collections/customers/documents/linus", {
method: "GET",
headers: {
"Api-Key": process.env.INCEPTIONDB_API_KEY,
"Api-Secret": process.env.INCEPTIONDB_API_SECRET
}
});
console.log(response.status);
console.log(await response.text());
// Java 17+, standard library only. Save as Main.java and run: java Main.java
import java.net.URI;
import java.net.http.HttpClient;
import java.net.http.HttpRequest;
import java.net.http.HttpResponse;
public class Main {
public static void main(String[] args) throws Exception {
var request = HttpRequest.newBuilder()
.uri(URI.create("https://inceptiondb.hola.cloud/v1/databases/" + System.getenv("INCEPTIONDB_DATABASE") + "/collections/customers/documents/linus"))
.method("GET", HttpRequest.BodyPublishers.noBody())
.header("Api-Key", System.getenv("INCEPTIONDB_API_KEY"))
.header("Api-Secret", System.getenv("INCEPTIONDB_API_SECRET"))
.build();
var response = HttpClient.newHttpClient().send(request, HttpResponse.BodyHandlers.ofString());
System.out.println(response.statusCode());
System.out.println(response.body());
}
}1{"id":"linus","document":{"country":"FI","id":"linus","mrr":0,"name":"Linus Torvalds","plan":"free","trial":true},"source":{"type":"fullscan"}}
source indica cómo se encontró el documento: index cuando respondió un índice sobre id, fullscan cuando se recorrió la colección. Un documento que no existe responde 404 Not Found.
Para leer muchos documentos a la vez, usa una consulta.
Actualizar documentos
Un patch cambia solo los campos que envías (un JSON merge patch): los campos nuevos se añaden, los existentes se sustituyen y null elimina un campo.
curl -X POST "https://inceptiondb.hola.cloud/v1/databases/$INCEPTIONDB_DATABASE/collections/customers:patch" \
-H "Api-Key: $INCEPTIONDB_API_KEY" \
-H "Api-Secret: $INCEPTIONDB_API_SECRET" \
-H "Content-Type: application/json" \
-d '{"filter": {"id": "linus"}, "patch": {"plan": "pro", "mrr": 49, "country": null}}'POST /v1/databases/$INCEPTIONDB_DATABASE/collections/customers:patch HTTP/1.1
Host: inceptiondb.hola.cloud
Api-Key: $INCEPTIONDB_API_KEY
Api-Secret: $INCEPTIONDB_API_SECRET
Content-Type: application/json
{"filter": {"id": "linus"}, "patch": {"plan": "pro", "mrr": 49, "country": null}}// Go 1.21+, standard library only. Save as main.go and run: go run main.go
package main
import (
"fmt"
"io"
"net/http"
"os"
"encoding/json"
"strings"
)
func main() {
payload := map[string]any{"filter": map[string]any{"id": "linus"}, "patch": map[string]any{"country": nil, "mrr": 49, "plan": "pro"}}
bodyBytes, err := json.Marshal(payload)
if err != nil {
panic(err)
}
body := string(bodyBytes)
req, err := http.NewRequest("POST", "https://inceptiondb.hola.cloud/v1/databases/" + os.Getenv("INCEPTIONDB_DATABASE") + "/collections/customers:patch", strings.NewReader(body))
if err != nil {
panic(err)
}
req.Header.Set("Api-Key", os.Getenv("INCEPTIONDB_API_KEY"))
req.Header.Set("Api-Secret", os.Getenv("INCEPTIONDB_API_SECRET"))
req.Header.Set("Content-Type", "application/json")
resp, err := http.DefaultClient.Do(req)
if err != nil {
panic(err)
}
defer resp.Body.Close()
responseBody, err := io.ReadAll(resp.Body)
if err != nil {
panic(err)
}
fmt.Println(resp.Status)
fmt.Println(string(responseBody))
}
<?php
// PHP 8+ with the curl extension. Save as example.php and run: php example.php
$payload = ['filter' => ['id' => 'linus'], 'patch' => ['country' => null, 'mrr' => 49, 'plan' => 'pro']];
$body = json_encode($payload);
$ch = curl_init();
curl_setopt_array($ch, [
CURLOPT_URL => 'https://inceptiondb.hola.cloud/v1/databases/' . getenv('INCEPTIONDB_DATABASE') . '/collections/customers:patch',
CURLOPT_RETURNTRANSFER => true,
CURLOPT_CUSTOMREQUEST => 'POST',
CURLOPT_POSTFIELDS => $body,
CURLOPT_HTTPHEADER => [
'Api-Key: ' . getenv('INCEPTIONDB_API_KEY'),
'Api-Secret: ' . getenv('INCEPTIONDB_API_SECRET'),
'Content-Type: application/json',
],
]);
$response = curl_exec($ch);
if ($response === false) {
throw new Exception(curl_error($ch));
}
echo curl_getinfo($ch, CURLINFO_RESPONSE_CODE), "\n", $response, "\n";
curl_close($ch);
# Python 3.8+. Install: pip install requests. Save as example.py and run: python example.py
import os
import requests
headers = {
"Api-Key": os.environ["INCEPTIONDB_API_KEY"],
"Api-Secret": os.environ["INCEPTIONDB_API_SECRET"],
"Content-Type": "application/json",
}
payload = {"filter": {"id": "linus"}, "patch": {"country": None, "mrr": 49, "plan": "pro"}}
response = requests.request(
"POST",
"https://inceptiondb.hola.cloud/v1/databases/" + os.environ["INCEPTIONDB_DATABASE"] + "/collections/customers:patch",
headers=headers,
json=payload
)
print(response.status_code)
print(response.text)
// Node.js 18+. Save as example.mjs and run: node example.mjs
const payload = {"filter": {"id": "linus"}, "patch": {"country": null, "mrr": 49, "plan": "pro"}};
const response = await fetch("https://inceptiondb.hola.cloud/v1/databases/" + process.env.INCEPTIONDB_DATABASE + "/collections/customers:patch", {
method: "POST",
headers: {
"Api-Key": process.env.INCEPTIONDB_API_KEY,
"Api-Secret": process.env.INCEPTIONDB_API_SECRET,
"Content-Type": "application/json"
},
body: JSON.stringify(payload)
});
console.log(response.status);
console.log(await response.text());
// Java 17+, standard library only. Save as Main.java and run: java Main.java
import java.net.URI;
import java.net.http.HttpClient;
import java.net.http.HttpRequest;
import java.net.http.HttpResponse;
public class Main {
public static void main(String[] args) throws Exception {
var body = """
{"filter": {"id": "linus"}, "patch": {"plan": "pro", "mrr": 49, "country": null}}
""";
var request = HttpRequest.newBuilder()
.uri(URI.create("https://inceptiondb.hola.cloud/v1/databases/" + System.getenv("INCEPTIONDB_DATABASE") + "/collections/customers:patch"))
.method("POST", HttpRequest.BodyPublishers.ofString(body))
.header("Api-Key", System.getenv("INCEPTIONDB_API_KEY"))
.header("Api-Secret", System.getenv("INCEPTIONDB_API_SECRET"))
.header("Content-Type", "application/json")
.build();
var response = HttpClient.newHttpClient().send(request, HttpResponse.BodyHandlers.ofString());
System.out.println(response.statusCode());
System.out.println(response.body());
}
}La respuesta contiene los documentos tal como quedan después del cambio:
1{"id":"linus","name":"Linus Torvalds","plan":"pro","mrr":49,"trial":true}
filter elige los documentos, con la misma sintaxis que las consultas. Igual que las consultas, un patch cambia un solo documento salvo que indiques limit. Marca a todos los clientes de prueba con priority:
curl -X POST "https://inceptiondb.hola.cloud/v1/databases/$INCEPTIONDB_DATABASE/collections/customers:patch" \
-H "Api-Key: $INCEPTIONDB_API_KEY" \
-H "Api-Secret: $INCEPTIONDB_API_SECRET" \
-H "Content-Type: application/json" \
-d '{"filter": {"trial": true}, "patch": {"priority": true}, "limit": -1}'POST /v1/databases/$INCEPTIONDB_DATABASE/collections/customers:patch HTTP/1.1
Host: inceptiondb.hola.cloud
Api-Key: $INCEPTIONDB_API_KEY
Api-Secret: $INCEPTIONDB_API_SECRET
Content-Type: application/json
{"filter": {"trial": true}, "patch": {"priority": true}, "limit": -1}// Go 1.21+, standard library only. Save as main.go and run: go run main.go
package main
import (
"fmt"
"io"
"net/http"
"os"
"encoding/json"
"strings"
)
func main() {
payload := map[string]any{"filter": map[string]any{"trial": true}, "limit": -1, "patch": map[string]any{"priority": true}}
bodyBytes, err := json.Marshal(payload)
if err != nil {
panic(err)
}
body := string(bodyBytes)
req, err := http.NewRequest("POST", "https://inceptiondb.hola.cloud/v1/databases/" + os.Getenv("INCEPTIONDB_DATABASE") + "/collections/customers:patch", strings.NewReader(body))
if err != nil {
panic(err)
}
req.Header.Set("Api-Key", os.Getenv("INCEPTIONDB_API_KEY"))
req.Header.Set("Api-Secret", os.Getenv("INCEPTIONDB_API_SECRET"))
req.Header.Set("Content-Type", "application/json")
resp, err := http.DefaultClient.Do(req)
if err != nil {
panic(err)
}
defer resp.Body.Close()
responseBody, err := io.ReadAll(resp.Body)
if err != nil {
panic(err)
}
fmt.Println(resp.Status)
fmt.Println(string(responseBody))
}
<?php
// PHP 8+ with the curl extension. Save as example.php and run: php example.php
$payload = ['filter' => ['trial' => true], 'limit' => -1, 'patch' => ['priority' => true]];
$body = json_encode($payload);
$ch = curl_init();
curl_setopt_array($ch, [
CURLOPT_URL => 'https://inceptiondb.hola.cloud/v1/databases/' . getenv('INCEPTIONDB_DATABASE') . '/collections/customers:patch',
CURLOPT_RETURNTRANSFER => true,
CURLOPT_CUSTOMREQUEST => 'POST',
CURLOPT_POSTFIELDS => $body,
CURLOPT_HTTPHEADER => [
'Api-Key: ' . getenv('INCEPTIONDB_API_KEY'),
'Api-Secret: ' . getenv('INCEPTIONDB_API_SECRET'),
'Content-Type: application/json',
],
]);
$response = curl_exec($ch);
if ($response === false) {
throw new Exception(curl_error($ch));
}
echo curl_getinfo($ch, CURLINFO_RESPONSE_CODE), "\n", $response, "\n";
curl_close($ch);
# Python 3.8+. Install: pip install requests. Save as example.py and run: python example.py
import os
import requests
headers = {
"Api-Key": os.environ["INCEPTIONDB_API_KEY"],
"Api-Secret": os.environ["INCEPTIONDB_API_SECRET"],
"Content-Type": "application/json",
}
payload = {"filter": {"trial": True}, "limit": -1, "patch": {"priority": True}}
response = requests.request(
"POST",
"https://inceptiondb.hola.cloud/v1/databases/" + os.environ["INCEPTIONDB_DATABASE"] + "/collections/customers:patch",
headers=headers,
json=payload
)
print(response.status_code)
print(response.text)
// Node.js 18+. Save as example.mjs and run: node example.mjs
const payload = {"filter": {"trial": true}, "limit": -1, "patch": {"priority": true}};
const response = await fetch("https://inceptiondb.hola.cloud/v1/databases/" + process.env.INCEPTIONDB_DATABASE + "/collections/customers:patch", {
method: "POST",
headers: {
"Api-Key": process.env.INCEPTIONDB_API_KEY,
"Api-Secret": process.env.INCEPTIONDB_API_SECRET,
"Content-Type": "application/json"
},
body: JSON.stringify(payload)
});
console.log(response.status);
console.log(await response.text());
// Java 17+, standard library only. Save as Main.java and run: java Main.java
import java.net.URI;
import java.net.http.HttpClient;
import java.net.http.HttpRequest;
import java.net.http.HttpResponse;
public class Main {
public static void main(String[] args) throws Exception {
var body = """
{"filter": {"trial": true}, "patch": {"priority": true}, "limit": -1}
""";
var request = HttpRequest.newBuilder()
.uri(URI.create("https://inceptiondb.hola.cloud/v1/databases/" + System.getenv("INCEPTIONDB_DATABASE") + "/collections/customers:patch"))
.method("POST", HttpRequest.BodyPublishers.ofString(body))
.header("Api-Key", System.getenv("INCEPTIONDB_API_KEY"))
.header("Api-Secret", System.getenv("INCEPTIONDB_API_SECRET"))
.header("Content-Type", "application/json")
.build();
var response = HttpClient.newHttpClient().send(request, HttpResponse.BodyHandlers.ofString());
System.out.println(response.statusCode());
System.out.println(response.body());
}
}1{"id":"linus","name":"Linus Torvalds","plan":"pro","mrr":49,"trial":true,"priority":true}
2{"id":"barbara","name":"Barbara Liskov","plan":"team","country":"US","mrr":300,"trial":true,"priority":true}
"limit": -1 significa todos los documentos que coinciden. Si nada coincide, la respuesta es 204 No Content.
Un patch también puede cambiar el id de un documento. Si la colección tiene un índice map sobre id, un patch a un id que ya existe no se aplica.
Borrar documentos
curl -X POST "https://inceptiondb.hola.cloud/v1/databases/$INCEPTIONDB_DATABASE/collections/customers:remove" \
-H "Api-Key: $INCEPTIONDB_API_KEY" \
-H "Api-Secret: $INCEPTIONDB_API_SECRET" \
-H "Content-Type: application/json" \
-d '{"filter": {"id": "linus"}}'POST /v1/databases/$INCEPTIONDB_DATABASE/collections/customers:remove HTTP/1.1
Host: inceptiondb.hola.cloud
Api-Key: $INCEPTIONDB_API_KEY
Api-Secret: $INCEPTIONDB_API_SECRET
Content-Type: application/json
{"filter": {"id": "linus"}}// Go 1.21+, standard library only. Save as main.go and run: go run main.go
package main
import (
"fmt"
"io"
"net/http"
"os"
"encoding/json"
"strings"
)
func main() {
payload := map[string]any{"filter": map[string]any{"id": "linus"}}
bodyBytes, err := json.Marshal(payload)
if err != nil {
panic(err)
}
body := string(bodyBytes)
req, err := http.NewRequest("POST", "https://inceptiondb.hola.cloud/v1/databases/" + os.Getenv("INCEPTIONDB_DATABASE") + "/collections/customers:remove", strings.NewReader(body))
if err != nil {
panic(err)
}
req.Header.Set("Api-Key", os.Getenv("INCEPTIONDB_API_KEY"))
req.Header.Set("Api-Secret", os.Getenv("INCEPTIONDB_API_SECRET"))
req.Header.Set("Content-Type", "application/json")
resp, err := http.DefaultClient.Do(req)
if err != nil {
panic(err)
}
defer resp.Body.Close()
responseBody, err := io.ReadAll(resp.Body)
if err != nil {
panic(err)
}
fmt.Println(resp.Status)
fmt.Println(string(responseBody))
}
<?php
// PHP 8+ with the curl extension. Save as example.php and run: php example.php
$payload = ['filter' => ['id' => 'linus']];
$body = json_encode($payload);
$ch = curl_init();
curl_setopt_array($ch, [
CURLOPT_URL => 'https://inceptiondb.hola.cloud/v1/databases/' . getenv('INCEPTIONDB_DATABASE') . '/collections/customers:remove',
CURLOPT_RETURNTRANSFER => true,
CURLOPT_CUSTOMREQUEST => 'POST',
CURLOPT_POSTFIELDS => $body,
CURLOPT_HTTPHEADER => [
'Api-Key: ' . getenv('INCEPTIONDB_API_KEY'),
'Api-Secret: ' . getenv('INCEPTIONDB_API_SECRET'),
'Content-Type: application/json',
],
]);
$response = curl_exec($ch);
if ($response === false) {
throw new Exception(curl_error($ch));
}
echo curl_getinfo($ch, CURLINFO_RESPONSE_CODE), "\n", $response, "\n";
curl_close($ch);
# Python 3.8+. Install: pip install requests. Save as example.py and run: python example.py
import os
import requests
headers = {
"Api-Key": os.environ["INCEPTIONDB_API_KEY"],
"Api-Secret": os.environ["INCEPTIONDB_API_SECRET"],
"Content-Type": "application/json",
}
payload = {"filter": {"id": "linus"}}
response = requests.request(
"POST",
"https://inceptiondb.hola.cloud/v1/databases/" + os.environ["INCEPTIONDB_DATABASE"] + "/collections/customers:remove",
headers=headers,
json=payload
)
print(response.status_code)
print(response.text)
// Node.js 18+. Save as example.mjs and run: node example.mjs
const payload = {"filter": {"id": "linus"}};
const response = await fetch("https://inceptiondb.hola.cloud/v1/databases/" + process.env.INCEPTIONDB_DATABASE + "/collections/customers:remove", {
method: "POST",
headers: {
"Api-Key": process.env.INCEPTIONDB_API_KEY,
"Api-Secret": process.env.INCEPTIONDB_API_SECRET,
"Content-Type": "application/json"
},
body: JSON.stringify(payload)
});
console.log(response.status);
console.log(await response.text());
// Java 17+, standard library only. Save as Main.java and run: java Main.java
import java.net.URI;
import java.net.http.HttpClient;
import java.net.http.HttpRequest;
import java.net.http.HttpResponse;
public class Main {
public static void main(String[] args) throws Exception {
var body = """
{"filter": {"id": "linus"}}
""";
var request = HttpRequest.newBuilder()
.uri(URI.create("https://inceptiondb.hola.cloud/v1/databases/" + System.getenv("INCEPTIONDB_DATABASE") + "/collections/customers:remove"))
.method("POST", HttpRequest.BodyPublishers.ofString(body))
.header("Api-Key", System.getenv("INCEPTIONDB_API_KEY"))
.header("Api-Secret", System.getenv("INCEPTIONDB_API_SECRET"))
.header("Content-Type", "application/json")
.build();
var response = HttpClient.newHttpClient().send(request, HttpResponse.BodyHandlers.ofString());
System.out.println(response.statusCode());
System.out.println(response.body());
}
}La respuesta contiene los documentos borrados:
1{"id":"linus","name":"Linus Torvalds","plan":"pro","mrr":49,"trial":true,"priority":true}
Borra un solo documento salvo que indiques limit. Borra todos los clientes de prueba, lo que además limpia esta guía:
curl -X POST "https://inceptiondb.hola.cloud/v1/databases/$INCEPTIONDB_DATABASE/collections/customers:remove" \
-H "Api-Key: $INCEPTIONDB_API_KEY" \
-H "Api-Secret: $INCEPTIONDB_API_SECRET" \
-H "Content-Type: application/json" \
-d '{"filter": {"trial": true}, "limit": -1}'POST /v1/databases/$INCEPTIONDB_DATABASE/collections/customers:remove HTTP/1.1
Host: inceptiondb.hola.cloud
Api-Key: $INCEPTIONDB_API_KEY
Api-Secret: $INCEPTIONDB_API_SECRET
Content-Type: application/json
{"filter": {"trial": true}, "limit": -1}// Go 1.21+, standard library only. Save as main.go and run: go run main.go
package main
import (
"fmt"
"io"
"net/http"
"os"
"encoding/json"
"strings"
)
func main() {
payload := map[string]any{"filter": map[string]any{"trial": true}, "limit": -1}
bodyBytes, err := json.Marshal(payload)
if err != nil {
panic(err)
}
body := string(bodyBytes)
req, err := http.NewRequest("POST", "https://inceptiondb.hola.cloud/v1/databases/" + os.Getenv("INCEPTIONDB_DATABASE") + "/collections/customers:remove", strings.NewReader(body))
if err != nil {
panic(err)
}
req.Header.Set("Api-Key", os.Getenv("INCEPTIONDB_API_KEY"))
req.Header.Set("Api-Secret", os.Getenv("INCEPTIONDB_API_SECRET"))
req.Header.Set("Content-Type", "application/json")
resp, err := http.DefaultClient.Do(req)
if err != nil {
panic(err)
}
defer resp.Body.Close()
responseBody, err := io.ReadAll(resp.Body)
if err != nil {
panic(err)
}
fmt.Println(resp.Status)
fmt.Println(string(responseBody))
}
<?php
// PHP 8+ with the curl extension. Save as example.php and run: php example.php
$payload = ['filter' => ['trial' => true], 'limit' => -1];
$body = json_encode($payload);
$ch = curl_init();
curl_setopt_array($ch, [
CURLOPT_URL => 'https://inceptiondb.hola.cloud/v1/databases/' . getenv('INCEPTIONDB_DATABASE') . '/collections/customers:remove',
CURLOPT_RETURNTRANSFER => true,
CURLOPT_CUSTOMREQUEST => 'POST',
CURLOPT_POSTFIELDS => $body,
CURLOPT_HTTPHEADER => [
'Api-Key: ' . getenv('INCEPTIONDB_API_KEY'),
'Api-Secret: ' . getenv('INCEPTIONDB_API_SECRET'),
'Content-Type: application/json',
],
]);
$response = curl_exec($ch);
if ($response === false) {
throw new Exception(curl_error($ch));
}
echo curl_getinfo($ch, CURLINFO_RESPONSE_CODE), "\n", $response, "\n";
curl_close($ch);
# Python 3.8+. Install: pip install requests. Save as example.py and run: python example.py
import os
import requests
headers = {
"Api-Key": os.environ["INCEPTIONDB_API_KEY"],
"Api-Secret": os.environ["INCEPTIONDB_API_SECRET"],
"Content-Type": "application/json",
}
payload = {"filter": {"trial": True}, "limit": -1}
response = requests.request(
"POST",
"https://inceptiondb.hola.cloud/v1/databases/" + os.environ["INCEPTIONDB_DATABASE"] + "/collections/customers:remove",
headers=headers,
json=payload
)
print(response.status_code)
print(response.text)
// Node.js 18+. Save as example.mjs and run: node example.mjs
const payload = {"filter": {"trial": true}, "limit": -1};
const response = await fetch("https://inceptiondb.hola.cloud/v1/databases/" + process.env.INCEPTIONDB_DATABASE + "/collections/customers:remove", {
method: "POST",
headers: {
"Api-Key": process.env.INCEPTIONDB_API_KEY,
"Api-Secret": process.env.INCEPTIONDB_API_SECRET,
"Content-Type": "application/json"
},
body: JSON.stringify(payload)
});
console.log(response.status);
console.log(await response.text());
// Java 17+, standard library only. Save as Main.java and run: java Main.java
import java.net.URI;
import java.net.http.HttpClient;
import java.net.http.HttpRequest;
import java.net.http.HttpResponse;
public class Main {
public static void main(String[] args) throws Exception {
var body = """
{"filter": {"trial": true}, "limit": -1}
""";
var request = HttpRequest.newBuilder()
.uri(URI.create("https://inceptiondb.hola.cloud/v1/databases/" + System.getenv("INCEPTIONDB_DATABASE") + "/collections/customers:remove"))
.method("POST", HttpRequest.BodyPublishers.ofString(body))
.header("Api-Key", System.getenv("INCEPTIONDB_API_KEY"))
.header("Api-Secret", System.getenv("INCEPTIONDB_API_SECRET"))
.header("Content-Type", "application/json")
.build();
var response = HttpClient.newHttpClient().send(request, HttpResponse.BodyHandlers.ofString());
System.out.println(response.statusCode());
System.out.println(response.body());
}
}1{"id":"barbara","name":"Barbara Liskov","plan":"team","country":"US","mrr":300,"trial":true,"priority":true}
Los documentos borrados no se pueden recuperar. Ejecuta antes el mismo filtro con :find para ver qué coincide.
Exportar una colección
:find con "limit": -1 devuelve todos los documentos en JSONL, listos para guardarlos en un archivo o insertarlos en otro sitio:
curl -X POST "https://inceptiondb.hola.cloud/v1/databases/$INCEPTIONDB_DATABASE/collections/customers:find" \
-H "Api-Key: $INCEPTIONDB_API_KEY" \
-H "Api-Secret: $INCEPTIONDB_API_SECRET" \
-H "Content-Type: application/json" \
-d '{"limit": -1}' \
-o customers.jsonlPOST /v1/databases/$INCEPTIONDB_DATABASE/collections/customers:find HTTP/1.1
Host: inceptiondb.hola.cloud
Api-Key: $INCEPTIONDB_API_KEY
Api-Secret: $INCEPTIONDB_API_SECRET
Content-Type: application/json
{"limit": -1}// Go 1.21+, standard library only. Save as main.go and run: go run main.go
package main
import (
"fmt"
"io"
"net/http"
"os"
"encoding/json"
"strings"
)
func main() {
payload := map[string]any{"limit": -1}
bodyBytes, err := json.Marshal(payload)
if err != nil {
panic(err)
}
body := string(bodyBytes)
req, err := http.NewRequest("POST", "https://inceptiondb.hola.cloud/v1/databases/" + os.Getenv("INCEPTIONDB_DATABASE") + "/collections/customers:find", strings.NewReader(body))
if err != nil {
panic(err)
}
req.Header.Set("Api-Key", os.Getenv("INCEPTIONDB_API_KEY"))
req.Header.Set("Api-Secret", os.Getenv("INCEPTIONDB_API_SECRET"))
req.Header.Set("Content-Type", "application/json")
resp, err := http.DefaultClient.Do(req)
if err != nil {
panic(err)
}
defer resp.Body.Close()
responseBody, err := io.ReadAll(resp.Body)
if err != nil {
panic(err)
}
fmt.Println(resp.Status)
fmt.Println(string(responseBody))
}
<?php
// PHP 8+ with the curl extension. Save as example.php and run: php example.php
$payload = ['limit' => -1];
$body = json_encode($payload);
$ch = curl_init();
curl_setopt_array($ch, [
CURLOPT_URL => 'https://inceptiondb.hola.cloud/v1/databases/' . getenv('INCEPTIONDB_DATABASE') . '/collections/customers:find',
CURLOPT_RETURNTRANSFER => true,
CURLOPT_CUSTOMREQUEST => 'POST',
CURLOPT_POSTFIELDS => $body,
CURLOPT_HTTPHEADER => [
'Api-Key: ' . getenv('INCEPTIONDB_API_KEY'),
'Api-Secret: ' . getenv('INCEPTIONDB_API_SECRET'),
'Content-Type: application/json',
],
]);
$response = curl_exec($ch);
if ($response === false) {
throw new Exception(curl_error($ch));
}
echo curl_getinfo($ch, CURLINFO_RESPONSE_CODE), "\n", $response, "\n";
curl_close($ch);
# Python 3.8+. Install: pip install requests. Save as example.py and run: python example.py
import os
import requests
headers = {
"Api-Key": os.environ["INCEPTIONDB_API_KEY"],
"Api-Secret": os.environ["INCEPTIONDB_API_SECRET"],
"Content-Type": "application/json",
}
payload = {"limit": -1}
response = requests.request(
"POST",
"https://inceptiondb.hola.cloud/v1/databases/" + os.environ["INCEPTIONDB_DATABASE"] + "/collections/customers:find",
headers=headers,
json=payload
)
print(response.status_code)
print(response.text)
// Node.js 18+. Save as example.mjs and run: node example.mjs
const payload = {"limit": -1};
const response = await fetch("https://inceptiondb.hola.cloud/v1/databases/" + process.env.INCEPTIONDB_DATABASE + "/collections/customers:find", {
method: "POST",
headers: {
"Api-Key": process.env.INCEPTIONDB_API_KEY,
"Api-Secret": process.env.INCEPTIONDB_API_SECRET,
"Content-Type": "application/json"
},
body: JSON.stringify(payload)
});
console.log(response.status);
console.log(await response.text());
// Java 17+, standard library only. Save as Main.java and run: java Main.java
import java.net.URI;
import java.net.http.HttpClient;
import java.net.http.HttpRequest;
import java.net.http.HttpResponse;
public class Main {
public static void main(String[] args) throws Exception {
var body = """
{"limit": -1}
""";
var request = HttpRequest.newBuilder()
.uri(URI.create("https://inceptiondb.hola.cloud/v1/databases/" + System.getenv("INCEPTIONDB_DATABASE") + "/collections/customers:find"))
.method("POST", HttpRequest.BodyPublishers.ofString(body))
.header("Api-Key", System.getenv("INCEPTIONDB_API_KEY"))
.header("Api-Secret", System.getenv("INCEPTIONDB_API_SECRET"))
.header("Content-Type", "application/json")
.build();
var response = HttpClient.newHttpClient().send(request, HttpResponse.BodyHandlers.ofString());
System.out.println(response.statusCode());
System.out.println(response.body());
}
}customers.jsonl contiene a Ada y Grace, una por línea: la colección ha vuelto a como estaba antes de esta guía.
Comentarios