Basics of NodeJS and MongoDB for Blockchain backend development
Introduction to Mongo DB
Backend software development companiesall over the world are facing a difficulty in storing this large amount of data. Since the traditional databases are unable to cope up with the increasing data storage requirements they are now left with two main options, i.e. Either to Scale-up or Scale-out. Scale-up basically means buying a Bigger machine which has a potential to store large amount of data and scaling-out is splitting of data across various machines. However, due to Heavy costs of these bigger machines, Scaling-up could be done only up to certain limit. So, there is now an urgent need to go for scale-out option for our databases and this is where MongoDB comes into Picture.
Before Diving deep into the basics of MongoDB, Fair understanding of NoSQL database is Paramount. NoSQL which basically stands for ‘not only SQL’ is a Non-Relational Database. It Encompasses wide variety of data models. They are typically used for working with large sets of Distributed data. NoSQL are majorly used in an application as where the need of scalability and performance outweighs the need for data consistency.
MongoDB was essentially designed in Mid-2000s to scale-out the Databases. It is a document-oriented model classified under NoSQL database which allows to split the data automatically across different servers. Every Database in MongoDB contains collections. These collections in turn contains documents. The size and content of documents can be different from each other. Unlike Relational databases there is no need of defining the schema beforehand. Data Models available within the MongoDB enables the storage of complex structures, Hierarchical relationships and arrays.
Commonly used Terms in MongoDB
_id - Every MongoDB document contains this a unique Value and is generated by default along with the creation of Document. It is like a primary key of the Document
Collection- Collection refers to the grouping of MongoDB documents. It is equivalent to the table in relational Database.
Document- Record in MongoDB Collection is Known as Document. It is Row equivalent in RDBMS which contains Field names and Values.
Field- Name-value pair in the document is called as Field. It is similar to Columns in RDBMS.
Example of Field with Key value pair
|CustomerID : 11|
Cursor- Pointer to the collection of documents returned is called cursor in MongoDB.
Advantages and Features of MongoDB
Document Oriented- Data is stored in a documents instead of relational schema.This makes it more flexible and adaptable to real time business requirements.
Ad hoc queries- Specific fields from the documents can be retrieved using the queries. Searching by field, regular expressions and range queries is possible with MongoDB.
Indexing- Any field in a MongoDB document can be indexed which in turn improves the performance of search in MongoDB.
Replication- MongoDB maintains two sets of replica primary and secondary, which ensures high availability. replica contains one or more instances of mongoDB.
Load balancing- In MongoDB concept of sharding to scale horizontally by splitting data across multiple MongoDB instances. Since MongoDB can run over multiple servers, balancing the load or data duplication can be achieved. This helps to keep the system up and running in case of hardware failure.
Node.js uses asynchronous programming. For instance, while requesting a file, it will send the task to the computer file system. Unlike PHP or ASP it does not wait for the file system to open and read the file. Rather it is ready to handle the next request. once the file system has opens and reads the file, the server returns the content to the client. Thus Node.js eliminates the waiting time.
Features of Node.js
Event-driven- which means that everything that happens in Node is in reaction to an event. For example, when a new request comes in the server will start processing it. If it then encounters a blocking I/O operation, instead of waiting for this to complete, it will register a callback before continuing to process the next event.
Speed- Rich library of Node.js helps in very fast execution of code.
Single Threaded but Highly Scalable- It uses single thread model to handle multiple concurrent requests. It uses event looping, which helps the server to respond in a non-blocking way, making it highly scalable unlike traditional servers
No Buffering- there is no buffering of data in Node.js. Applications simply output the data in chunks.