{"action":"created","issue":{"url":"https://api.github.com/repos/LearnBoost/mongoose/issues/2642","labels_url":"https://api.github.com/repos/LearnBoost/mongoose/issues/2642/labels{/name}","comments_url":"https://api.github.com/repos/LearnBoost/mongoose/issues/2642/comments","events_url":"https://api.github.com/repos/LearnBoost/mongoose/issues/2642/events","html_url":"https://github.com/LearnBoost/mongoose/issues/2642","id":56357676,"number":2642,"title":"Temporary (virtual) properties on model instance","user":{"login":"whitecolor","id":736697,"avatar_url":"https://avatars.githubusercontent.com/u/736697?v=3","gravatar_id":"","url":"https://api.github.com/users/whitecolor","html_url":"https://github.com/whitecolor","followers_url":"https://api.github.com/users/whitecolor/followers","following_url":"https://api.github.com/users/whitecolor/following{/other_user}","gists_url":"https://api.github.com/users/whitecolor/gists{/gist_id}","starred_url":"https://api.github.com/users/whitecolor/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/whitecolor/subscriptions","organizations_url":"https://api.github.com/users/whitecolor/orgs","repos_url":"https://api.github.com/users/whitecolor/repos","events_url":"https://api.github.com/users/whitecolor/events{/privacy}","received_events_url":"https://api.github.com/users/whitecolor/received_events","type":"User","site_admin":false},"labels":[{"url":"https://api.github.com/repos/LearnBoost/mongoose/labels/help","name":"help","color":"FFFFFF"}],"state":"closed","locked":false,"assignee":null,"milestone":null,"comments":9,"created_at":"2015-02-03T10:12:39Z","updated_at":"2015-02-18T07:40:24Z","closed_at":"2015-02-07T23:06:35Z","body":"Sometimes I need to have on a model instance some temporaty property that is not in DB but for example I want to send to the client.\r\n\r\nThe scenario:\r\n\r\nsay I a have a ```Parent``` Model and ```Child``` model, I store them in separeate collections and only ```Child``` model has reference to it's parent\r\n```javascript\r\nParent.findOne({..}, function(err, parent){\r\n    Child.find({parent: parent.id}, function(){err, children}{\r\n        // now I want to send to the client parent instance with children as nested array\r\n          parent.children = children\r\n          res.send(parent)\r\n    })\r\n})\r\n```\r\n\r\nthe problem is that  ```parent.children = children``` won't work if ```children``` property is not defined on ```Parent``` schema. But its only temporary property and should not be saved to DB, it is not reasonable (and just incorrect) to define it on ```Parent``` schema.\r\n\r\nThe workaround I found is to add the followin virtual peroperty to ```Parent``` model:\r\n\r\n```javascript\r\nparentSchema.virtual('children').get(function(){\r\n    return this.__children\r\n}).set(function(val){\r\n    this.__children = val\r\n})\r\n```\r\n\r\nI thought maybe something like that could work for such proerties:\r\n```javascript\r\n// that would allow setting ```children``` but do not store it in DB\r\nparentSchema.virtual('children') \r\n```\r\n\r\nWhat do you think?"},"comment":{"url":"https://api.github.com/repos/LearnBoost/mongoose/issues/comments/74824523","html_url":"https://github.com/LearnBoost/mongoose/issues/2642#issuecomment-74824523","issue_url":"https://api.github.com/repos/LearnBoost/mongoose/issues/2642","id":74824523,"user":{"login":"whitecolor","id":736697,"avatar_url":"https://avatars.githubusercontent.com/u/736697?v=3","gravatar_id":"","url":"https://api.github.com/users/whitecolor","html_url":"https://github.com/whitecolor","followers_url":"https://api.github.com/users/whitecolor/followers","following_url":"https://api.github.com/users/whitecolor/following{/other_user}","gists_url":"https://api.github.com/users/whitecolor/gists{/gist_id}","starred_url":"https://api.github.com/users/whitecolor/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/whitecolor/subscriptions","organizations_url":"https://api.github.com/users/whitecolor/orgs","repos_url":"https://api.github.com/users/whitecolor/repos","events_url":"https://api.github.com/users/whitecolor/events{/privacy}","received_events_url":"https://api.github.com/users/whitecolor/received_events","type":"User","site_admin":false},"created_at":"2015-02-18T07:40:24Z","updated_at":"2015-02-18T07:40:24Z","body":"`what if you have a schema path called __children` \r\n\r\nyes I undertand it, `__children` its just my local implementation, mongoose could implement it some how diffrently iternaly. Any way if you don't like it, not a big problem."}}