如何使用 xdmp:node-insert 在 Marklogic 的 JSON 文檔中插入節點 (how to insert node in JSON doc in Marklogic using xdmp:node-insert)


問題描述

如何使用 xdmp:node‑insert 在 Marklogic 的 JSON 文檔中插入節點 (how to insert node in JSON doc in Marklogic using xdmp:node‑insert)

我想像這樣在 JSON 文檔中插入節點:

{ “fileName”:“l”,“user_id”:“test”,“requestId”:“ 1232”,“feedbackType”:“cpd_mah” }

運行此代碼:

let $old := $doc/filename
return
  xdmp:node‑insert‑after($old, object‑node{"isSaved": ""})

但這會引發錯誤XDMP‑CHILDUNNAMED


參考解法

方法 1:

I agree with Mads, that using node‑insert‑child makes more sense with JSON. You can use node‑insert‑after though. Your attempt is throwing an XDMP‑CHILDUNNAMED error because you are passing in an unnamed object node (which is basically an anonymous wrapper for its properties), rather than the named property you'd like to insert. Mads code is giving away how you can make it work:

let $old := doc("/test.json")/filename
return
  xdmp:node‑insert‑after($old, object‑node{"isSaved": ""}/isSaved)

Note: if you run it multiple times, it will replace rather than insert, since properties should be unique.

HTH!

方法 2:

JSON properties don't have the concept of a sibling, the way that XML elements do.

A JSON object is basically a map with a set of properties. So, rather than inserting an object as a child node after one of the properties, you want to insert a child node (notice the XPath selecting isSaved from the constructed object) of the JSON document object‑node()

let $old := doc("/test.json")/object‑node()
return
  xdmp:node‑insert‑child($old, object‑node{"isSaved": ""}/isSaved)

A few examples of working with JSON in XQuery from the MarkLogic documentation: https://docs.marklogic.com/guide/app‑dev/json#id_60123

(by KumarGgrtjnMads Hansen)

參考文件

  1. how to insert node in JSON doc in Marklogic using xdmp:node‑insert (CC BY‑SA 2.5/3.0/4.0)

#marklogic #marklogic-9 #NoSQL






相關問題

Marklogic 是否有 liquibase 等價物 (Is there a liquibase equivalent for Marklogic)

Cách hiệu quả nhất để lưu trữ các cặp tên / giá trị trong cơ sở dữ liệu Marklogic là gì (What is the most efficient way to store name/value pairs in a Marklogic database)

對特定用戶隱藏 marklogic 數據庫(權限) (Hide a marklogic database to specific user (permissions))

在 marklogic 中使用 xquery 返回搜索結果 (Returning search results using xquery in marklogic)

創建僅對給定數據庫具有權限的用戶 (Create user that has permission only to given database)

Marklogic Java API 語義三重搜索 (Marklogic Java API Semantic Triple Search)

marklogic mlcp 自定義轉換將聚合文檔拆分為多個文件 (marklogic mlcp custom transform split aggregate document to multiple files)

如果某些文檔具有空值元素,則在日期字段上使用 element-range-query 搜索 (Search with element-range-query on date field if some of the documents have empty-value elements)

使用 MarkLogic 節點 API,我可以通過 LDAP 進行身份驗證嗎? (Using the MarkLogic Node API, can I authenticate through LDAP?)

如何使用 xdmp:node-insert 在 Marklogic 的 JSON 文檔中插入節點 (how to insert node in JSON doc in Marklogic using xdmp:node-insert)

我們可以在不同版本的 marklogic 之間進行森林或數據複製嗎? (Can we have forests or data replication between different versions of marklogic?)

換行在 XQuery 的行尾留下額外的空間 (Line feed leaving extra space at end of line in XQuery)







留言討論