vogels icon indicating copy to clipboard operation
vogels copied to clipboard

$add and $del

Open elkhawajah opened this issue 8 years ago • 3 comments

how to handle adding and removing items at the same update call ?

elkhawajah avatar May 24 '16 12:05 elkhawajah

I don't know for sure but have you tried something like this:?

var data = {
  items: { $add: ['foo'], $del: ['bar'] }
}
Example.update(items, callback)

justinmchase avatar May 25 '16 21:05 justinmchase

Yes it does not work. Based on the UpdateExpression we cannot use nested attributes, I was storing nested attributes. So I tried a different approach :

// Assume model name is Blog
let blog = Blog.get('id');
// get array [{ .. fields .. }, { .. fields ..}, { ... fields .. }]
let tags = blog.get('tags');

// update tags to remove items from the array, then add new items then
Blog.update({
id : ..,
tags: tags
});

The result always the last item will be duplicated, i.e. assume objects are numbers [1, 2, 3], and I removed [2], and I added [4], the final result will be [1, 3, 4, 4].

No idea why it is duplicating last item. I tried it with a nested object (json) and an array of strings.

elkhawajah avatar May 25 '16 21:05 elkhawajah

What if you just did it in two separate calls?

Example.update({ tags: { $add: ['foo'] } }, (err) => {
  if (err) return callback(err)
  Example.update({ tags: { $del: ['bar'] } }, callback)
})

The $add and the $del definitely do work. You have to make sure that your property is setup like this though in the Table Schema:

tags: vogels.types.stringSet()

justinmchase avatar May 25 '16 22:05 justinmchase