Emiko.dev β˜€οΈ
Emiko.dev β˜€οΈ

@emiko_dev

12 Tweets 1 reads Nov 21, 2022
If you think MongoDB is comparable with SQL, you don't know database. πŸ‘€
Here's what all the basics you need to know about MongoDB:
MongoDB is a DBMS (database management system). πŸ’š
A DMBS is often used for carefully organized data, for retrieval by the web server. (e.g. web user, shop inventory, message board).
There are two mainly types of DMBS: relational database and non-relational database. πŸ’½
Relational database means tables of rows and columns.
Non-relational (NoSQL) database means flexible documents, without a fixed table structure. πŸ•ŠοΈ
MDB is an open-source NoSQL database management system (DBMS).
It is:
- Non-relational (lack of support of the concept of relations)
- Not easy to implement joins in query
- Supporting dynamic schema
MongoDB stores data records as 'documents' πŸ“
What is a 'document' in MongoDB? πŸ€”
Document is similar to JS object, or a row in a relational DB.
A MongoDB document is a JSON-style data structure composed of field-and-value pairs.
They are stored as 'BSON' (Binary JSON): a binary-encoded serialization of JSON documents. πŸ€“
What is 'BSON'? πŸ€”
BSON is a binary-encoded serialization of JSON documents.
The value of a field can be any of the BSON types (String, Double,
32/64-bit integer, etc.), including other documents, arrays, and arrays of documents! ✨
See more detail on: mongodb.com
How are document stored? πŸ“₯
By default, MDB automatically assigns a unique value of type 'ObjectId' in a compulsory 'field _id'.
'_id' serves as the primary key, it:
- is always the first field in the document
- value must be unique
- can be a value of any type except array ✨
Using MongoDB with Node.js ✨
In a web application, the backend (e.g. Node.js) is responsible to access the database server.
Two possible ways to access MongoDB from Node.js:
- Use MDB directly with Mongo Shell commands in
Node.js
- Use Mongoose πŸ¦†
What is Mongoose? πŸ€”
It's an Object Data Modeling (ODM) library on top of MongoDB.
It supports schemas to describe documents:
- type check and conversion
- check if a value is unique
- check if a required value is omitted
It simplifies interaction between MDB and Node.js ✨
How to CRUD in MongoDB?
CRUD are the common operations for data items:
✍️ Create
πŸ‘€ Read / Retrieve
πŸ†• Update
🚯 Delete
CRUD operations are supported by Mongoose query functions.
Queries are executed asynchronously, and results are passed to callbacks πŸ€™
Note: This thread is inspired by my previous controversial tweet - feel free to check the comments for explanations of why they're incomparable! πŸ˜‚
That's a wrap!
If you enjoyed this thread:
1. Follow me @emiko_dev for more of these
2. RT the tweet below to share this thread with your audience

Loading suggestions...