saarahasad / Resources-for-MEVN-Application

Design and development resources to build an end to end application using MongoDB, Express, Vue.js, and Node.js

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Resources-for-MEVN-Application

Design and development resources to build an end to end application using MongoDB, Express, Vue.js, and Node.js

Basic Information on MongoDB

It is an open-source document database and leading NoSQL database. MongoDB works on the concept of collection and document.

These are some important features of MongoDB:

  • Support ad hoc queries: In MongoDB, you can search by field, range query and it also supports regular expression searches.
  • Indexing: You can index any field in a document.
  • Replication: MongoDB supports Master Slave replication. A master can perform Reads and Writes and a Slave copies data from the master and can only be used for reads or back up (not writes)
  • Duplication of data: MongoDB can run over multiple servers. The data is duplicated to keep the system up and also keep its running condition in case of hardware failure.
  • Load balancing: It has an automatic load balancing configuration because of data placed in shards.
  • Supports map reduce and aggregation tools.
  • Uses JavaScript instead of Procedures.
  • It is a schema-less database written in C++.
  • Provides high performance.
  • Stores files of any size easily without complicating your stack.
  • Easy to administer in the case of failures.
  • It also supports:
  • JSON data model with dynamic schemas
  • Auto-sharding for horizontal scalability
  • Built in replication for high availability
Database:

Database is a physical container for collections. Each database gets its own set of files on the file system. A single MongoDB server typically has multiple databases.

Collection:

Collection is a group of MongoDB documents. It is the equivalent of an RDBMS table. A collection exists within a single database. Collections do not enforce a schema. Documents within a collection can have different fields. Typically, all documents in a collection are of similar or related purpose.

Document:

A document is a set of key-value pairs. Documents have a dynamic schema. Dynamic schema means that documents in the same collection do not need to have the same set of fields or structure, and common fields in a collection's documents may hold different types of data.

Design Resources

Storing images

There are three ways to achieve this

  1. Save the Field directly in MongoDB (document)
  2. Save the Field directly in MongoDB (GridFS)
  3. Upload to AmazonS3 and save the ID directly in MongoDB (document)
  1. Only in a few cases where there is not too much to store. (not recommended)

  2. It's ideal for large images and where you need to add some "logic". Reference GridFS: https://docs.mongodb.com/manual/core/gridfs/

  3. Very scalable solution that offers you some advantage (S3 is less expensive than GridFS in terms of cost because GridFS charges on "database space" where Amazon-S3 is charged to you as "just object-storage".S3 came with a NPM module that is easy to use and manage.

MongoDB GridFS vs Amazon S3 Storage

The main points to compare are:

Storage Size :-

As you already might have guessed it , in Mongo db GridFs Any file would take a larger amount of size to be stored into the database . So what is the reason for the same ? Well it is because of how Mongodb GridFs works . It basically divides a file into smaller chunks with a default size of 256KB and stores it into the fs.chunks and its metafile data into fs.files collection . So each chunk would need some extra memory to store metadata along with the whole file metadata with various chunks information in fs.files. On the other hand Amazon S3 works in a different way which brings the file size difference . As a result any file stored in MongoDb GridFs would consume more memory than that of Amazon S3.

Cost:-

If we assume that we will use amazon for both i.e hosting our MongoDb server on AWS and using AWS S3 then definitely AWS s3 is a clear win here. AWS EBS can cost as much as double as that of Amazon S3 and bandwidth would be of the same price . However if you are hosting on dedicated servers or somewhere else, it can be a game changer as in that case using Mongodb GridFs can be quite cheap both with respect to hosting and bandwidth cost . However that is mainly due to the fact of HIGH AWS costs compared to dedicated servers (as much as 1000 times if we compare bandwidth ), if you know how to set up and manage those . If you want we can do the same for you . If you are thinking I am doing self promotion !! Yesss I am ha ha . Now move to the next point.

Scalability :-

AWS s3 is a managed service which is built for scalability based on under the hood scalable infrastructure. But in case of mongodb as it would be a self hosted / managed solution then we would be responsible for scaling the same as it would be in the case of normally scaling a nosql database via load balancers , proper collections arrangement and partitioning .

Conclusion :-

AWS S3 is built for file storage and performs better in almost all aspects but if you are worried about High AWS bandwidth costs MongoDb GridFS on self managed servers would be a way to go.

References for best MongoDB practices :

Benefits of data modeling:

NoSQL schema design is a best practice to ensure that applications evolve, scale, and perform well. A good data model helps reduce development time, increase application quality, and lower execution risks across the enterprise.

Further reading: https://hackolade.com/nosqldb/mongodb-data-modeling.html

Tools to model data which are free or have a free trial period we could use:

About

Design and development resources to build an end to end application using MongoDB, Express, Vue.js, and Node.js