PROJECTENGRAM

MONGODB

MongoDB

Tags: #◇Web Development #◇CCAPDEV #◇MongoDB #◇Code Snippets #◇M5


◇Overview

Extracted notes for MongoDB covering both theoretical and practical elements.

◇Key Definitions

Model-View-Controller https://developer.mozilla.org/en-US/docs/Glossary/MVC
Model-View-Controller https://medium.com/@joespinelli_6190/mvc-model-view-controller-ef878e2fd6f5
What is MongoDB? “MongoDB is a general purpose, document-based, distributed database built for modern application developers and for the cloud era” MongoDB is a document database wherein one collection (equiv. to a table in RDBMS) holds different documents (equiv. to a tuple in RDBMS). Number of fields, content, and size of the document can differ from one document to another
What is MongoDB? A record in MongoDB is referred to as a document, which is a data structure compose of field and value pairs. Just like JSON objects The values of fields may include other documents, arrays, and arrays of documents
Installing MongoDB (Locally) Search for MongoDB in the npm registry and follow the installation instructions Navigate to your project directory Run npm install mongodb This will install the MongoDB driver for a node.js project Install the MongoDB service (mongod) into your machine. https://www.mongodb.com/docs/manual/administration/install-community/

◇Code Implementation

『』
TEXT
Side: CommonJS/ES6 Modules
There are two ways to import modules:
CommonJS
ES6
CommonJS is the NodeJS default, and follows the syntax:
For imports: const <name> = require(<module name/path to module>);
For exports: module.exports = { <objects/functions to be exported> }
『』
TEXT
Side: CommonJS/ES6 Modules
ES6 follows the syntax:
For imports:import <name> from <module_name/path>;orimport {<obj/func to import>, <other obj/funcs>} from <module_name/path>; 
For exports:export const obj = ...orexport function myFunc() { ... }or export {<prevDefinedConst>, <prevDefinedFunc>};
『』
TEXT
Creating a MongoDB Connection
Inside conn.js:
To create an instance, use the constructornew MongoClient(<url>)
url – the socket where we will connect to. The port can be set to anything as long as it is not occupied by another service.
『』
TEXT
Creating a MongoDB Connection
Inside conn.js:
Two functions are exported:
connectToMongo (callback)
getDb (dbName)
connectToMongo(callback)
Establishes the initial connection to the mongodb process. The connection should be maintained until the main program (e.g., app.js/server.js) is closed
Has to be called before any other operations on the database.
getDb(dbName)
Returns an instance of the database with the specified name. 
『』
TEXT
Side: Environment Variables
For best practices: config values, credentials, and sensitive information should be stored inside a .env file.
This .env file will then be loaded as environment variables which can then be accessed in NodeJS via:
process.env.<var_name>
In version control (e.g., git), ignore the .env file from your repository.
This is to prevent your credentials from being leaked to the internet!

◇Syntax Breakdown

Ensure correct syntax for MongoDB Pay attention to specific functions and declarations.

◇Complexity Analysis

Time Complexity: O(1) for typical operations unless looping. Space Complexity: O(1) generally.

◇References & Navigation

CONFIRMATION // 理解度の確認

ACTIVE RECALL VERIFICATION

Select an answer to verify conceptual comprehension and reveal the technical rationale.

Q1

In MongoDB, a "document" is functionally equivalent to which concept in a traditional Relational Database Management System (RDBMS)?

Q2

Which of the following best describes the CommonJS syntax used by default in Node.js for importing modules?

Q3

Why is it considered a best practice to use .env files for configuration values and credentials?

MongoDB | PROJECT ENGRAM