A sample

Just a sample for today. Be sure to read chapter 16 and look at the Mongoose Documentation.

lib/models/Users2.js
var mongoose = require('mongoose');
var bcrypt = require('bcrypt');

var roleValidator = function(role) {
	return role === 'student' || role === 'instructor';
};

var usersSchema = mongoose.Schema({
	uid: {
		type: String,
		required: true,
		index: true,
		unique: true
	},
	name: {
		type: String,
		required: true
	},
	role: {
		type: String,
		default: 'student',
		//validate: roleValidator
		match: /(student|instructor)/
	},
	passwordHash: String,
	DOB: Date,
	enrollments: [String]
});

usersSchema.virtual('password')
	.set(function(password) {
		this.passwordHash = bcrypt.hashSync(password, 10);
	});

usersSchema.methods.checkPassword = function(password) {
	if (!this.passwordHash) return false;
	return (bcrypt.compareSync(password, this.passwordHash));
};

usersSchema.query.byUID = function(uid) {
	return this.where({uid: uid});
};

usersSchema.query.byRole = function(role) {
	return this.where({role: role});
};

var Users2 = mongoose.model('User2', usersSchema);

module.exports = Users2;
lib/routes.js
var _ = require('lodash');
var express = require('express');
var router = express.Router();
var Users = require('./models/Users');
var Users2 = require('./models/Users2');

router.route('/users')
	.get(function(req, res) {
		var query = Users2.find();
		if (req.query && req.query.role) {
			query = query.byRole(req.query.role);
		}
		query.exec()
			.catch(err => {
				res.status(500);
				res.json(err);
			})
			.then(docs => {
					res.status(200);
					res.json(docs);
				});
	})
	.post(function(req, res) {
		var newUserInfo = req.body;
		var newUser = new Users2(newUserInfo);
		newUser.save()
			.catch(err => {
				res.status(500);
				res.json(err);
			})
			.then(doc => {
				res.status(200);
				res.json(doc);
			});
	});

router.route('/users/:userId')
	.get(function(req, res) {
		Users2.findOne({uid: req.params.userId}).exec()
			.catch(err => {
				res.status(404);
				res.jsonp(err);
			})
			.then(doc => {
				res.status(200);
				res.jsonp(doc);
			})
	})
	.put(function(req, res) { // start paying attention here
		if (req.params.userId !== String(req.body.uid)) {
			res.status(400);
			return res.jsonp("Your user IDs don't match.");
		}

		// First, find any documents that may be in the system
		// I added .byUID in the model for ease. You can do the same by mimicking the .byRole method we created
		Users2.findOne().byUID(req.body.uid)
			.exec()
			.catch(err => res.status(500).json({error: err})) // if something failed, it's probably our fault at this stage, 500
			.then(doc => {
				// If we got a doc back, we can update it and save it
				if (doc) {
					// doc.set will wholesale update the document with new contents
					doc.set(req.body);
					return doc.save()
						.catch(err => res.status(400).json({error: err})) // errors here are probably schema issues, validate will run
						.then(doc => res.status(200).json(doc)); // otherwise, return the doc
				}
				// If we didn't find a doc, then we need to create a new one
				var u = new Users2(req.body);
				u.save()
					.catch(err => res.status(400).json({error: err}))
					.then(doc => res.status(200).json(doc));
			});
	});

module.exports = router;
index.js
var MongoClient = require('mongodb').MongoClient;
var express = require('express');
var bodyParser = require('body-parser');
var app = express();
var mongoose = require('mongoose');

app.use(bodyParser.json());

app.use('/v1', require('./lib/routes'));

function connectToMongo(url, db, cb) {
	var options = {
		useNewUrlParser: true
	};

	MongoClient.connect(url, options, function(err, client) {
		if (err) {
			console.log('Error connecting to Mongo');
			return cb(err);
		}
		console.log('Connected successfully to server');

		cb(null, client.db(db));
	});
}

connectToMongo('mongodb://localhost:27017', 'test', function(err, client) {
	if (err) {
		console.log(err);
		return;
	}

	mongoose.connect('mongodb://localhost/test');
	var db = mongoose.connection;
	app.locals.mdb = db;

	app.locals.db = client;
	app.listen(8080, function() {
		console.log('Listening on 8080');
	});
});