Migrating from MySQL to MongoDB with the help of Parse – [Part 2]
On the first part of this guide, I gave you an introduction about data types in MongoDB that can be used to keep MySQL equivalent values.
I also showed you the Classes we are going to create in order to store that data.
Today is “hands-on” day, so get your cup of coffee, fasten your seat belts and let’s get it started!
Graphically or Programatically?
Parse supports both ways of creating your Parse.Objects (a.k.a. Classes) and their properties, so you can choose whatever flavor you like best.
Creating Parse.Objects graphically have the advantage of letting you see in real-time the result of your creation, double-check for data types while you are creating them and is a very, very user-friendly way to work.
Creating a new Class graphically
Adding a property graphically
Creating them programmatically has the advantage of letting Parse lazily generate everything for you, choosing the data type it interprets as the best fit and allowing you to create Parse.Objects from your code, which can be very useful if you have to create your data structure in runtime.
let Product = Parse.Object.extend("Product"); let product = new Product(); product.set ("price", 100);
Creating the same Class programmatically
So, as you can see, both ways are very simple. Choose the way that best fits your likings and your project’s needs.
For this tutorial, I am going to create a few classes graphically and others programmatically, so you can see both ways work.
Our first Class: Size
Let’s create our first Class and populate it.
I chose the Size class to be created graphically because it is a very small class (only one property) and, as you saw in the first part of the article, in MySQL this is an ENUM type, which is not supported on MongoDB, and as it only has a few objects, it will be easy to add them manually.
We will start by clicking the “Create a new class” button on our Database Browser:
On the popup we will choose Custom, as this is will be a custom class. This means we will manually set all of its properties.
Let’s name this class Size (capital S because it is a class) and hit the “Create class” button:
Now it is time to create our properties. Just one in this case.
Hit that “Add a new column” button on the top right:
Now, on the popup choose the data type as String, as we will be storing string values such as “small”, “medium” and “large”, and give your property a good lowercase name (properties should use lowercase as standard): “size” and hit that “Add column” button.
Now, as we will only have a few values, let’s add them manually. Later on, I’ll show you how to add values programmatically to other classes.
Click that “Add a row” button:
And a new row should show up. Double click the cell on the size column and you will be able to type your size: “small”.
Hit enter or click outside of the cell to save:
In order to add the other values, click the “+” button on the left side under the objects:
Now, repeat these steps until you have all the sizes that you want in your database. In my case I created only 3 sizes, but you can add as many as you need:
Now, how about some code?
Now that you learned how to create a new class graphically, let’s start creating some code. I’ll be writing this code in NodeJS, but you can choose any programming language that you want, as long as it is supported by the Parse platform.
We also support direct REST API calls in case your programming language does not have a specific SDK, so that should be compatible with most programming languages.
If you are not familiar with NodeJS, we have a very good tutorial about how to set it up here.
Create an index.js file that we will be adding code to.
We will be coding the Breeder class and adding some objects to it, so first let’s tell Parse we want to create a class with that name by extending it. Add this to your index.js file:
const Breeder = Parse.Object.extend("Breeder");
Now let’s add a new Breeder object programmatically, but manually setting its values instead of migrating them (we will see that later on):
let breeder = new Breeder();
This is the object we will be saving, so let’s set a few properties to it:
breeder.set ("name", "Alex"); breeder.set ("age", 39); breeder.set ("address", "St. Monica Street, 1234"); breeder.set ("state", "CA"); breeder.set ("city", "Sunny Vale"); breeder.set ("phone", "555-1234"); breeder.set ("email", "[email protected]");
Note that I just tell Parse the name of the properties I want to save and its values, and Parse will lazily create the class and the properties (with the correct data types) for me when we first save it:
Let’s create another one:
let anotherBreeder = new Breeder(); anotherBreeder.set ("name", "Alysson"); anotherBreeder.set ("age", 40); anotherBreeder.set ("address", "Oak Avenue, 456"); anotherBreeder.set ("state", "CA"); anotherBreeder.set ("city", "Mountain View"); anotherBreeder.set ("phone", "555-6789"); anotherBreeder.set ("email", "[email protected]"); anotherBreeder.save();
Now it is time to…
Save and Run 🤩
Save that index.js file and run it by typing this in your console:
You should see the new records in your App’s Dashboard:
Now, ain’t that easy peasy?
Now that we saw how to create our Classes and Properties both graphically and programmatically, we are ready to move on.
On the next post, I will show you how to read the data from MySQL and write it to MongoDB using the fundaments that we learned today.
Spoiler alert! It will get VERY technical. And you will love it!