Last active
January 18, 2019 10:36
-
-
Save pablocattaneo/f91abbf9dfdcc06d4c07c0fc4c4a9378 to your computer and use it in GitHub Desktop.
How to get one document in mongodb? source: mongoShell: https://www.udemy.com/mongodb-the-complete-developers-guide/learn/v4/t/lecture/11739212?start=34
mongoDriverNode: http://mongodb.github.io/node-mongodb-native/3.1/api/Collection.html#findOne
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
.collection('collectionName').findOne({key:value}) | |
// Example | |
const db = require('../db') | |
const ObjectId = require('mongodb').ObjectId | |
exports.getUser = ((req, res) => { | |
console.log('getUser') | |
filter = { | |
_id: new ObjectId(req.params.userId) | |
} | |
db.getDb() | |
.db() | |
.collection('usersToSendEmail') | |
.findOne(filter) | |
.then((doc) => { | |
res.status(200).json({doc}) | |
}) | |
.catch((error)=>{ | |
console.log(error) | |
}) | |
}) |
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
db.collectionName.find({key:value}) | |
// Example | |
> db.usersToSendEmail.find({_id: ObjectId("5c405972f3d33522c6242d31")}) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment