1

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.

Diego
  • 493
  • 1
  • 9
  • 26
  • Can you be more specific about exactly what you're expecting the input to be and what you'd expect the patched output to look like? – loganfsmyth Jan 31 '15 at 04:50
  • Well , I have two inputs on the client side , with them I want to build an object and send them to the database via patch to be added to a document according to an id . I'm using Mongoose database , but I don't know if necessarily there have to be a model created to have access to the path patch that have to be created. Thank you very much for your answer. regards – Diego Jan 31 '15 at 16:20
  • Did you get this sorted? The question is a bit unclear. – Douglas Ferguson Feb 15 '15 at 22:13
  • Yes the problem was in the api and mongoose, aperently mongoose add to a document some extra data so I had to change the way to update the data in the DB. – Diego Feb 16 '15 at 00:40

1 Answers1

0

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...
        }
Diego
  • 493
  • 1
  • 9
  • 26