mysticBel / Building-a-Node-JS-API

👁👅👁✨👉 Building API using Node JS : In this project I'm learning some JavaScript, Node JS event loop, creating server, connect to database and sending json responses. Project for practising purposes 🤗
0 stars 0 forks source link

Connecting project to MongoDB 💚✨ #3

Closed mysticBel closed 2 years ago

mysticBel commented 2 years ago
mysticBel commented 2 years ago

How to connect to MongoDB ?

  1. Go to https://www.mongodb.com/es/cloud/atlas then create an account.
  2. Build my first cluster --> Give a name for your cluster and hit the button that says Create Cluster 😜
  3. Config Network Access to any IP using 0.0.0.0/0 as whitelist entry. This will allow to use our mongo atlas cluster from any IP address. Once done, click Confirm.
  4. Click on Database Access --> Create Database User with Name and Password 👀REMEMber : We will need this info to put into our project later
  5. Get Connection URI String --> Click on Clusters , click on Connect your application , 👀 copy the Connection String.
  6. Go to your project , then connect your app with database with the following code in:
// import mongoose 💚
const mongoose = require('mongoose');

//db connection
mongoose.connect(
  ' _paste here the URI, it is a string so use single quotes for it, also put your password!!!_ ',
  {useNewUrlParser: true}
)
.then(() => console.log('DB Connected :D  yay! '))

mongoose.connection.on('error', err => {
  console.log(`DB connection error: ${err.message}`)
});

That's all. Now you can use this connection string to use mongodb in the cloud (mongoDB Atlas) 🦄✨

mysticBel commented 2 years ago

👀 wow 👍 https://university.mongodb.com/courses/M001/about

mysticBel commented 2 years ago

MongoDB

https://www.freecodecamp.org/news/introduction-to-mongoose-for-mongodb-d2a7aa593c57/

MongoDB is a schema-less NoSQL document database. It means you can store JSON documents in it, and thestructure 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.

Below is an example of how data is stored in Mongo vs. SQL Database:

image

image

Terminologies

Collections

‘Collections’ in Mongo are equivalent to tables in relational databases. They can hold multiple JSON documents.

Documents

‘Documents’ are equivalent to records or rows of data in SQL. While a SQL row can reference data in other tables, Mongo documents usually combine that in a document.

Fields

‘Fields’ or attributes are similar to columns in a SQL table.

Schema

While Mongo is schema-less, SQL defines a schema via the table definition. A Mongoose ‘schema’ is a document data structure (or shape of the document) that is enforced via the application layer.

Models

‘Models’ are higher-order constructors that take a schema and create an instance of a document equivalent to records in a relational database.

mysticBel commented 2 years ago

Using Mongoose

Mongoose is an Object Data Modeling (ODM) library for MongoDB and Node.js. It's an object modeling tool designed to work in an asynchronous environment. Mongoose supports both promises and callbacks. 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.


// Using Node.js `require()`
const mongoose = require('mongoose');

// Using ES6 imports
import mongoose from 'mongoose';

image