Colecciones
Una colección es un grupo de documentos JSON con nombre dentro de una base de datos, como customers u orders. Los documentos de una misma colección pueden tener campos distintos: no hay esquema que declarar.
Antes de empezar: solo necesitas las variables de entorno de Primeros pasos. Esta guía es independiente: crea una colección llamada collections-demo y la borra al final. No toca customers.
Crear una colección
curl -X POST "https://inceptiondb.hola.cloud/v1/databases/$INCEPTIONDB_DATABASE/collections" \
-H "Api-Key: $INCEPTIONDB_API_KEY" \
-H "Api-Secret: $INCEPTIONDB_API_SECRET" \
-H "Content-Type: application/json" \
-d '{"name": "collections-demo"}'POST /v1/databases/$INCEPTIONDB_DATABASE/collections HTTP/1.1
Host: inceptiondb.hola.cloud
Api-Key: $INCEPTIONDB_API_KEY
Api-Secret: $INCEPTIONDB_API_SECRET
Content-Type: application/json
{"name": "collections-demo"}// 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{"name": "collections-demo"}
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", 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 = ['name' => 'collections-demo'];
$body = json_encode($payload);
$ch = curl_init();
curl_setopt_array($ch, [
CURLOPT_URL => 'https://inceptiondb.hola.cloud/v1/databases/' . getenv('INCEPTIONDB_DATABASE') . '/collections',
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 = {"name": "collections-demo"}
response = requests.request(
"POST",
"https://inceptiondb.hola.cloud/v1/databases/" + os.environ["INCEPTIONDB_DATABASE"] + "/collections",
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 = {"name": "collections-demo"};
const response = await fetch("https://inceptiondb.hola.cloud/v1/databases/" + process.env.INCEPTIONDB_DATABASE + "/collections", {
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 = """
{"name": "collections-demo"}
""";
var request = HttpRequest.newBuilder()
.uri(URI.create("https://inceptiondb.hola.cloud/v1/databases/" + System.getenv("INCEPTIONDB_DATABASE") + "/collections"))
.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());
}
}Respuesta (201 Created):
1{"defaults":{"id":"uuid()"},"indexes":0,"name":"collections-demo","total":0}
| Campo | Significado |
|---|---|
name |
Nombre de la colección. Se usa en todas sus URLs. |
total |
Número de documentos. |
indexes |
Número de índices. |
defaults |
Valores que se añaden a los documentos que no los tienen (ver más abajo). |
Los nombres distinguen mayúsculas y minúsculas. Una colección que ya existe responde 500 con el mensaje collection `collections-demo` already exists: si ya seguiste esta guía, ejecuta el último paso, Borrar una colección, y empieza de nuevo.
Insertar en una colección que no existe la crea, con los valores por defecto. Una errata en el nombre de la colección en :insert guarda los documentos en una colección nueva en vez de fallar.
Listar las colecciones
curl "https://inceptiondb.hola.cloud/v1/databases/$INCEPTIONDB_DATABASE/collections" \
-H "Api-Key: $INCEPTIONDB_API_KEY" \
-H "Api-Secret: $INCEPTIONDB_API_SECRET"GET /v1/databases/$INCEPTIONDB_DATABASE/collections 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", 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',
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",
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", {
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"))
.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[{"defaults":{"id":"uuid()"},"indexes":0,"name":"collections-demo","total":0},{"defaults":{"id":"uuid()"},"indexes":0,"name":"customers","total":2}]
customers aparece si seguiste Primeros pasos.
Obtener una colección
curl "https://inceptiondb.hola.cloud/v1/databases/$INCEPTIONDB_DATABASE/collections/collections-demo" \
-H "Api-Key: $INCEPTIONDB_API_KEY" \
-H "Api-Secret: $INCEPTIONDB_API_SECRET"GET /v1/databases/$INCEPTIONDB_DATABASE/collections/collections-demo 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/collections-demo", 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/collections-demo',
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/collections-demo",
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/collections-demo", {
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/collections-demo"))
.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{"defaults":{"id":"uuid()"},"indexes":0,"name":"collections-demo","total":0}
Una colección que no existe responde 404 Not Found.
Valores por defecto
Los valores por defecto rellenan los campos que un documento insertado no tiene. Un campo enviado como null también recibe su valor por defecto. Además de valores fijos, hay tres generados:
| Valor | Genera |
|---|---|
"uuid()" |
Un ID único aleatorio, como "35ec7d8f-96f4-4cdf-a09e-463c720e7c8c". |
"auto()" |
Un número creciente: 1, 2, 3... Nunca se repite, aunque puede saltarse números. |
"unixnano()" |
El momento de la inserción, en nanosegundos desde 1970. |
Dale a cada documento nuevo un ID, el plan free y su fecha de creación:
curl -X POST "https://inceptiondb.hola.cloud/v1/databases/$INCEPTIONDB_DATABASE/collections/collections-demo:setDefaults" \
-H "Api-Key: $INCEPTIONDB_API_KEY" \
-H "Api-Secret: $INCEPTIONDB_API_SECRET" \
-H "Content-Type: application/json" \
-d '{"id": "uuid()", "plan": "free", "created": "unixnano()"}'POST /v1/databases/$INCEPTIONDB_DATABASE/collections/collections-demo:setDefaults HTTP/1.1
Host: inceptiondb.hola.cloud
Api-Key: $INCEPTIONDB_API_KEY
Api-Secret: $INCEPTIONDB_API_SECRET
Content-Type: application/json
{"id": "uuid()", "plan": "free", "created": "unixnano()"}// 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{"created": "unixnano()", "id": "uuid()", "plan": "free"}
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/collections-demo:setDefaults", 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 = ['created' => 'unixnano()', 'id' => 'uuid()', 'plan' => 'free'];
$body = json_encode($payload);
$ch = curl_init();
curl_setopt_array($ch, [
CURLOPT_URL => 'https://inceptiondb.hola.cloud/v1/databases/' . getenv('INCEPTIONDB_DATABASE') . '/collections/collections-demo:setDefaults',
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 = {"created": "unixnano()", "id": "uuid()", "plan": "free"}
response = requests.request(
"POST",
"https://inceptiondb.hola.cloud/v1/databases/" + os.environ["INCEPTIONDB_DATABASE"] + "/collections/collections-demo:setDefaults",
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 = {"created": "unixnano()", "id": "uuid()", "plan": "free"};
const response = await fetch("https://inceptiondb.hola.cloud/v1/databases/" + process.env.INCEPTIONDB_DATABASE + "/collections/collections-demo:setDefaults", {
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 = """
{"id": "uuid()", "plan": "free", "created": "unixnano()"}
""";
var request = HttpRequest.newBuilder()
.uri(URI.create("https://inceptiondb.hola.cloud/v1/databases/" + System.getenv("INCEPTIONDB_DATABASE") + "/collections/collections-demo:setDefaults"))
.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{"created":"unixnano()","id":"uuid()","plan":"free"}
Ahora inserta un documento solo con nombre y email:
curl -X POST "https://inceptiondb.hola.cloud/v1/databases/$INCEPTIONDB_DATABASE/collections/collections-demo:insert" \
-H "Api-Key: $INCEPTIONDB_API_KEY" \
-H "Api-Secret: $INCEPTIONDB_API_SECRET" \
-H "Content-Type: application/x-ndjson" \
-d '{"name": "Margaret Hamilton", "email": "margaret@example.com"}'POST /v1/databases/$INCEPTIONDB_DATABASE/collections/collections-demo:insert HTTP/1.1
Host: inceptiondb.hola.cloud
Api-Key: $INCEPTIONDB_API_KEY
Api-Secret: $INCEPTIONDB_API_SECRET
Content-Type: application/x-ndjson
{"name": "Margaret Hamilton", "email": "margaret@example.com"}// 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{"email": "margaret@example.com", "name": "Margaret Hamilton"}
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/collections-demo: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
$payload = ['email' => 'margaret@example.com', 'name' => 'Margaret Hamilton'];
$body = json_encode($payload);
$ch = curl_init();
curl_setopt_array($ch, [
CURLOPT_URL => 'https://inceptiondb.hola.cloud/v1/databases/' . getenv('INCEPTIONDB_DATABASE') . '/collections/collections-demo: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",
}
payload = {"email": "margaret@example.com", "name": "Margaret Hamilton"}
response = requests.request(
"POST",
"https://inceptiondb.hola.cloud/v1/databases/" + os.environ["INCEPTIONDB_DATABASE"] + "/collections/collections-demo:insert",
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 = {"email": "margaret@example.com", "name": "Margaret Hamilton"};
const response = await fetch("https://inceptiondb.hola.cloud/v1/databases/" + process.env.INCEPTIONDB_DATABASE + "/collections/collections-demo:insert", {
method: "POST",
headers: {
"Api-Key": process.env.INCEPTIONDB_API_KEY,
"Api-Secret": process.env.INCEPTIONDB_API_SECRET,
"Content-Type": "application/x-ndjson"
},
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 = """
{"name": "Margaret Hamilton", "email": "margaret@example.com"}
""";
var request = HttpRequest.newBuilder()
.uri(URI.create("https://inceptiondb.hola.cloud/v1/databases/" + System.getenv("INCEPTIONDB_DATABASE") + "/collections/collections-demo: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());
}
}1{"created":1793164963789350717,"email":"margaret@example.com","id":"35ec7d8f-96f4-4cdf-a09e-463c720e7c8c","name":"Margaret Hamilton","plan":"free"}
Los valores por defecto se aplican a los documentos que se insertan a partir de ahora; los que ya existen no cambian. setDefaults añade los campos que envías a los valores actuales y sustituye los que ya existen. Para quitar un valor por defecto, envíalo como null: {"plan": null}.
Tamaño
Cuánta memoria ocupan los documentos, el tamaño en disco y, para cada índice, su memoria como index.<nombre>, en bytes:
curl -X POST "https://inceptiondb.hola.cloud/v1/databases/$INCEPTIONDB_DATABASE/collections/collections-demo:size" \
-H "Api-Key: $INCEPTIONDB_API_KEY" \
-H "Api-Secret: $INCEPTIONDB_API_SECRET"POST /v1/databases/$INCEPTIONDB_DATABASE/collections/collections-demo:size 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("POST", "https://inceptiondb.hola.cloud/v1/databases/" + os.Getenv("INCEPTIONDB_DATABASE") + "/collections/collections-demo:size", 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/collections-demo:size',
CURLOPT_RETURNTRANSFER => true,
CURLOPT_CUSTOMREQUEST => 'POST',
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(
"POST",
"https://inceptiondb.hola.cloud/v1/databases/" + os.environ["INCEPTIONDB_DATABASE"] + "/collections/collections-demo:size",
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/collections-demo:size", {
method: "POST",
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/collections-demo:size"))
.method("POST", 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{"disk":265,"memory":203}
Borrar una colección
Esto borra la colección con todos sus documentos e índices. No se puede deshacer: comprueba el nombre en la URL antes de ejecutarlo. Borra collections-demo, la colección de esta guía:
curl -X POST "https://inceptiondb.hola.cloud/v1/databases/$INCEPTIONDB_DATABASE/collections/collections-demo:dropCollection" \
-H "Api-Key: $INCEPTIONDB_API_KEY" \
-H "Api-Secret: $INCEPTIONDB_API_SECRET"POST /v1/databases/$INCEPTIONDB_DATABASE/collections/collections-demo:dropCollection 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("POST", "https://inceptiondb.hola.cloud/v1/databases/" + os.Getenv("INCEPTIONDB_DATABASE") + "/collections/collections-demo:dropCollection", 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/collections-demo:dropCollection',
CURLOPT_RETURNTRANSFER => true,
CURLOPT_CUSTOMREQUEST => 'POST',
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(
"POST",
"https://inceptiondb.hola.cloud/v1/databases/" + os.environ["INCEPTIONDB_DATABASE"] + "/collections/collections-demo:dropCollection",
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/collections-demo:dropCollection", {
method: "POST",
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/collections-demo:dropCollection"))
.method("POST", 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());
}
}La respuesta es 204 No Content. Borrar una colección que no existe responde 500 con el mensaje collection `collections-demo` not found.
Comentarios