
Initialize a simple api server for node apps using Mongoose and Express, setting up package.json and dependencies. Create a basic server file with a request handler and run the host.
Connect to a MongoDB database with mongoose, define a schema, compile a model, and retrieve documents from the posts collection using model.find({}).
Create and save an object in a Mongoose model via a post route, using middleware to parse bodies, and rely on the save method to run pre/post hooks and validation.
Explore how Mongoose's rich schema configurations enforce validation, required fields, and error handling in NodeJS apps; learn to trim whitespace, ignore unknown fields, and prevent saving incomplete data.
Define and validate posts with Mongoose schema types, using regex, length constraints, booleans, and default values. Require fields, created and updated timestamps, and prevent invalid data from reaching the database.
Explore how the Ok module bubbles errors in node callbacks, reducing duplicate if/next/return logic and making code more readable and maintainable.
Learn to restrict field values with the enum schema property in mongoose, using a roles array that allows only user or admin, not super user.
Explore mongoose schema setters that intercept set operations for formatting and basic filtering, and getters that customize output, including lowercasing titles in JSON responses.
Explore Mongoose schemas, models, and document validation for a posts API, including nested structures and object IDs. Learn safe deletion by fetching a document and calling remove to perform delete.
Learn how Mongoose virtual fields act as non-physical properties to compute a post's comments. Use them in templates to show no comments or loop through comments.
discover how a pre save hook in mongoose runs before saving a document to automatically set the current timestamp, replacing manual updates and ensuring the timestamp reflects changes.
Explore how a Mongoose pre validate hook runs before validation to ensure a post with comments contains text and author IDs, throwing an error if required fields are missing.
Post save hooks in Mongoose run logic after a document is saved. The post save callback receives the document, enabling actions once the save completes.
Learn to use Mongoose populate to automatically replace author object ID references with full user data in post documents, enabling nested data retrieval via a populate map.
Learn to define static and instance methods in Mongoose, when to call on the model or a document, and move logic from routes into methods before creating models.
Relate models in mongoose, auto-populate data from documents, timestamp saves on each save, validate author IDs and existing users, and differentiate static versus instance methods for documents and models.
Learn how to store binary data in a mongoose database by defining a binary field, uploading with busboy, and storing large files in external storage when needed.
Watch a quick HackHall demo covering login with admin rights, profile view, comments and authored posts, post interactions (like, watch, remove), and approving or promoting members on the people page.
This case study demonstrates defining mongoose models from schemas, using finder creates plugin, and implementing user and post schemas with likes and comments via routes and DBI middleware.
This chapter is from Pro Express.js: Master Express.js—The Node.js Framework For Your Web Development which is available at proexpressjs (dot) com. It is a bonus content for Mongoose Course. It's for educational purposes only. Avoid sharing this document outside of this community.
Mongoose Course: Node and Mongo Explained
If you are seeing this page, then you are somewhat related to software engineering, and we all know that for people in tech learning a new technology is important. But we often procrastinate and defer it. And it’s not our fault, because in most cases there are just no good tutorials. This is especially true for new and emerging technologies like NodeJS.
NodeJS + NoSQL = <3
NodeJS (JavaScript on the server) is awesome, but some developers are confused about NoSQL when it comes to working in Node with MongoDB (NoSQL database). Do you need a better explanation of how to use Node.js with MongoDB? The most popular choice is the Mongoose library. Read on to find out why.
Why Mongoose is Your Friend
The danger of not using ORM (object-relational mapping) / ODM (object-document mapping) is that every time you make changes to the structure of the data, you have to modify the application code. In other words, if your application layer touches a database, then you’re in for a big trouble waiting to happen God forbid there are any changes to the database layer… and the chance of you having to make future changes in the software that you wrote is like 99.99%. :-( However, by using Mongoose you can mitigate this issue!
Also, you can make your application to have models, validations and at the same time organize code better with custom methods, and pre and post hooks. The additional benefit is that Mongoose methods are easier (and more fun) to use than native MongoDB driver ones.
Finally an Easy Way to Understand How to Use Node with MongoDB
Take a look at Mongoose’s extensive documentation at mongoosejs (dot) com (slash) docs. It’s fairly good, but who has time to read this documentation when you can use that time to write your own apps?! Also, reading online documentation and the library’s source code is boring. Many studies have shown that when something is boring, you learn it less effectively!
That’s why we’re working on a Mongoose course for you! This is the course on Mongoose you’ve been waiting for (as almost 200 backers of our “kickstarter” campaign proved).
What You’ll Be Able to Do
The Mongoose course has more than one hour of HD videos, and INSANE number of examples. After finishing lectures, answering quizzes and doing exercises outlined in the course, you’ll be able to do virtually anything with Node and MongoDB in a sane manner, using the industry’s best techniques and practices.
The course is ready and you can be access IMMEDIATELY… it’s time to take action to ensure you know the best way to use Node and MongoDB!