find all 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: findbyid mongoose
await Adventure.findById(id).exec();
Adventure.findById(id, function (err, adventure) {});
await Adventure.findById(id, 'name length').exec();
Example 3: mongoose select
query.select('a b');
query.select(['a', 'b']);
query.select({ a: 1, b: 1 });
query.select('-c -d');
const schema = new Schema({
foo: { type: String, select: false },
bar: String
});
query.select('+foo');
query.select({ a: 1, b: 1 });
query.select({ c: 0, d: 0 });