修改文档

修改通过全表扫描或索引遍历选中的文档。端点以 JSON Lines 返回每个已修改文档。

认证

需要 Api-KeyApi-Secret,或者在数据库 owner 被允许时使用 Glue owner token。

请求体

字段 类型 描述
patch object 应用于每个匹配文档的 patch 对象。
index string 可选索引名称。省略时扫描整个集合。
filter object patch 前应用的可选 Connor filter。
skip integer 跳过的匹配文档数量。默认:0
limit integer 最多修改的文档数量。默认:10 不修改文档。

请求体中也可以包含索引专用字段。

HTTP 请求

curl -X POST "https://api.hola.cloud/v1/databases/a1b2c3d4-e5f6-7890-abcd-ef1234567890/collections/users:patch" \
  -H "Api-Key: 1abbe476-6ad6-4b97-9cca-6deb6ab2901d" \
  -H "Api-Secret: 4bda6d52-762b-4e5d-bed7-85614c13b8bf" \
  -H "Content-Type: application/json" \
  -d '{
  "filter": {"id": "user-1"},
  "patch": {"role": "owner"},
  "limit": 1
}'
POST /v1/databases/a1b2c3d4-e5f6-7890-abcd-ef1234567890/collections/users:patch HTTP/1.1
Host: api.hola.cloud
Api-Key: 1abbe476-6ad6-4b97-9cca-6deb6ab2901d
Api-Secret: 4bda6d52-762b-4e5d-bed7-85614c13b8bf
Content-Type: application/json

{
  "filter": {"id": "user-1"},
  "patch": {"role": "owner"},
  "limit": 1
}
package main

import (
	"fmt"
	"io"
	"net/http"
	"encoding/json"
	"strings"
)

func main() {
	payload := map[string]any{"filter": map[string]any{"id": "user-1"}, "limit": 1, "patch": map[string]any{"role": "owner"}}
	bodyBytes, err := json.Marshal(payload)
	if err != nil {
		panic(err)
	}
	body := string(bodyBytes)

	req, err := http.NewRequest("POST", "https://api.hola.cloud/v1/databases/a1b2c3d4-e5f6-7890-abcd-ef1234567890/collections/users:patch", strings.NewReader(body))
	if err != nil {
		panic(err)
	}
	req.Header.Set("Api-Key", "1abbe476-6ad6-4b97-9cca-6deb6ab2901d")
	req.Header.Set("Api-Secret", "4bda6d52-762b-4e5d-bed7-85614c13b8bf")
	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(string(responseBody))
}
<?php
$payload = ['filter' => ['id' => 'user-1'], 'limit' => 1, 'patch' => ['role' => 'owner']];
$body = json_encode($payload);

$ch = curl_init();

curl_setopt_array($ch, [
    CURLOPT_URL => 'https://api.hola.cloud/v1/databases/a1b2c3d4-e5f6-7890-abcd-ef1234567890/collections/users:patch',
    CURLOPT_RETURNTRANSFER => true,
    CURLOPT_CUSTOMREQUEST => 'POST',
    CURLOPT_POSTFIELDS => $body,
    CURLOPT_HTTPHEADER => [
        'Api-Key: 1abbe476-6ad6-4b97-9cca-6deb6ab2901d',
        'Api-Secret: 4bda6d52-762b-4e5d-bed7-85614c13b8bf',
        'Content-Type: application/json',
    ],
]);

$response = curl_exec($ch);
if ($response === false) {
    throw new Exception(curl_error($ch));
}
curl_close($ch);

echo $response;
import requests

import json

headers = {
    "Api-Key": "1abbe476-6ad6-4b97-9cca-6deb6ab2901d",
    "Api-Secret": "4bda6d52-762b-4e5d-bed7-85614c13b8bf",
    "Content-Type": "application/json",
}

payload = {"filter": {"id": "user-1"}, "limit": 1, "patch": {"role": "owner"}}
body = json.dumps(payload)

response = requests.request(
    "POST",
    "https://api.hola.cloud/v1/databases/a1b2c3d4-e5f6-7890-abcd-ef1234567890/collections/users:patch",
    headers=headers,
    data=body
)

print(response.text)
const payload = {"filter": {"id": "user-1"}, "limit": 1, "patch": {"role": "owner"}};

const response = await fetch("https://api.hola.cloud/v1/databases/a1b2c3d4-e5f6-7890-abcd-ef1234567890/collections/users:patch", {
  method: "POST",
  headers: {
    "Api-Key": "1abbe476-6ad6-4b97-9cca-6deb6ab2901d",
    "Api-Secret": "4bda6d52-762b-4e5d-bed7-85614c13b8bf",
    "Content-Type": "application/json"
  },
  body: JSON.stringify(payload)
});

console.log(await response.text());
const payload = {"filter": {"id": "user-1"}, "limit": 1, "patch": {"role": "owner"}};

const response = await fetch("https://api.hola.cloud/v1/databases/a1b2c3d4-e5f6-7890-abcd-ef1234567890/collections/users:patch", {
  method: "POST",
  headers: {
    "Api-Key": "1abbe476-6ad6-4b97-9cca-6deb6ab2901d",
    "Api-Secret": "4bda6d52-762b-4e5d-bed7-85614c13b8bf",
    "Content-Type": "application/json"
  },
  body: JSON.stringify(payload)
});

const text = await response.text();
console.log(text);
import java.net.URI;
import java.net.http.HttpClient;
import java.net.http.HttpRequest;
import java.net.http.HttpResponse;
import com.fasterxml.jackson.databind.ObjectMapper;
import java.util.Map;
import java.util.List;

public class Main {
    public static void main(String[] args) throws Exception {
        var payload = Map.of("filter", Map.of("id", "user-1"), "limit", 1, "patch", Map.of("role", "owner"));
        var body = new ObjectMapper().writeValueAsString(payload);

        var request = HttpRequest.newBuilder()
            .uri(URI.create("https://api.hola.cloud/v1/databases/a1b2c3d4-e5f6-7890-abcd-ef1234567890/collections/users:patch"))
            .method("POST", HttpRequest.BodyPublishers.ofString(body))
            .header("Api-Key", "1abbe476-6ad6-4b97-9cca-6deb6ab2901d")
            .header("Api-Secret", "4bda6d52-762b-4e5d-bed7-85614c13b8bf")
            .header("Content-Type", "application/json")
            .build();

        var response = HttpClient.newHttpClient().send(request, HttpResponse.BodyHandlers.ofString());
        System.out.println(response.body());
    }
}

响应

1{"id":"user-1","name":"Alice","role":"owner"}

评论

发表评论