mongodb-js / jsonpatch-to-mongodb

Convert JSON patches into a MongoDB update
MIT License
35 stars 27 forks source link

Not supporting nested objects #10

Open rxjs-space opened 7 years ago

rxjs-space commented 7 years ago

The doc in db:

{"details": {
    "parts": [
          {"id": "p001", "name": "abc"}
        ]
  }
}

the json-patch to apply:

[ { op: 'add',
    path: '/details/parts/2',
    value: { id: 'p002', name: 'xyz' } }]

the query generated:

{ '$push': { 'details.parts.2': { id: 'p002', name: 'xyz' } } }

with above query, the doc after change:

details.parts[1] === [{id: 'p002', name: 'xyz'}]

but the doc expected is actually:

details.parts[1] = {id: 'p002', name: 'xyz'}
marcj commented 6 years ago

I guess the missing check here is that when the last part of path: '/details/parts/2' which is here 2 is a numeric value, then only it should use $push, else $set.