I have a kuzzle document with a json like this:
j00:
{ '1': 'dsds',
'2': 'rer',
'5': 'yytyh hgvhg',
'8': 'koo kllkl vv'
}
currently i'am doing this to update a key :
kuzzle_doc = await get_kuzzle_doc(i,c,d);
current_value = kuzzle_doc._source.j00[key_to_modify];
new_value = modify(current_value);
kuzzle_doc._source.j00[key_to_modify] = new_value;
try {
const response = await kuzzle.document.update(
i,
c,
d,
{
j00: kuzzle_doc._source.j00
}
);
console.log('response',response);
} catch (error) {
console.error("await kuzzle.document.update ....", error);
}
{
'a': 'dsds',
'b': 'rer',
'c': 'yytyh hgvhg',
'd': 'koo kllkl vv'
'e': {}
}
What's the efficient way to update a key in the nested json ?
To update one attribute from a document you can modify the attribute of your document and then send the whole document:
const myDoc = {
foo: 'bar',
baz: {
will: 'smith'
}
};
myDoc.foo = 'newValue';
myDoc.baz.will = 'anderson';
kuzzle.document.update(
'index',
'collection',
'docId',
myDoc
);