如何在MarkLogic中将行插入JSON文档[更新]

问题描述 投票:0回答:2

我有一个JSON文档,其中包含字段A,B,C。我想要添加字段D,E,F。如何添加/连接它们?我正在使用cts.uris来首先搜索要更新的正确文档,一旦我将其作为序列,然后我将其转换为对象进行处理。我知道如何更新特定字段,而不是如何在其中添加新行。我在查询控制台中用JavaScript编码。

javascript json marklogic document
2个回答
3
投票

您可以执行以下步骤来更新json文档:

declareUpdate();
// get the document
const uri = '/folder/doc.json';
const doc = cts.doc(uri);

// create an object from the document
const obj = doc.toObject();

// add the new fields to the object
obj.d = 'd';
obj.e = 'e';
obj.f = 'f';

// save the object as a json document
xdmp.documentInsert(uri, obj);

1
投票

您可以通过为所属项分配值来向JSON对象添加属性。

const obj = {
  a: true,
  b: 42,
  c: 'Hello World!'
}

obj['d'] = 'To be, or not to be'
obj['e'] = 'foobar'
obj['f'] = false

console.log(obj)
© www.soinside.com 2019 - 2024. All rights reserved.