我有一个JSON文档,其中包含字段A,B,C。我想要添加字段D,E,F。如何添加/连接它们?我正在使用cts.uris来首先搜索要更新的正确文档,一旦我将其作为序列,然后我将其转换为对象进行处理。我知道如何更新特定字段,而不是如何在其中添加新行。我在查询控制台中用JavaScript编码。
您可以执行以下步骤来更新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);
您可以通过为所属项分配值来向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)