NodeJs and MongoDb on Windows : Connecting and Inserting<!-- --> | <!-- -->Patrick Desjardins Blog
Patrick Desjardins Blog
Patrick Desjardins picture from a conference

NodeJs and MongoDb on Windows : Connecting and Inserting

Posted on: August 7, 2017

MongoDb is an interesting choice of permanent persistence when using NodeJs since it stores document which is ideal of JSON document to get stored.

You can download a free version directly on MongoD : Once it's downloaded, it's best to be sure that the system environment variable is set to have an entry in the path to point to the bin folder of the installation path of MongoDb.

Before working out MongoDb with NodeJs, I recommend to download RoboMongo for free: This tool allows to query MongoDb and explore the data. Before using RoboMongo or to use the MongoDb library, we need to run the MongoDb server. To do so, go in the development folder you are working and execute the mongod command with the dbpath. For example, the following command will store the MongoDb in the "data" folder in the development folder.

1mongod --dbpath=.\\data

Inside your NodeJs project, you needs sto have the mongodb library. If you are using TypeScript, you can get the type definition too.

1npm install mongodb --save npm install @types/mongodb --save-dev

At this point, you can start using the library to access the information. First step, connecting to the server. Second step, connecting to the collection. The first step is the same as any other database, the second one is just that every documents are stored into a collection. Think about it as a table.

From here, we need to import some classes.

1import { MongoClient, MongoError, Db, InsertOneWriteOpResult } from "mongodb";

MongoClient is the main class to connect to Mongo. MongoError is the class that wrap the error which we will use to get information about the connection's error. Db is the class that hold information about MongoDb once connected. We need it to properly close the connection, but also to select the collection in which we want to invoke the action (find, insert, delete). The InsertOneWriteOpResult is the result on an insert.

Here is the connection code:

1MongoClient.connect(url, (errMongo: MongoError, db: Db) => {
2 if (errMongo) {
3 console.log(errMongo);
4 } else {
5 console.log("Connected successfully to server");
6 }

To insert something in you need to get the collection name and use the insert method. Something I haven't yet figure out perfectly is how to handle Date from a .json file. In my case, I was opening file (.json) and inserting them into MongoDb. The Json parse method was returning the date as string, hence I needed to assign the value back with an instance of date

1// To have a real date format in MongoDb
2objToInsert.fullDate = new Date(objToInsert.fullDate);
4// Access the collection we want to insert
5const collection = db.collection("documents"); // "documents" can be anything you want your collection to be named
7// Insert with a callback that has an error or the result data
9 objToInsert,
10 (err: MongoError, result: InsertOneWriteOpResult) => {}

The code above will add a new entry and alter the object to add an _id with a GUID for the object. This way, every entry has a unique identifier automatically.