Using MongoDB in node.js
Ref
Concepts
What is MongoDB
- A leading document, NoSQL database.
- Highly available, scalable.
- Flexible schema.
Document and scale-out
- Instead of storing tablesof rows or columns like RDBMS, record in MongoDB is document based in BSON, which is a binary representation for JSON.
- Document format makes data:
- highly flexible
- allowing varaitions in structure
- allowing partial complete data
- can embed one document in another
- Based on scale-out architecture, a structure which enables many small machines to work together to create fast systems and handle huge amounts of data.
- With it, MongoDB can scale.
BSON Example:
1 | { |
Glossary
- Database: Container for collections.
- Colletion: A group of documents. No schema enforced. But documents should be grouped for similar purpose.
- Document: A set of key-value pairs. Dynamic schema.
- Embedded Documents: Embedded documents are like join in SQL.
When to use
- Big Data
- Content Management and Delivery
- Mobile and Social Infrastructure
- Data Hub
Hands-on
Installation and running
- Install MongoDB Community Server.
- Go to
<install_folder>/bin
, runmongod
.- You got:
NonExistentPath: Data directory: D:\\data\\db\\ not found
. Need to override this pre-configuration first.
- You got:
mongod
can run with command line parameters or read from a config file. Let’s just use command line first.- Run
mongod --dbpath D:/mongodata/data --logpath D:/mongodata/log/mongod.log --port 27017 --bind_ip 0.0.0.0
. 27017 is default port. Bind ip to0.0.0.0
to avoid default127.0.0.1
binding. - Now the server is on.
MongoDB shell
- For servers >= 6.0, shell not longer shipped along with.
- Download mongosh
MongoDB Compass
- The official GUI tool for MongoDB.
Using MongoDB in node.js
https://blog-cdt1.vercel.app/2022/11/01/Using-MongoDB-in-node-js/