Simple Save/Update to MongoDB using Mongoose in NodeJS

I am currently doing some side development with a few coworkers using NodeJS. We are trying to develop a mobile web application for playing the popular game, Mastermind. I am still getting a grasp of writing Javascript for server-side code, but it turns out that NodeJS is pretty neat. We are hosting our application with OpenShift. We are also using QUnit for out unit test (although I have not used it that much yet, perhaps when I dig into the details I’ll make another post).

My assignment on our first iteration was to save a leader board score. For now, we will define it as:

  • Name – string
  • Score – number
  • Move Count – number
  • Time Elapsed – number

Simple enough, right? Let’s jump right in. We will be using an node packaged module (NPM) called Mongoose. I won’t get into the details of how to install it, that documentation is easy enough to find. Instead I’ll concentrate on the code needed to do the save/update. First thing you need to do is require mongoose:

I am a self proclaimed C# / .NET guy, so I like to think of this like doing a standard import. Anyways… Back to JavaScript. Doing this ‘require’ will allow us to use the functionality of mongoose by making calls from the declared variable. The next step for us will be to connect to our database:

Once we  are connected to our database, we can create our schema. Connecting mongoose to the database before creating the model is important because the model needs to know which table to look at in the database (in this case, ‘my_table’):

For the purpose of this post, I’ll concentrate on Mongoose and we can just declare some simple variables for the score to be saved/updated. In our actual code, we are using an NPM called express to setup a service and post scores with the request object (check this informative blog post for more info on that). Here are the variables we’ll use:

That was the easy stuff. Now to save/update… Don’t worry, Mongoose makes it much easier than it would be. We will utilize the function ‘findOne’ from Mongoose and we will search by name (remember we declared it as ‘unique’ in the schema):

The rest of the code discussed will be inserted into the highlighted block above with the comment of ‘our code goes here’. The basic flow of logic here is as follows:

  1. Check for any errors, gracefully return them.
  2. Create temporary variable that will tell us if we need to save.
  3. Determine if the player has an existing score.
    1. If not, instantiate one.
    2. If so, check if the new score is higher and possibly set new values.
  4. Save based on our temporary variable.

That’s it! Mongoose makes the technical details of the database an afterthought. All we have to do is call ‘save’, as highlighted above.

Posted in Professional Tagged with: , , ,

Leave a Reply

Your email address will not be published. Required fields are marked *


Subscribe to my Insights via Email

Join 61 other subscribers

%d bloggers like this: