Converting MongoDB Queries to MySQL in Node: A Step-by-Step Guide

I am currently facing a challenge where I need to migrate my app's database from mongoDB to mysql. The application was initially developed using node.js, but I encountered an issue with the "Create table" query while attempting the conversion process.

var setting = new MongoCon.Schema({
site: {
    type: Object,
    default: {
        someData: {
            lengthMsgBc: 250,
            likeUpPic: 10,
        },
        title: "",
        walllikes: {
            likeUpPic: 10,
        },
    },
},
dro3: {
    type: Array,
    default: [
        "1604251747557.gif",
        "1604251842627.gif",
        "1604251846871.gif",
    ],
},
});

After trying to convert the code snippet above to SQL, I encountered errors in the resulting query.

mysqlCon.query('CREATE TABLE al3ochek_settings (_id INT NOT NULL AUTO_INCREMENT, site VARCHAR(255) DEFAULT "{"someData":{"lengthMsgBc":250,"likeUpPic":10},"title":"","walllikes":{"likeUpPic":10}}", dro3 VARCHAR(255) DEFAULT "["1604251747557.gif","1604251842627.gif","1604251846871.gif"]", PRIMARY KEY (_id) )', err => {
if (err) throw err;
    console.log("Table al3ochek_settings created!")
})

The problematic part of the SQL query arises when using JSON.stringify to save an object in the "site" column, resulting in an error due to a specific character combination.

"{"  "["

If you have a solution to this issue or any suggestions on how it can be resolved, your assistance would be greatly appreciated.

Answer №1

To ensure proper formatting in your query, it's advisable to use a consistent type of quotation marks. In this case, you can enclose the entire query within backticks like so:

`CREATE TABLE al3ochek_settings (_id INT NOT NULL AUTO_INCREMENT, site VARCHAR(255) DEFAULT '{"someData":{"lengthMsgBc":250,"likeUpPic":10},"title":"","walllikes":{"likeUpPic":10}}", dro3 VARCHAR(255) DEFAULT "["1604251747557.gif","1604251842627.gif","1604251846871.gif"]', PRIMARY KEY (_id) )`

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Updating materials within the Forge

Currently, we have implemented a system where the client retrieves object states when the page loads, changing the colors of 'pending' objects in the model. We continue to poll for changes to update the coloring - initially coloring pending objec ...

Error: The function is not defined on this.props during the handleCHange event

After going through numerous answers to similar questions on this topic, I believe that I am following all the necessary steps but for some reason, it is not working. Below is the specific section of code that is causing the error: import React from &apos ...

The buffering timed out while attempting to insert a record into a remote database

After successfully connecting to my remote mongo server, I encountered a puzzling issue: {"t":{"$date":"2020-12-21T15:57:30.022+00:00"},"s":"I", "c":"ACCESS", "id":20250, ...

Identifying whether a Alphabet or a Digit has been Pressed - JavaScript

I understand that it is possible to detect if a key has been pressed and identify which key was pressed using JavaScript. In order to check if a key is down or pressed, jQuery can be utilized with ease: $( "#some id" ).keydown(function() or $( "#m" ). ...

Utilize JavaScript to compute and implement a deeper shade of background color

To dynamically apply darker shades of background using JavaScript, I have devised the following code. .event-list .bg{ background:#eee; padding:5px; } .grid .event-list:first-child .bg{ background: #2aac97 } .grid .event-list:nth-child(2) .bg{ backgrou ...

Modifying the URL does not alter the selected tab

As part of my project, I wanted to ensure that when the page is refreshed, the previously selected tab remains active. To achieve this, I created a basic HTML page and added some jQuery. However, I encountered an issue when manually changing the URL from ...

Is there a way to apply -webkit-line-clamp to this JavaScript content using CSS?

i have a random-posts script for my blogger website <div class="noop-random-posts"><script type="text/javascript"> var randarray = new Array(); var l=0; var flag; var numofpost=10; function nooprandomposts(json){ var total = ...

Exploring the Differences between HTML Values in HTML and jQuery

Can someone assist me with comparing HTML values to check for equality? I am looking to compare the values of two select elements in HTML and needing guidance on how to accomplish this. Specifically, I want to determine if the selected values from both s ...

Disseminate several outcomes using a Discord bot

My first experience using stackoverflow was to seek help regarding a bot created to post results whenever a new episode of a show in the search list is added on nyaa.si. The issue I'm facing is that the bot posts the same episode multiple times within ...

How can one ensure that Discord waits for a script to complete running, and how can you prevent Discord from responding until all necessary data has been obtained?

I recently started working with node.js and asynchronous programming, and I'm facing a challenge that has me puzzled. My goal is to create a Discord bot that fetches data from a third-party website. While I can successfully retrieve the data and see i ...

Guide on how to assign a masterpage DOM element to a content page using JavaScript

Within an ASP.NET master page, there is a specific div element that I want to hide in one of its content pages. To achieve this, I included the following JavaScript code at the end of the content page: if (document.getElementById('sitemap')) { ...

Unable to access the URL slug within the client component in Next.js version 13

In my upcoming project with Next 13, I have a client-side component that is being rendered under the route /journal/[date] Unfortunately, I'm facing an issue trying to extract the date from the URL. I attempted to use: import { useRouter } from &apos ...

The template literal expression is being flagged as an "Invalid type" because it includes both string and undefined values, despite my cautious use of

I am facing an issue with a simple component that loops out buttons. During the TypeScript build, I encountered an error when calling this loop: 17:60 Error: Invalid type "string | undefined" of template literal expression. In my JSX return, I ...

Navigating shadow dom elements in HTML with Selenium WebDriver

public WebElement retrieveShadowRootElement(WebElement element) { WebElement shadowRoot = (WebElement) ((JavascriptExecutor)driver) .executeScript("return arguments[0].shadowRoot", element); return shadowRoot; } WebElement rootElement= dri ...

Reading and extracting data from a massive XML document using Node.js

Currently, I am facing a challenge with an XML file that is quite large - exceeding 70mb. My goal is to parse this data in Node.js for potential data visualizations down the line. After giving it some thought, I decided that converting the XML file to JSON ...

Ways to retrieve several parameters from a controller using Ajax Jquery in Codeigniter

I need to retrieve a list of images from a folder based on a specific ID. Currently, I am able to get the file names but I also require the upload path. Is there a way to obtain both sets of data using a single function? Javascript Code: listFilesOnServ ...

I'm looking for assistance in grasping the SQL code that utilizes JOIN and LEFT JOIN. As a beginner in SQL, any help understanding this would

In the database, there are three main tables being used: Students table, Subjects table, and Examinations table. Students table: +------------+--------------+ | student_id | student_name | +------------+--------------+ | 1 | Alice | | ...

Failed installation of Semantic-ui through npm encountered

Encountering an error while attempting to install semantic-ui through npm for a new project. Here are the version details: $ node -v v16.14.0 $ npm -v 8.10.0 $ npm i semantic-ui npm WARN deprecated <a href="/cdn-cgi/l/email-protection" class="__cf_em ...

What is the best way to connect to a single table multiple times in Laravel?

I am trying to figure out the best way to display a sorted list of users along with their top 5 traits using Laravel 8 and SQL. I have tables for users, user_traits, and traits. Here are the table structures: users: (id, name, .... ) user_traits: ...

What is the best way to add a dynamic parameter to the URL?

Here is an example of my URL: https://my-website.com/api/players?countryId=1&clubId=2&playerName=abc The parameter values can vary. This is the code snippet I use: getDataPlayer(payload) { let params if(payload.countryId && payl ...