kuzzle

Updating a kuzzle doc


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);
}
  1. Is this the correct way to update the json ?
  2. what about a nested json ? like :
    { 
     '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 ?


Solution

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