Backend basics
Tags: mongodb, beginners
Published: 2021-12-23 15:36:06

CRUD operations: CRUD operations are to create, read, update, and delete documents. 
Create or insert operations add new documents to a collection. If the collection does not currently exist, insert operations will create the collection. Create or insert operations uses the POST method.  
The read function is similar to a search function. It allows users to search and retrieve specific records in the table and read their values. Users may be able to find desired records using keywords, or by filtering the data based on customized criteria. Read function uses the GET method.
The update function is used to modify existing records that exist in the database. To fully change a record, users may have to modify information in multiple fields. Update function uses the PUT method.
The delete function allows users to remove records from a database that is no longer needed. Delete function uses the DELETE method.

JWT: JWT full form is JSON Web Token. It is an industry standard RFC 7519 method which secures two parties. It guarantees data ownership but not encryption. 
JWT is a useful technology for API authentication and server-to-server authorization. JWT claims to be encoded as a JSON object that is digitally signed using JSON Web Signature (JWS) and/or encrypted using JSON Web Encryption (JWE). JWT can be used between two parties to exchange information. JWT is digitally-signed and can be used in a secure public/private key pair. Information is verified using the public key on the other end. JWT can contain user information in the payload and can be used in the session to authenticate the user. Once authenticated, users can access protected resources in an application using the JWT included in the request. So, every request will be authenticated by verifying the JWT.

Mongoose: Mongoose is an Object Data Modeling (ODM) library for MongoDB and Node.js. It manages relationships between data, provides schema validation, and is used to translate between objects in code and the representation of those objects in MongoDB. 
MongoDB is a schema-less NoSQL document database. It can store JSON documents in it, and the structure of these documents can vary as it is not enforced like SQL databases. This is one of the advantages of using NoSQL as it speeds up application development and reduces the complexity of deployments. A huge benefit of using a NoSQL database like MongoDB is that it is constrained to a rigid data model. Add or remove fields, nest data multiple layers deep, and have a truly flexible data model that meets our needs today and can adapt to our ever-changing needs. But being too flexible can also be a challenge. If there is no consensus on what the data model should look like, and every document in a collection contains vastly different fields, then it will have a bad time.
A schema defines document properties through an object where the key name corresponds to the property name in the collection. While Mongo is schema-less, SQL defines a schema via the table definition. A Mongoose schema is a document data structure that is enforced via the application layer. A schema definition should be simple, but its complexity is usually based on application requirements. Schemas can be reused and they can contain several child-schemas too. In the example above, the value of the email property is a simple value type. However, it can also be an object type with additional properties on it.
MongoDB Schema Validation makes it possible to easily enforce a schema against your MongoDB database, while maintaining a high degree of flexibility, giving you the best of both worlds. In the past, the only way to enforce a schema against a MongoDB collection was to do it at the application level using an ODM like Mongoose, but that posed significant challenges for developers.
Models are higher-order constructors that take a schema and create an instance of a document equivalent to records in a relational database.
A Mongoose model is a wrapper on the Mongoose schema. A Mongoose schema defines the structure of the document, default values, validators, etc., whereas a Mongoose model provides an interface to the database for creating, querying, updating, deleting records, etc.
Mongoose has a flexible API and provides many ways to accomplish a task. Most of the operations can be done in more than one way either syntactically or via the application architecture.