{"action":"created","issue":{"url":"https://api.github.com/repos/JackAdams/meteor-transactions/issues/14","labels_url":"https://api.github.com/repos/JackAdams/meteor-transactions/issues/14/labels{/name}","comments_url":"https://api.github.com/repos/JackAdams/meteor-transactions/issues/14/comments","events_url":"https://api.github.com/repos/JackAdams/meteor-transactions/issues/14/events","html_url":"https://github.com/JackAdams/meteor-transactions/issues/14","id":61614954,"number":14,"title":"Support for $addToSet: {$each ....}","user":{"login":"rjsmith","id":678653,"avatar_url":"https://avatars.githubusercontent.com/u/678653?v=3","gravatar_id":"","url":"https://api.github.com/users/rjsmith","html_url":"https://github.com/rjsmith","followers_url":"https://api.github.com/users/rjsmith/followers","following_url":"https://api.github.com/users/rjsmith/following{/other_user}","gists_url":"https://api.github.com/users/rjsmith/gists{/gist_id}","starred_url":"https://api.github.com/users/rjsmith/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/rjsmith/subscriptions","organizations_url":"https://api.github.com/users/rjsmith/orgs","repos_url":"https://api.github.com/users/rjsmith/repos","events_url":"https://api.github.com/users/rjsmith/events{/privacy}","received_events_url":"https://api.github.com/users/rjsmith/received_events","type":"User","site_admin":false},"labels":[],"state":"open","locked":false,"assignee":null,"milestone":null,"comments":31,"created_at":"2015-03-14T14:00:15Z","updated_at":"2015-03-25T10:54:48Z","closed_at":null,"body":"Hi,\r\nI have a use case in my app where I need to add multiple items to an array field for an existing document. I use the `$addToSet: {arrField: {$each: [someObject, anotherObject]}}` update modifier to do the update in single atomic MongoDB operation.\r\n\r\nNow I am in the process of integrating meteor-transactions into my app, I realised that  modifiers of this form do not work.  Looking in the code, I can see there is no explicit support for any of the `$addToSet` array modifiers. There are two issues:\r\n\r\n1. When meteor-transactions tries to save the resulting tx.Transactions document, it fails with a MongoDB error because some of the field keys (in the `update` and `inverse` command objects) have a key that starts with $ (the `$each` key in the original update modifier)\r\n2. The inverse command is not correct (it changes the `$addToSet` command to a `$pull` command, but `$each` is not supported as a `$pull` modifier.  Actually, I don't think you can inverse `$addToSet` with `$pull` so easily, because addToSet only adds more objects if they do not already exist in the array being modified. So , when inversing, you have to be careful not to delete array items that were in the addToSet modifier, but were already in the array before then. Phew!\r\n\r\nAfter working on this today, I think I have a solution for number 2.  I think the inverse operation of `$addToSet` should be a `$set` of the complete state of the array field prior to the `$addToSet`. It's a brute force approach vs unpicking it with `$pull`, but should work for any `$addToSet` operation. Penalty is storing a larger amount of data in tx.Transactions. I my own use case, I will only max 100 items in these array fields, with each array item object object just being a couple of small strings.\r\n\r\nI have started working on number 1, but am currently stuck somewhere in the packaging / unpackaging methods. Basically, need to ensure that any field keys starting with $ get replaced with something else before saving, and then un-replaced when the tx.Transaction doc is read back out of the db again.\r\n\r\nWill update this issue when I have made some more progress.\r\n"},"comment":{"url":"https://api.github.com/repos/JackAdams/meteor-transactions/issues/comments/85981673","html_url":"https://github.com/JackAdams/meteor-transactions/issues/14#issuecomment-85981673","issue_url":"https://api.github.com/repos/JackAdams/meteor-transactions/issues/14","id":85981673,"user":{"login":"rjsmith","id":678653,"avatar_url":"https://avatars.githubusercontent.com/u/678653?v=3","gravatar_id":"","url":"https://api.github.com/users/rjsmith","html_url":"https://github.com/rjsmith","followers_url":"https://api.github.com/users/rjsmith/followers","following_url":"https://api.github.com/users/rjsmith/following{/other_user}","gists_url":"https://api.github.com/users/rjsmith/gists{/gist_id}","starred_url":"https://api.github.com/users/rjsmith/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/rjsmith/subscriptions","organizations_url":"https://api.github.com/users/rjsmith/orgs","repos_url":"https://api.github.com/users/rjsmith/repos","events_url":"https://api.github.com/users/rjsmith/events{/privacy}","received_events_url":"https://api.github.com/users/rjsmith/received_events","type":"User","site_admin":false},"created_at":"2015-03-25T10:54:48Z","updated_at":"2015-03-25T10:54:48Z","body":"Cool. I will carry on testing for rest of today and send a PR by end of my day today (UK time), or give another update here .. thanks for your support."}}