查找文档

POST /v1/databases/{databaseId}/collections/{collection}:find · API key

请求:JSON,Content-Type: application/json · 响应:JSONL,或响应体为空的 204 No Content

请求体字段 默认值 含义
filter {} 条件;见 查询。
limit 1 最多返回的文档数。-1 返回全部。
skip 0 先跳过的匹配文档数。
index 无 用于响应查询的 索引 名称。
value 要查找的值,配合 map 索引。
from、to 范围边界,配合 btree 索引。例如:{"mrr": 100}。
reverse false 降序,配合 btree 索引。
match 要搜索的词,配合 fts 索引。
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 '{"filter": {"plan": {"$in": ["pro", "team"]}}, "limit": 10}'
POST /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

{"filter": {"plan": {"$in": ["pro", "team"]}}, "limit": 10}
// 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{"plan": map[string]any{"$in": []any{"pro", "team"}}}, "limit": 10}
	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 = ['filter' => ['plan' => ['$in' => ['pro', 'team']]], 'limit' => 10];
$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 = {"filter": {"plan": {"$in": ["pro", "team"]}}, "limit": 10}

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 = {"filter": {"plan": {"$in": ["pro", "team"]}}, "limit": 10};

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 = """
{"filter": {"plan": {"$in": ["pro", "team"]}}, "limit": 10}
""";

        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());
    }
}

响应 200 OK,以 JSON Lines 返回文档。没有匹配时返回 204 No Content。

1{"id":"ada","name":"Ada Lovelace","plan":"pro","mrr":120}

运算符:$eq、$ne、$gt、$ge、$lt、$le、$in、$nin、$contains、$and、$or。

$gte 和 $lte 不存在: 请使用 $ge 和 $le。未知运算符不会报错:过滤器不匹配任何文档,响应为 204 No Content。

评论

发表评论