Information Expert Principle Applied To Mongoose Models

Adding a method to a Mongoose Model

In programming the Information expert, or the expert principle, is an approach used to determine where to delegate responsibilities. In other words, where should you place the code that completes specific tasks. The Information expert principle will help a developer to place the responsibility in the class with the most information required to fulfill it. In this tutorial we are going to clean up the process of generating JSON Web Tokens to make our code more clear and easier to maintain.…

Node.js MongoDB User Registration

mongodb user registration

So we are going to start building the most basic of User Registration systems in Node.js using MongoDB as the data store, Express as the routing system, Joi as the validator, and of course Mongoose to make interacting with Mongo from Node easy. Below we have our sample project layout. User-Registration is the top level directory which holds the index.js file, and then we have a models directory and a routes directory.…

Mongoose Relationships Tutorial

Mongoose Relationships Tutorial

NoSQL databases like MongoDB work differently than the older and more established Relational Databases like MySQL, Oracle, Microsoft SQL, and so on. Relationships in the traditional sense don’t really exist in MongoDB like they do in MySQL. In this tutorial we’ll take a look at how you can work with related data, even though it is not explicitly enforced by MongoDB. We’ll have a look at Reference Based Relationships (Normalization) as well as Embedded Documents Relationships (Denormalization).…

Mongoose Validation Examples

Mongoose Validation Examples

Just like all other frameworks, Mongoose provides a way to validate data before you save that data to a database. Data validation is important to make sure that “bad” data does not get persisted in your application. A benefit of using Mongoose when inserting data into MongoDB is its built-in support for data schemas, and the automatic validation of data when it is persisted. You would not get this without Mongoose.…

Mongoose Crud Tutorial

Mongoose Crud Tutorial

In this tutorial we are going to use Mongoose in our Node.js and MongoDB environment to create, read, update, and delete documents from the database. We will cover connecting to MongoDB using Mongoose, explore MongoDB Schemas, and review Models in MongoDB. From there we’ll move on to saving a document, as well as querying documents using comparison and logical expressions. We’ll also see how to update documents using Mongoose both with the Query First and Update First approaches.…