{"action":"created","issue":{"url":"https://api.github.com/repos/Automattic/mongoose/issues/4711","repository_url":"https://api.github.com/repos/Automattic/mongoose","labels_url":"https://api.github.com/repos/Automattic/mongoose/issues/4711/labels{/name}","comments_url":"https://api.github.com/repos/Automattic/mongoose/issues/4711/comments","events_url":"https://api.github.com/repos/Automattic/mongoose/issues/4711/events","html_url":"https://github.com/Automattic/mongoose/issues/4711","id":189421103,"number":4711,"title":"There is no opportunity to enable required validation for Update ","user":{"login":"hrachyam","id":5027490,"avatar_url":"https://avatars.githubusercontent.com/u/5027490?v=3","gravatar_id":"","url":"https://api.github.com/users/hrachyam","html_url":"https://github.com/hrachyam","followers_url":"https://api.github.com/users/hrachyam/followers","following_url":"https://api.github.com/users/hrachyam/following{/other_user}","gists_url":"https://api.github.com/users/hrachyam/gists{/gist_id}","starred_url":"https://api.github.com/users/hrachyam/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/hrachyam/subscriptions","organizations_url":"https://api.github.com/users/hrachyam/orgs","repos_url":"https://api.github.com/users/hrachyam/repos","events_url":"https://api.github.com/users/hrachyam/events{/privacy}","received_events_url":"https://api.github.com/users/hrachyam/received_events","type":"User","site_admin":false},"labels":[],"state":"closed","locked":false,"assignee":null,"assignees":[],"milestone":null,"comments":2,"created_at":"2016-11-15T15:24:20Z","updated_at":"2016-11-16T20:45:16Z","closed_at":"2016-11-16T19:46:53Z","body":"Hello,\r\n\r\nI am able to enable Schema validation while updating document  using this attribute:\r\n    {runValidators: true} \r\nBut the validation \"required\" is still disabled.\r\n\r\nNote: \r\nit is OK for PATCH request but it is not OK for PUT request while implementing Restful API as all required fields must be specified in case of adding new document instead of updating one.\r\n\r\nIn the PUT I have used  { upsert: true }\r\n\r\n```javascript\r\napp.put('/employees/:id', function(req, res) {\r\n    var msg = 'Update or Create employee by Id: ' + req.params.id;\r\n    console.log(msg);\r\n    Employee.findOneAndUpdate({'emp_no' : req.params.id}, req.body,\r\n        { upsert: true, new: true, runValidators: true },\r\n        function (err, employee) {\r\n            if (err){\r\n                return res.send(err);\r\n            }\r\n            res.send(msg + '\\n');\r\n        });\r\n});\r\n```\r\n\r\n```\r\napp.patch('/employees/:id', function(req, res) {\r\n    var msg = 'Update employee by Id: ' + req.params.id;\r\n    console.log(msg);\r\n    Employee.findOneAndUpdate({'emp_no' : req.params.id}, req.body,\r\n        { new: true, runValidators: true },\r\n        function (err, employee) {\r\n            if (err){\r\n                return res.send(err);\r\n            }\r\n            res.send(msg + '\\n');\r\n        });\r\n});\r\n```\r\n\r\nEverything is OK for PATCH but for PUT it will be used like for POST. \r\n\r\n\r\n"},"comment":{"url":"https://api.github.com/repos/Automattic/mongoose/issues/comments/261066266","html_url":"https://github.com/Automattic/mongoose/issues/4711#issuecomment-261066266","issue_url":"https://api.github.com/repos/Automattic/mongoose/issues/4711","id":261066266,"user":{"login":"hrachyam","id":5027490,"avatar_url":"https://avatars.githubusercontent.com/u/5027490?v=3","gravatar_id":"","url":"https://api.github.com/users/hrachyam","html_url":"https://github.com/hrachyam","followers_url":"https://api.github.com/users/hrachyam/followers","following_url":"https://api.github.com/users/hrachyam/following{/other_user}","gists_url":"https://api.github.com/users/hrachyam/gists{/gist_id}","starred_url":"https://api.github.com/users/hrachyam/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/hrachyam/subscriptions","organizations_url":"https://api.github.com/users/hrachyam/orgs","repos_url":"https://api.github.com/users/hrachyam/repos","events_url":"https://api.github.com/users/hrachyam/events{/privacy}","received_events_url":"https://api.github.com/users/hrachyam/received_events","type":"User","site_admin":false},"created_at":"2016-11-16T20:45:16Z","updated_at":"2016-11-16T20:45:16Z","body":"Hello,\r\n\r\nThanks for the explanation.\r\n\r\napp.put('/employees/:id', function(req, res) {\r\n    Employee.findOne({'emp_no' : req.params.id}, function(err, employee) {\r\n        if (employee == null) {\r\n            var newEmployee = new Employee(req.body);\r\n            newEmployee.save(function (err) {\r\n                if (err){\r\n                    return res.send(err);\r\n                }\r\n                var msg = 'Created employee by Id: ' + req.params.id;\r\n                console.log(msg);\r\n                res.send(msg + '\\n');\r\n            });\r\n        } else {\r\n            employee.first_name = req.body.first_name;\r\n            employee.last_name = req.body.last_name;\r\n            employee.birth_date = req.body.birth_date;\r\n            employee.gender = req.body.gender;\r\n            employee.hire_date = req.body.hire_date;\r\n            employee.save(function (err) {\r\n                if (err){\r\n                    return res.send(err);\r\n                }\r\n                var msg = 'Updated employee by Id: ' + req.params.id;\r\n                console.log(msg);\r\n                res.send(msg + '\\n');\r\n            });\r\n        }\r\n    });\r\n});\r\n\r\napp.patch('/employees/:id', function(req, res) {\r\n    Employee.findOneAndUpdate({'emp_no' : req.params.id}, req.body,\r\n        { new: true, runValidators: true }, \r\n        function (err, obj) {\r\n            if (err){\r\n                return res.send(err);\r\n            }\r\n            if (obj === null) {\r\n                var msg = 'Employee by Id: ' + req.params.id + ' is not found';\r\n                console.log(msg);\r\n                res.send(msg + '\\n');\r\n            } else {\r\n                var msg = 'Updated employee by Id: ' + req.params.id;\r\n                console.log(msg);\r\n                res.send(msg + '\\n');\r\n            }\r\n        }\r\n    );\r\n});\r\n\r\n\r\nIs it correct way  to use mongoose?\r\n\r\nNOTE: I would like to use findOneAndUpfate with upsert: true in  post request also and not check employee existence and then call save methods to create or update employee.\r\n\r\nCould you please provide your opinion about the above mentioned?\r\n\r\nThanks,\r\nHrach"}}
{"actor":{"display_login":"hrachyam"}}