mongoose response to object
MyModel.findOne().lean().exec(function(err, doc) {
doc.addedProperty = 'foobar';
res.json(doc);
});
mongoose response to object
MyModel.findOne().lean().exec(function(err, doc) {
doc.addedProperty = 'foobar';
res.json(doc);
});
findbyid mongoose
// Find the adventure with the given `id`, or `null` if not found
await Adventure.findById(id).exec();
// using callback
Adventure.findById(id, function (err, adventure) {});
// select only the adventures name and length
await Adventure.findById(id, 'name length').exec();
mongoose where
exports.generateList = function (req, res) {
subcategories
.find({})//grabs all subcategoris
.where('categoryId').ne([])//filter out the ones that don't have a category
.populate('categoryId')
.where('active').equals(true)
.where('display').equals(true)
.where('categoryId.active').equals(true)
.where('display').in('categoryId').equals(true)
.exec(function (err, data) {
if (err) {
console.log(err);
console.log('error returned');
res.send(500, { error: 'Failed insert' });
}
if (!data) {
res.send(403, { error: 'Authentication Failed' });
}
res.send(200, data);
console.log('success generate List');
});
};
mongoose get document
//Router file. After doing all your imports at beginning of file
const router = express.Router();
app.use('/pathforpostrequests', router);
router.get('/:name', controller.getPerson, (req, res, next) => {
res.status(200).json(res.locals.person);
});
// Controller file. After doing all your imports at beginning of file. Person is an example mongo schema you will need to setup in a schema file.
const controller = {
getPerson (req, res, next) {
Person.findOne({ firstName: req.params.name }).exec()
.then((result) => {
res.locals.person = result;
next();
})
},
}
Copyright © 2021 Codeinu
Forgot your account's password or having trouble logging into your Account? Don't worry, we'll help you to get back your account. Enter your email address and we'll send you a recovery link to reset your password. If you are experiencing problems resetting your password contact us