Skip to main content

Command Palette

Search for a command to run...

[TIL] Converting MySQL -> MongoDB (NoSQL)

05/18/23

Published
[TIL] Converting MySQL -> MongoDB (NoSQL)

๐Ÿ“Œ Issues encountered

Issue #1

When querying the entire chat history from MySQL, the query took too long. When testing with Postman, it took around 2.xx seconds.

Issue #2

Adding MongoDB connection.

๐Ÿ“Œ What I tried

Issue #1

  • There were two tables to join with the Chats table in MySQL, and the part where the entire chat history was queried required a lot of query executions and processing to manipulate the returned objects in the service layer.

  • Therefore, I decided to change the Chats table in MySQL to MongoDB, and created two schemas: Chats schema and Messages schema.

  • Chat-related data is stored in MongoDB, while the rest is stored in MySQL, so I added MySQL data to the Chats schema to minimize the parts that fetch data from MySQL while performing CRUD operations on Chat.

  • The Chats schema includes fields such as title, is_sold, product_id, members, members_nickname, and address.

  • After actually changing the Chat part to MongoDB, the query time decreased from 2.xx seconds to around 600 ms.

Issue #2

  1. In utils/mongodb.js, I defined the part that connects to MongoDB as follows:
const mongoose = require("mongoose");
const { mongodb } = require("../config/config.js");

const db_url = `mongodb+srv://${mongodb.id}:${mongodb.password}@${mongodb.cluster}/${mongodb.db}?retryWrites=true&w=majority`;

const connectToMongoDB = async (app) => {
  mongoose
    .connect(db_url)
    .then(() => {
      console.log("MongoDB connected")
    })
    .catch((err) => {
      throw err;
    });
};

module.exports = connectToMongoDB;
  1. In app.js, I imported MongoDB as follows:
const connectToMongoDB = require("./utils/mongodb");
connectToMongoDB(app);
  1. In the schema folder, I defined the Chats and Messages schemas and imported them in the respective repositories where these schemas are used.

More from this blog

[์ฝ”ํ…Œ] ๊ทธ๋ฆฌ๋”” ๋ฌธ์ œ - ๋ฌด์ง€์˜ ๋จน๋ฐฉ ๋ผ์ด๋ธŒ

https://school.programmers.co.kr/learn/courses/30/lessons/42891 ํšจ์œจ์„ฑ ํ…Œ์ŠคํŠธ์— ์‹ ๊ฒฝ์จ์•ผ ํ•˜๋Š” ๋ฌธ์ œ ์šฐ์„ ์ˆœ์œ„ ํ๋ฅผ ํ™œ์šฉํ•ด์„œ ๋จน๋Š” ์‹œ๊ฐ„์ด ์งง์€ ์Œ์‹๋ถ€ํ„ฐ ํ์—์„œ ๋นผ๊ธฐ import heapq # ์šฐ์„ ์ˆœ์œ„ํ ํ™œ์šฉ: food_time์ด ์งง์€ ์Œ์‹๋ถ€ํ„ฐ ์‚ญ์ œ def solution(food_times, k): if sum(food_times) <= k: return -1 ...

Apr 4, 2024
[์ฝ”ํ…Œ] ๊ทธ๋ฆฌ๋”” ๋ฌธ์ œ - ๋ฌด์ง€์˜ ๋จน๋ฐฉ ๋ผ์ด๋ธŒ

[์ฝ”ํ…Œ] ์—ฌํ–‰๊ฒฝ๋กœ

๐Ÿ’ก [์ถœ๋ฐœ์ง€, ๋„์ฐฉ์ง€] ํ˜•ํƒœ๋กœ ์ฃผ์–ด์ง„ ๋น„ํ–‰๊ธฐ ํ‹ฐ์ผ“์„ ํ†ตํ•ด ๋ชจ๋“  ํ‹ฐ์ผ“์„ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋Š” ๊ฒฝ์šฐ์˜ ๊ณตํ•ญ์„ ๋ฐฉ๋ฌธ ์ˆœ์„œ ๊ตฌํ•˜๊ธฐ (๋‹จ, ์—ฌ๋Ÿฌ ๊ณตํ•ญ์„ ๋ฐฉ๋ฌธํ•  ์ˆ˜ ์žˆ๋Š” ๊ฒฝ์šฐ ์•ŒํŒŒ๋ฒณ์ด ๋น ๋ฅธ ๊ณตํ•ญ๋ถ€ํ„ฐ ๋ฐฉ๋ฌธํ•œ๋‹ค.) ํ‹€๋ ธ๋˜ ์ฝ”๋“œ from collections import defaultdict def dfs(graph, route, depart): if graph[depart]: connected = graph[depart][0] ...

Feb 26, 2024
[์ฝ”ํ…Œ] ์—ฌํ–‰๊ฒฝ๋กœ

siwon.log

161 posts