如何使用curl RESTAPI将数据插入到现有文件中的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"
}
}
}

然后构造一个名为add-recipe.json:的更新JSON内容(选择以下选项之一(

  1. recipe1之后添加JSON对象
{ 
"patch": [
{ "insert": {
"context": "/recipe/recipe1",
"position": "after",
"content":   
{ "recipe2": {
"name" : "Chocolate Cake", 
"fromScratch" : true, 
"ingredients" : "Coca"
}}

}}
] }
  1. recipe1之前添加JSON对象:
{ 
"patch": [
{ "insert": {
"context": "/recipe/recipe1",
"position": "before",
"content":   
{ "recipe2": {
"name" : "Chocolate Cake", 
"fromScratch" : true, 
"ingredients" : "Coca"
}}

}}
] }
  1. 在最后一个子对象后面添加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"

最新更新