如何使用curlRESTAPI将数据插入现有文件中的marklogic数据库中
我在互联网上搜索并没有发现任何关于这让我重新思考“可能我们无法将数据插入到 Marklogic 中已经存在的文件中”。但是,我想在这里验证我知道使用 curl PUT 命令我们可以更新或创建一个新文档我使用以下查询创建了一个 json
**curl -v -X PUT
--digest --user rest-writer:x
-d'{"recipe": {"name" :"Apple pie", "fromScratch":true, "ingredients":"The Universe"}}'
'http://localhost:8011/LATEST/documents?uri=/example/recipe.json'**
创建此后,我想在同一文件 /example/recipe.json 中添加另一个配方,如下所示
"name" :"Chocolate Cake", "fromScratch":yes, "ingredients":"Coca"
我如何在 Marklogic 中使用 curl 来实现这一点?
回答
您当然可以在现有 JSON 文档中插入其他 JSON 对象。作为一个多模型数据库,数据模型应该是首要考虑的问题。
为了方便JSON对象更新,JSON模型应该是:
{
"recipe" : {
"recipe1":{
"name" : "Apple pie",
"fromScratch" : true,
"ingredients" : "The Universe"
}
}
}
然后构造一个名为 的更新 JSON 内容(选择以下选项之一)
add-recipe.json:
- 在之后添加 JSON 对象
recipe1
{
"patch": [
{ "insert": {
"context": "/recipe/recipe1",
"position": "after",
"content":
{ "recipe2": {
"name" : "Chocolate Cake",
"fromScratch" : true,
"ingredients" : "Coca"
}}
}}
] }
- 在之前添加 JSON 对象
recipe1:
{
"patch": [
{ "insert": {
"context": "/recipe/recipe1",
"position": "before",
"content":
{ "recipe2": {
"name" : "Chocolate Cake",
"fromScratch" : true,
"ingredients" : "Coca"
}}
}}
] }
- 在最后一个孩子之后添加 JSON 对象(如果您有嵌套的 JSON 对象):
{
"patch": [
{ "insert": {
"context": "/recipe",
"position": "last-child",
"content":
{ "recipe2": {
"name" : "Chocolate Cake",
"fromScratch" : true,
"ingredients" : "Coca"
}}
}}
] }
最后,执行
patchREST API 请求以完成操作:
curl --anyauth --user {username}:{password} -X PATCH -d @./add-recipe.json -i -H "Content-type: application/json" "http://{hostname}:{port-number}/v1/documents?uri=/example/recipe.json"
THE END
二维码