Can I remove the content of a field while merging?

#1

I have an object Foo.make({id:‘bar’, a:1, b:2, msg:‘blabla’}) that is persisted.
I would like to merge it with Foo.make({id:‘bar’, a:2, b:3, msg:null})
“msg” field remains present, tried empty string instead of null without success.

0 Likes

#2

Specify the fields that should be merged in the MergeSpec, which is the optional second argument to merge():

Foo.merge({id: ‘bar’, a: 2, b: 3, msg: null}, {include: “a, b, msg”});

1 Like

#3

Another option is to use the 2 object version of upsert

Foo.upsert({id: ‘bar’, a: 2, b: 3}, {id: ‘bar’, msg: ‘oldVal’})

basically the merge/upsert cannot differentiate between a “missing” value and a “null” value

0 Likes

#4

It seems cumbersome to have to figure out the old value, I prefer the merge+include version from @henry.hsieh

0 Likes