我在互联网上搜索后,一无所获,使我重新思考“也许我们无法将数据插入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
:
recipe1
{
"patch": [
{ "insert": {
"context": "/recipe/recipe1",
"position": "after",
"content":
{ "recipe2": {
"name" : "Chocolate Cake",
"fromScratch" : true,
"ingredients" : "Coca"
}}
}}
] }
recipe1
:{
"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"
}}
}}
] }
最后,执行
patch
REST 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"