25

The State of NoSQL with MongoDB and Node.js 2018

 5 years ago
source link: https://www.tuicool.com/articles/hit/Y3ymqqJ
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

The State of NoSQL with MongoDB and Node.js 2018

The NoSQL vs SQL database debate is very opinionated and a lot has changed over time. So, in this post, we will see what is the current state of different database strategies in brief and go into details of MongoDB and its connection with Node.js. In the end, you should be comfortable with the jargons of NoSQL and should be ready to start your journey with MongoDB and Node.js.

Background and History

QJr2Eri.jpg!web
Source: Reddit
  • Databases are classified into two types SQL databases and OTHER databases.
  • Other databases can be anything ranging from the physical files on your desk to complex electronic documents stored and managed by a DBMS on some remote server.
  • These other databases also include NoSQL. Which initially was a database without a structure(NO SQL), but now allows combining SQL with the flexibility of NoSQL(Not only SQL).

Shared in Bit’s blog

Bit turns components into building blocks, and you are the architect. Share, discover and develop components together to build faster with your team.

Types of NoSQL

  • Key-value stores: Every item in the database is stored as a pair of key(attribute) and its value. Popular examples are Riak, Voldemort, and Redis. Use cases are to store user preferences or session data.
  • Column-based stores: Each storage block contains data from only one column, and are optimized for queries over large datasets. The most popular are Cassandra and HBase. Systems that require heavy write requests like blogging platforms may use this.
  • Document-based databases pair each key with a complex data structure known as a document. Documents can contain many different key-value pairs, or key-array pairs, or even nested documents. The database is in JSON format. MongoDB is the most popular of these databases. Some use cases are e-commerce and blogging platforms.
  • Graph databases: They are used to store information about various types of networks. It uses edges and nodes to represent and store data. Examples are Neo4J and HyperGraphDB. Use cases include a social network, fraud detection, etc.

In this post, we will be focusing on the document-based database MongoDB.

Basic Terms

nEBfYvz.png!web
Left: SQL — Right: NoSQL

SQL:A database has tables. A table has rows and columns. All elements in a column has the same datatype.

NoSQL:A database has collections. A collection has documents. A document has fields. Two documents in a collection may or may not have the same fields, that is, the structure of the document is not fixed.

Why?, When? and Where? of NoSQL

Why use NoSQL?

Schema agnostic:NoSQL databases are schema agnostic. You aren’t required to do a lot on designing your schema before you can store data in NoSQL databases. You can start coding and store and retrieve data without knowing how the database stores and works internally. If you need advanced functionality, then you can customize the schema manually before indexing the data. Schema agnosticism may be the most significant difference between NoSQL and relational databases.

Scalability:SQL databases are vertically scalable/scaling-in. It means that an increase in load can be managed by increasing the CPU, RAM, SSD, etc., on a single server. NoSQL databases support horizontal scaling/scaling-out methodology that makes it easy to add or reduce capacity quickly without touching with commodity hardware. This eliminates the tremendous cost and complexity of manual sharding that is necessary when attempting to scale RDBMS.

Performance:Scaling is easier so performance improvement is possible. Commodity servers which are cheaper can be added to increase performance. Also querying the database does not involve the overhead of performing complex joins on tables.

Global availability:By automatically replicating data across multiple servers, data centers or cloud resources, distributed NoSQL databases can minimize latency and ensure a consistent application experience wherever users are located. An added benefit is that there is no need for specialized DBAs since there isn’t much work for them.

When to use NoSQL?

After reading the above advantages, you may be thinking of using MongoDB for all your future projects. But wait! There are specific use cases in which a NoSQL database may prove to be advantageous. NoSQL will not replace RDBMS.

  • If you are going to deal with a lot of data , Big Data is a term that is thrown around quite often, then you may need NoSQL. It basically means handling data which the traditional methods couldn’t manage.
  • Social network sites(Facebook, Twitter), e-commerce sites(Amazon, eBay), blogs/news publishing sites (times, medium), etc are creating a huge amount of data, and all this data needs to be stored somewhere till the platform exists.
  • The data we talked about in the above points is generally semi-structured or unstructured data. If your application produces data in a format that doesn’t fit well in a table. As an example, a blog writing platform can store articles and its metadata in tables and retrieve one blog with a complex/nested/join query. On the other hand, store one entire blog as a single document.
  • All these platforms are not only needed to store data but also need to have high read/write performance on the same. Consider a chat application, which has thousands of messages being sent and received every second. The amount of data generated is huge, not-structured and requires high performance.
  • You are working on a project with rapid time-to-market, with development practices like agile sprints. NoSQL databases are easier and faster to develop software.
  • If your application is heavily dependent on transactions, it is recommended to use an RDBMS because they are more stable, support data integrity and has ACID properties (Note: In a recent release, MongoDB now also supports ACID natively).

Where to use MongoDB?

2YrQB3u.png!web
Structure of a Modern app
  • Your frontend(red boxes) consisting of Website or Mobile app is not directly connected to MongoDB, but it connects to your server which in turn connects to the database.
  • The blue box consists of your server logic. MongoDB is independent of the implementation of the server, so you can switch Node with any other language like Python, Ruby, etc.
  • The server is responsible to check whether the user trying to edit the database has the sufficient rights, is from a trusted source, etc. So it basically maintains the confidentiality, availability, and integrity of the database.

Mongoose

It is an Object Relational Mapper(ORM)/Object Data mapper(ODM). Mongoose helps to easily use and connect to MongoDB without directly using mongo’s APIs.

It helps to define a schema and model the application data in your code itself. It also provides useful features like built-in typecasting, validation, query building, business logic hooks and more, out of the box.

Models & Schema

A model represents all of the data in a collection. When you create a model mongoose will automatically create a collection of that model in our database.

QbAn6nU.png!web
const mongoose = require('mongoose');
const Schema = mongoose.Schema;
const PokemonSchema = new Schema({
  name: String,
  type: String,
  attack: Number,
  defence: Number
});
module.exports = mongoose.model('Pokemon', PokemonSchema);

After we install mongoose as a package to our project, you need to require it in your implementation file. This is because all packages in Node.js execute within their scope.

After we create a new Schema, we need to feed this Schema to mongoose to create its model. In other words, to use the schema, we need to create an instance of the schema called model.

Node gives us a handy method module.exports to use this model by requiring it in other files.

Conclusion

Now we should be ready to begin with MongoDB NoSQL databases with a solid headstart. A few things that should be clear are:

  • NoSQL is not here to replace SQL based RDBMS.
  • NoSQL is not the opposite of SQL (since it says ‘Not only SQL’).
  • MongoDB is hot, but don’t try to fit your application to it.

If you have any questions or thoughts, please feel free to comment below!

Next steps: Making a backend API to use MongoDB with CRUD functionality. Stay tuned :)


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK