第十一章 REST 客户端方法 - 插入和更新文档
插入和更新文档
SaveDocument:将新文档插入到指定数据库中。
curl -i -X POST -H "Content-Type: application/json"
http://localhost:57774/api/docdb/v1/namespaceName/doc/databaseName/
请注意,此 URI 末尾有一个斜杠。
要插入一个或多个文档,请执行 POST。请求的正文是 JSON 文档对象或文档对象的 JSON 数组。请注意,文档对象可能处于仅包含内容的展开形式。此展开的表单始终会导致插入新的 %DocumentId。指定其 %DocumentId 不存在于数据库中的包装文档对象会导致插入该 %DocumentId。否则,%DocumentId 属性将被忽略,并使用新的 %DocumentId 进行插入。如果请求成功,则返回单个 JSON 文档标头对象或 JSON 文档标头对象数组。如果请求失败,JSON 标头对象将替换为错误对象。
SaveDocument:替换指定数据库中的现有文档。
curl -i -X PUT -H "Content-Type: application/json"
http://localhost:57774/api/docdb/v1/namespaceName/doc/databaseName/id
在特定 id 位置插入单个 JSON 文档对象。如果指定的 %DocumentId 已存在,系统将用新文档替换现有文档。
SaveDocumentByKey:替换指定数据库中的现有文档。
curl -i -X PUT -H "Content-Type: application/json"
http://localhost:57774/api/docdb/v1/namespaceName/doc/databaseName/keyPropertyName/keyValue
删除文档
DeleteDocument:从指定数据库中删除文档。
curl -i -X DELETE -H "Content-Type: application/json"
http://localhost:57774/api/docdb/v1/namespaceName/doc/databaseName/id
删除 %DocumentId 指定的文档。如果请求成功,则删除指定文档,返回文档包装器元数据{"%DocumentId":<IDnum>,"%LastModified":<timestamp>} ,并返回 200 (OK) 状态。
DeleteDocumentByKey:从指定数据库中删除文档。
curl -i -X DELETE -H "Content-Type: application/json"
http://localhost:57774/api/docdb/v1/namespaceName/doc/databaseName/keyPropertyName/keyValue
检索文档
GetDocument:从数据库中返回指定的文档。
curl -i -X GET -H "Content-Type: application/json"
http://localhost:57774/api/docdb/v1/namespaceName/doc/databaseName/id? wrapped=true|false
GetDocumentByKey:通过定义为数据库中唯一键的属性返回文档。
curl -i -X POST -H "Content-Type: application/json"
http://localhost:57774/api/docdb/v1/namespaceName/doc/databaseName/keyPropertyName/keyValue
FindDocuments:返回数据库中符合查询规范的所有文档。
curl -i -X POST -H "Content-Type: application/json"
http://localhost:57774/api/docdb/v1/namespaceName/find/databaseName? wrapped=true|false
以下curl 脚本示例提供完整的用户凭据和标头信息。它返回 MySamples 命名空间中 Continents 文档数据库中的所有文档:
curl --user _SYSTEM:SYS -w "\n\n%{http_code}\n" -X POST
-H "Accept: application/json" -H "Content-Type: application/json"
http://localhost:57774/api/docdb/v1/mysamples/find/continents
它从文档数据库返回 JSON 数据,如下所示:
{"content":{"sqlcode":100,"message":null,"content":[
{"%Doc":"{\"code\":\"NA\",\"name\":\"North America\"}","%DocumentId":"1","%LastModified":"2018-02-15 21:33:03.64"},
{"%Doc":"{\"code\":\"SA\",\"name\":\"South America\"}","%DocumentId":"2","%LastModified":"2018-02-15 21:33:03.64"},
{"%Doc":"{\"code\":\"AF\",\"name\":\"Africa\"}","%DocumentId":"3","%LastModified":"2018-02-15 21:33:03.64"},
{"%Doc":"{\"code\":\"AS\",\"name\":\"Asia\"}","%DocumentId":"4","%LastModified":"2018-02-15 21:33:03.64"},
{"%Doc":"{\"code\":\"EU\",\"name\":\"Europe\"}","%DocumentId":"5","%LastModified":"2018-02-15 21:33:03.64"},
{"%Doc":"{\"code\":\"OC\",\"name\":\"Oceana\"}","%DocumentId":"6","%LastModified":"2018-02-15 21:33:03.64"},
{"%Doc":"{\"code\":\"AN\",\"name\":\"Antarctica\"}","%DocumentId":"7","%LastModified":"2018-02-15 21:33:03.64"}]}}
限制:以下curl 脚本示例通过指定限制对象来限制从Continents 文档数据库返回的文档。此限制将返回的文件限制为姓名以字母 A 开头的人员:
curl --user _SYSTEM:SYS -w "\n\n%{http_code}\n" -X POST
-H "Accept: application/json" -H "Content-Type: application/json"
http://localhost:57774/api/docdb/v1/mysamples/find/continents -d
'{"restriction":["Name","A","%STARTSWITH"]}'
它从文档数据库返回以下 JSON 文档:
{"content":{"sqlcode":100,"message":null,"content":[
{"%Doc":"{\"code\":\"AF\",\"name\":\"Africa\"}","%DocumentId":"3","%LastModified":"2018-02-15 21:33:03.64"},
{"%Doc":"{\"code\":\"AS\",\"name\":\"Asia\"}","%DocumentId":"4","%LastModified":"2018-02-15 21:33:03.64"},
{"%Doc":"{\"code\":\"AN\",\"name\":\"Antarctica\"}","%DocumentId":"7","%LastModified":"2018-02-15 21:33:03.64"}]}}
投影:以下curl 脚本示例投影从 Continents 文档数据库中的每个文档返回哪些 JSON 属性。它使用与前面的示例相同的限制:
curl --user _SYSTEM:SYS -w "\n\n%{http_code}\n" -X POST
-H "Accept: application/json" -H "Content-Type: application/json"
http://localhost:57774/api/docdb/v1/mysamples/find/continents -d
'{"restriction":["Name","A","%STARTSWITH"],"projection":["%DocumentId","name"]}'
它从文档数据库返回 JSON 数据,如下所示:
{"content":{"sqlcode":100,"message":null,"content":[
{"%Doc":"{"%DocumentId":"3","name":"Africa"}},
{"%Doc":"{"%DocumentId":"4","name":"Asia"}},
{"%Doc":"{"%DocumentId":"7","name":"Antarctica"}}]}}