Created
July 3, 2019 16:05
-
-
Save codebubb/8d6bfa75f4581f1f21033d18fea4d855 to your computer and use it in GitHub Desktop.
MongoDB RestFul calls
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| router.get('/', (req, res, next) => { | |
| const users = req.app.locals.users; | |
| users | |
| .find({}) | |
| .toArray() | |
| .then(data => res.json(data)); | |
| }); | |
| router.post('/', (req,res, next) => { | |
| const users = req.app.locals.users; | |
| const document = req.body; | |
| users | |
| .insert(document) | |
| .then(data => res.json(data)); | |
| }); | |
| router.get('/:id', (req, res, next) => { | |
| const users = req.app.locals.users; | |
| const id = ObjectId(req.params.id); | |
| users | |
| .findOne({ _id: id }) | |
| .then(data => res.json(data)); | |
| }); | |
| router.patch('/:id/languages', (req,res, next) => { | |
| const users = req.app.locals.users; | |
| const id = ObjectId(req.params.id); | |
| const languages = req.body.languages; | |
| users | |
| .updateOne({ _id: id }, { $set: { languages: languages }}) | |
| .then(data => res.json(data)); | |
| }); | |
| router.put('/:id', (req,res, next) => { | |
| const users = req.app.locals.users; | |
| const id = ObjectId(req.params.id); | |
| const document = req.body; | |
| users | |
| .replaceOne({ _id: id }, document) | |
| .then(data => res.json(data)); | |
| }); | |
| router.delete('/:id', (req,res, next) => { | |
| const users = req.app.locals.users; | |
| const id = ObjectId(req.params.id); | |
| users | |
| .deleteOne({ _id: id }) | |
| .then(data => res.json(data)); | |
| }); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment