find all documents in mongoose code example
Example 1: find all mongoose
const mongoose = require("mongoose");
const Schema = mongoose.Schema;
const StudentSchema = new Schema({
name: String,
dob: Date,
mobile: String,
address: String,
});
module.exports = mongoose.model("students", StudentSchema);
const STUDENT = require("../models/Student");
exports.getProducts = (req, res, next) => {
STUDENT.find().then((p) => {
res.send(p);
});
};
exports.getProducts = (req, res, next) => {
STUDENT.find({},(err,p)=>{
res.send(p);
});
};
Example 2: mongoose where
exports.generateList = function (req, res) {
subcategories
.find({})
.where('categoryId').ne([])
.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');
});
};