Getting started with mongoose
So these days I have been experimenting with express as a way to make node.js powered full stack applications. In many of these simple projects I have been using lowdb as a database solution, which works fine when it comes to simple projects that are going to be used off line. However if I aim to make something that will be deployed to a server, I am going to want to use something a little more professional, for this there is of course mongodb, and mongoose.js. This post will be a getting stared post on using mongoose.js to work with mongodb as a database solution.
1 - What to know
1.1 Version numbers matter
As of the writing of this post I am using nodejs v8.9.3, with mongodb v4.0.0, and mongoose 5.1.7. If you run into trouble reproducing what I have work out here always check the simplest things first.
2 - A Basic Example using mongoose.js
So This basic example of using mongoose to work with mongodb will be a simple command line interface tool that will create, and list instances of a Model called Day that represents the amount of users that visit a site in a certain day. This will be a basic exercise of using mongoose to connect to the mongodb service that should be running in the os, and add a few instnaces of a Model to a collection of models in a database.
2.1 - setup
For this project I will just need node.js, and mongodb installed, and the mogodb service running on the default port. When it comes to npm packages the only package I need is mongoose.
2.2 - The basic.js file
The basic js file will consist of all the code that will connect to mondodb, parse arguments from the command line, and create instances of a model.
2.2.1 - Make the connection
I stared off basic.js, by grabbing a reference to what is reported by the mongoose package, and then proced to write the code that will define the mongodb url that is used to connect to mongo db. This is a sting that defined the protocol to use to connect to mongodb, as wel as the hostname, port, and the name of the database.
Once I have the mongodb url in order I can then use that to make a connection to the mongodb service that should be up and running. If you run into trouble look into the port and hostname values and make sure they will work with your setup.
I then finish up my making a reference to mongoose.connection.
2.2.2 - The Day Model
After that I define the code that will define my Model.
Nothing fancy here I just create an instance of a Day Model by calling mongoose.model, followed by the name of the model, and the schema of the model. There is a great deal more to write about when it comes to models, But this is a basic example so I will be keeping it basic.
2.2.3 - The Options object
After I have my connection setup, and a model defined, then I define an object that will be used to create instnaces of that model, as well as get a list of them, and drop the whole databse.
These methods will of course be used in the next portion of the script in which I am defining some event handers as to what to do when a connection has been established successfully.
2.2.4 - On Error, and once open
Once I have the bulk of my logic together then it is just a matter of doing what is to be done when everything goes as plain, or not. Here I use the reference to mongoose.connection to set up when is to be done in the event of an error, or once the database is open and ready to be used.
When the script is called from the command line, by default it will just list any instances of the Day model that might be in the database. I can then give it arguments to create instances of day, or drop the whole database and start over.