Warm tip: This article is reproduced from serverfault.com, please click

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

(其他 - How to insert data into marklogic database in already existing file using curl RESTAPI)

发布于 2020-12-12 19:45:34

我在互联网上搜索后,一无所获,使我重新思考“也许我们无法将数据插入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实现此目的?

Questioner
user2708013
Viewed
11
Fiona Chen 2020-12-13 13:57:18

你当然可以在现有JSON文档中插入其他JSON对象。作为多模型数据库,应该首先考虑数据模型。

为了促进JSON对象更新,JSON模型应为:

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

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

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

  1. 在之前添加JSON对象recipe1
{ 
  "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"