How to Use Mongoose for Mongodb & Nodejs

How to Use Mongoose for Mongodb & Nodejs

Here we show how to use Mongoose to save data to a MongoDB.

(This article is part of our MongoDB Guide. Use the right-hand menu to navigate.)

What is Mongoose?

In this example, we will use NodeJS. Do not worry if you do not know NodeJS. We will explain every line of code. It is server-side JavaScript. It is a little difficult to understand at first, in part because it is asynchronous, meaning multi-threaded. So you have to use either:

  • Synchronous functions only
  • Callbacks

Otherwise when you do statement n, statement n+1 will run right away too. So you can be, for example, processing a text file before you have read it completely.

Mongoose is an API on top of an API. It makes using the MongoDB NodeJS API easier to use.

Prerequisites

First, you need NodeJS version 6 or higher. Then install mongoose like this:

npm install mongoose
Robert Thorne
Author

Robert Thorne

Robert Thorne covers electric vehicle innovations, autonomous driving systems, global mobility trends, and automotive engineering developments.