MongoDb

Ethan Hess
4 min readOct 22, 2018

--

MongoDB is an open-source, non-relational, or NoSQL, database that stores data as documents in a binary representation called BSON, which stands for Binary JSON. Essentially what BSON is, is a binary form for representing simple data structures, associative arrays, and various data types of specific interest to MongoDB. Furthermore, MongoDB’s document data model maps naturally to objects in application code, making it simple for developers to learn and use. Documents also give you the ability to represent hierarchical relationships to store arrays and other more complex structures fairly easily.

So to get started, we must know that MongoDB applications consist of the three basic components: Establishing a connection to a MongoDB instance. Having logic to access and manipulate database data. Closing the connection to the MongoDB instance.

Before any requests can be made to MongoDB, a connection must first be created. Establishing a connection is similar to placing a telephone call in that you need the “phone number” of the MongoDB instance and the database it will interact with. This “phone number” is made up of the host name of the MongoDB instance and the specific database name

In addition to the “phone number” there are other optional parameters such as the user id and password, for this demonstration that wont be necessary, but typically for security reasons you would want to have those optional parameters as well incase the database contains sensitive information

So, since database access is typically distributed across more than one source file in an application, a best practice is to isolate the host and database names into a config.js file that is shared across all application components. The advantage of this is that altering either or both of these host or database names only requires an update to the config.js file, rather than to each application source file that requires a connection. The contents of the config.js file are on the right side of the screen. As you can see we create a config object with a property of db set to another object. We then create properties on the config.db object set to the host and database names, in this case we have them set to a local host and a name of ‘account’. We then export the config object so that it can be used in other components that require this file.

So now we move on to a file named index.js to establish a MongoDB connection using the properties in the config.js file. To do so, a require must be issued to allow the properties in it to be referenced from within the source file creating the connection. These properties are then used to create the URI connection string and a MongoClient reference that subsequent database requests will be issued through. The first such request is mongoClient.connect(mongoUri) which creates the connection and returns a reference to the client instance.

Once a connection has been established, the application is ready to access and manipulate documents in the database. The first step in this process is to define the document collection within the database. This is conceptually similar to a table in a relational database, but unlike those tables whose rows all have the same format, a collection can contain different types of documents, each having a different format. In this example the collection is created by the function call: collection = accountsDb.collection(‘accounts’); which defines a reference to a collection called ‘accounts’. Once the collection is created it can be used to invoke MongoDB function calls to access documents in the database. For example, collection.count()returns the number of documents in the collection.

After the application has completed its goal and no more data is to be retrieved from the database, it’s a good practice to gracefully terminate the connection to the MongoDB instance. This is accomplished by simply closing the connection. In the example above this is done by calling the accountsDb.close() function to stop the connection and free up any resources devoted to it. Also just to add, you may have noticed that in the first .then(db) function, the value of db was saved to a variable called accountDb. This was done in anticipation of it being needed later in the application logic to close the connection. And it was necessary since the scope of db is limited to that first .then(db) function. One additional consideration is there are cases where you may not want to close the connection. Since opening and closing connections are relatively expensive, high volume applications might want to use connection pooling to reuse connections rather than continually opening and closing new ones.

In conclusion, MongoDB is a popular NoSQL database solution that suits modern development requirements. It has a great query DSL, wonderful developer documentation and libraries, and built in support for scaling and replication. When it comes time to choose your production database however-as with any technology — you should do your research, and find out if MongoDB fits your requirements.

--

--