如何使用 curl RESTAPI 将数据插入现有文件中的 marklogic 数据库

How to insert data into marklogic database in already existing file using curl RESTAPI

我在网上搜索了一下,没有发现任何相关信息,这让我重新思考“可能我们无法将数据插入到 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 object。作为一个multi-model数据库,数据模型应该是首要考虑因素。

In order to facilitate JSON object update, the JSON model should be:

{
  "recipe" : {
    "recipe1":{
      "name" : "Apple pie", 
      "fromScratch" : true, 
      "ingredients" : "The Universe"
    }
  }
}

Then construct an update JSON content (choose one of below options) called add-recipe.json:

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

  1. recipe1之前添加JSONobject:
{ 
  "patch": [
   { "insert": {
       "context": "/recipe/recipe1",
         "position": "before",
           "content":   
            { "recipe2": {
                "name" : "Chocolate Cake", 
                "fromScratch" : true, 
                "ingredients" : "Coca"
            }}
    
    }}
] }
  1. 在最后一个 child 之后添加 JSON object(如果您嵌套了 JSON object):
{ 
  "patch": [
   { "insert": {
       "context": "/recipe",
         "position": "last-child",
           "content":   
            { "recipe2": {
                "name" : "Chocolate Cake", 
                "fromScratch" : true, 
                "ingredients" : "Coca"
            }}
    
    }}
] }

Finally, perform a patch REST API request to complete the operation:

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"