mongoose find all documents code example

Example 1: find all mongoose

//create model students

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) => {
	//fetch all data student
  STUDENT.find().then((p) => {
    res.send(p);
  });
};

exports.getProducts = (req, res, next) => {
	//fetch all data student
  STUDENT.find({},(err,p)=>{
  	 res.send(p);
  });
};

Example 2: mongoose select

// include a and b, exclude other fields
query.select('a b');
// Equivalent syntaxes:
query.select(['a', 'b']);
query.select({ a: 1, b: 1 });

// exclude c and d, include other fields
query.select('-c -d');

// Use `+` to override schema-level `select: false` without making the
// projection inclusive.
const schema = new Schema({
  foo: { type: String, select: false },
  bar: String
});
// ...
query.select('+foo'); // Override foo's `select: false` without excluding `bar`

// or you may use object notation, useful when
// you have keys already prefixed with a "-"
query.select({ a: 1, b: 1 });
query.select({ c: 0, d: 0 });

Example 3: 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');
        });
    };