본문 바로가기

Backend/Node.js

Node.js - Working with MongDB

반응형

There are many database we can connect to Node.js server. MongDB is an easy to work no-sql database and very popular among Node.js developers. In this writing, we can will how we can use MongoDB with Node.js.

Getting Connection Code from MongoDB

Go to https://cloud.mongodb.com/ and click database. Next to the title of the database you will find 'Connect'. Click it

Choose 'Connect your application'

Copy the 'connection string'

Linking to Applications

Go the the application and open the console. Then install mongoose which will make working with mongoDB easier.

npm i mongoose

Open the server file (by conversion, server.js or app.js are used) import mongoose. And use .connect() to connect to the database with the connection string as its parameter.

// server.js

const mongoose = require('mongoose')
mongoose.connect('insertConnectionString').then(() => console.log('connected')).catch(() => console.log('failed'))

Note that you have to change the <passwor> with your actuall password.

// connection string original

mongodb+srv://test:<password>@test.fwefef.mongodb.net/

For instance, if the password is 1234, then it will like as shown below

// connection string with password

mongodb+srv://test:1234@test.fwefef.mongodb.net/

We can set the collection as well. Collections are where the data is stored and can have multiple documents which represent tables in the relational database

// connection string with password and collection name

mongodb+srv://test:1234@test.fwefef.mongodb.net/<collectionName>

Cleaning Up Files (Optional)

When all the codes are written in one file, it makes hard to read so it is common to have separate files for each purpose. To separate the DB configuration, create a folder called config and inside the folder add a file with connection settings

/* config/mongo-db.js */

const mongoose = require("mongoose");

exports.mongoDB = () => {
  mongoose
  .connect("mongodb+srv://test:1234@test.fwefef.mongodb.net/")
  .then(() => console.log("connected"))
  .catch(() => console.log("mongodb connection failed"));  
}

Then in the server file, import the configuration and run it

/* server.js */

const express = require("express");
const app = express();
const { mongoDB } = require("./config/mongo-db");

// mongoDB
mongoDB();

※ Another syntax we can use

/* config/mongo-db.js */

const mongoose = require('mongoose')
mongoose.connect(
  process.env.MONG).then(() => console.log('connected'))
  .catch(() => console.log('failed'))
/* server.js */

require('./config/db')

Using ENV (Optional)

 

Environment Variables

Environment variables are dynamic variables that help us to set configuration variables such as URLs for both the development and global environment at once, it also makes it hard for others to see the value inside (However it will be included in the build

jin-co.tistory.com

In this writing, we have seen how we can use MongDB with Node.js

 

728x90
반응형

'Backend > Node.js' 카테고리의 다른 글

NPM  (1) 2023.12.12
Creating a Server with Node.js  (1) 2023.08.17
File Upload  (0) 2023.05.22