Search code examples
jsonnode.jsjson-patch

Using jsonpatch to create new path


I'm trying to use jsonpatch to create new path in a mongoose document, but I can't make it work.

The page oficial page enter link description here says that I can add new values to a path like this,

{op: add, path:/mypath, value: 'new value'}

But what I want is to add new path with new values. I've try passing a variable to the path like this:

var valuesItem = [{
            op: 'add',
            path: '/' + data,
            value: 'data'
        }];

But that don't work. Thanks.


Solution

  • I have to change the way that it was updated the data in the database, like this:

    value = value.toObject();
    var patches = request.payload;
    jsonpatch.apply(value, patches);
    
          Value.update({ _id: request.params.id }, value, { overwrite: true }, function(err) {
            if (err) {
              reply(err);
            } else {
             ...code...
            }