简体   繁体   中英

How to insert lines into a JSON document in MarkLogic [Update]

I have a JSON document with say fields A,B,C. I want to add fields D,E,F to it. How do I add/concat them? I'm using a cts.uris to first search for the right document to update and once I have that as a sequence then I'm converting it to an object for processing. I know how to update a particular field by not how to add new lines in it. I'm coding in JavaScript in the query console.

You can do the following steps to update a json document:

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);

You can add a property to a JSON object by assigning a value to the belonging key.

 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) 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM