How to setup a simple application using node.js and MongoDB.
Simple application using node.js and MongoDB: Most of your interactions with Yeoman will be through the command line. Run commands in the Terminal app if you’re on Mac, your shell in Linux, or cmder (preferably) / PowerShell / cmd.exe if you are on Windows. Install prerequisites Before installing Yeoman, you will need the following: -Node.js v0.10.x+ -npm (which comes bundled with Node) v2.1.0+ -git You can check if you have Node and npm installed by typing: $node –version && npm –version If you need to upgrade or install Node, the easiest way is to use an installer for your platform. Download the .msi for Windows or .pkg for Mac from the NodeJS website. The npm package manager is bundled with Node, although you might need to update it. Some Node versions ship with rather old versions of npm. You can update npm using this command: $npm install –global npm@latest You can check if you have Git installed by typing: $git –version
The quick start guide will show you how to setup a simple application using node.js and MongoDB. Its scope is only how to set up the driver and perform the simple crud operations. For more in depth coverage we encourage reading the tutorials.
Let’s create a directory where our application will live. In our case we will put this under our projects directory.
Enter the following command and answer the questions to create the initial structure for your new project
Next we need to edit the generated package.json file to add the dependency for the MongoDB driver. The package.json file below is just an example and your will look different depending on how you answered the questions after entering
Save the file and return to the shell or command prompt and use NPM to install all the dependencies.
You should see NPM download a lot of files. Once it’s done you’ll find all the downloaded packages under the node_modules directory.
Let’s boot up a MongoDB server instance. Download the right MongoDB version from MongoDB, open a new shell or command line and ensure the mongod command is in the shell or command line path. Now let’s create a database directory (in our case under /data).
You should see the mongod process start up and print some status information.
Let’s create a new app.js file that we will use to show the basic CRUD operations using the MongoDB driver. First let’s add code to connect to the server and the database myproject.
Given that you booted up the mongod process earlier the application should connect successfully and print Connected correctly to server to the console. Let’s Add some code to show the different CRUD operations available.
Let’s create a function that will insert some documents for us.
The insert command will return a results object that contains several fields that might be useful.
- result Contains the result document from MongoDB
- ops Contains the documents inserted with added _id fields
- connection Contains the connection used to perform the insert
Let’s add call the insertDocuments command to the MongoClient.connect method callback.
We can now run the update app.js file.
You should see the following output after running the app.js file.
Let’s look at how to do a simple document update by adding a new field b to the document that has the field a set to 2.
The method will update the first document where the field a is equal to 2 by adding a new field b to the document set to 1. Let’s update the callback function from MongoClient.connect to include the update method.
Next lets delete the document where the field a equals to 3.
This will delete the first document where the field a equals to 3. Let’s add the method to the MongoClient .connect callback function.
Finally let’s retrieve all the documents using a simple find.
We will finish up the Quickstart CRUD methods by performing a simple query that returns all the documents matching the query.
This query will return all the documents in the documents collection. Since we deleted a document the total documents returned is 2. Finally let’s add the findDocument method to the MongoClient.connect callback.
This concludes the QuickStart of connecting and performing some Basic operations using the MongoDB Node.js driver. For more detailed information you can look at the tutorials covering more specific topics of interest.