vogels
vogels copied to clipboard
$add and $del
how to handle adding and removing items at the same update call ?
I don't know for sure but have you tried something like this:?
var data = {
items: { $add: ['foo'], $del: ['bar'] }
}
Example.update(items, callback)
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.
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()