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

如何使用curl RESTAPI将数据插入已有文件中的marklogic数据库,curl,marklogic,restapi,Curl,Marklogic,Restapi,我在互联网上搜索了一下,没有发现任何相关信息,这让我重新思考,我们可能无法将数据插入到Marklogic中已经存在的文件中 我知道使用curlput命令可以更新或创建新文档 我使用以下查询创建了一个json **curl -v -X PUT \ --digest --user rest-writer:x \ -d'{"recipe": {"name" :"Apple pie", "fromScratch":t

我在互联网上搜索了一下,没有发现任何相关信息,这让我重新思考,我们可能无法将数据插入到Marklogic中已经存在的文件中 我知道使用curlput命令可以更新或创建新文档 我使用以下查询创建了一个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模型应为:

然后构造一个更新JSON内容选择以下选项之一,称为add-recipe.JSON:

在recipe1之后添加JSON对象 在recipe1之前添加JSON对象: 如果您有嵌套的JSON对象,请在最后一个子对象之后添加JSON对象: 最后,执行补丁REST API请求以完成操作:


您当然可以在现有JSON文档中插入其他JSON对象。作为一个多模型数据库,数据模型应该是首要考虑的问题

为了便于JSON对象更新,JSON模型应为:

然后构造一个更新JSON内容选择以下选项之一,称为add-recipe.JSON:

在recipe1之后添加JSON对象 在recipe1之前添加JSON对象: 如果您有嵌套的JSON对象,请在最后一个子对象之后添加JSON对象: 最后,执行补丁REST API请求以完成操作:


是否尝试附加内容以更新文档?还是要存储单独的文档?如果是后者,请确保为其提供不同的URI,即documents?URI=/example/recipe/chocolate-cake.jsonI建议不要将所有食谱存储在一个文件中。你会从中受益匪浅。对于初学者来说,通过这种方式搜索单个菜谱会更直接。而且数据更新的扩展性也会更好。您是否正在尝试附加内容以更新文档?还是要存储单独的文档?如果是后者,请确保为其提供不同的URI,即documents?URI=/example/recipe/chocolate-cake.jsonI建议不要将所有食谱存储在一个文件中。你会从中受益匪浅。对于初学者来说,通过这种方式搜索单个菜谱会更直接。数据更新的规模也会更好。
{
  "recipe" : {
    "recipe1":{
      "name" : "Apple pie", 
      "fromScratch" : true, 
      "ingredients" : "The Universe"
    }
  }
}
{ 
  "patch": [
   { "insert": {
       "context": "/recipe/recipe1",
         "position": "after",
           "content":   
            { "recipe2": {
                "name" : "Chocolate Cake", 
                "fromScratch" : true, 
                "ingredients" : "Coca"
            }}
    
    }}
] }

{ 
  "patch": [
   { "insert": {
       "context": "/recipe/recipe1",
         "position": "before",
           "content":   
            { "recipe2": {
                "name" : "Chocolate Cake", 
                "fromScratch" : true, 
                "ingredients" : "Coca"
            }}
    
    }}
] }
{ 
  "patch": [
   { "insert": {
       "context": "/recipe",
         "position": "last-child",
           "content":   
            { "recipe2": {
                "name" : "Chocolate Cake", 
                "fromScratch" : true, 
                "ingredients" : "Coca"
            }}
    
    }}
] }
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"