<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: yagueto</title>
    <description>The latest articles on DEV Community by yagueto (@yagueto).</description>
    <link>https://dev.to/yagueto</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F952389%2Fa9945417-24bf-4e3e-b98f-129ae3b95382.png</url>
      <title>DEV Community: yagueto</title>
      <link>https://dev.to/yagueto</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/yagueto"/>
    <language>en</language>
    <item>
      <title>Predicting gold prices with MindsDB and MongoDB</title>
      <dc:creator>yagueto</dc:creator>
      <pubDate>Wed, 26 Oct 2022 20:15:11 +0000</pubDate>
      <link>https://dev.to/yagueto/predicting-gold-prices-with-mindsdb-and-mongodb-4k22</link>
      <guid>https://dev.to/yagueto/predicting-gold-prices-with-mindsdb-and-mongodb-4k22</guid>
      <description>&lt;h2&gt;
  
  
  Introduction
&lt;/h2&gt;

&lt;p&gt;This tutorial will show how to use MindsDB to predict gold prices based on historical data stored in a MongoDB database.&lt;/p&gt;

&lt;h3&gt;
  
  
  What is MindsDB?
&lt;/h3&gt;

&lt;p&gt;MindsDB is an Open Source technology which adds machine learning capabilities to databases, enabling making predictions over standard databases through standard queries.&lt;/p&gt;

&lt;h3&gt;
  
  
  What is MongoDB?
&lt;/h3&gt;

&lt;p&gt;MongoDB is a popular non-relational document database that provides support for JSON-like storage.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F5olpkcjqbjq52od29158.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F5olpkcjqbjq52od29158.png" alt="Image showing how MindsDB works. On the left side, an application which queries a traditional database. On the right, an application which queries MindsDB, which consults a traditional database."&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;By using these two, we can predict data values in large databases. In this tutorial, we will predict the gold price over time using &lt;a href="https://www.kaggle.com/datasets/arashnic/learn-time-series-forecasting-from-gold-price" rel="noopener noreferrer"&gt;this Kaggle dataset&lt;/a&gt;.&lt;/p&gt;




&lt;h2&gt;
  
  
  Setting everything up
&lt;/h2&gt;

&lt;p&gt;MindsDB is available both using their cloud service &lt;a href="//cloud.mindsdb.com"&gt;(cloud.mindsdb.com)&lt;/a&gt; or locally, and so is MongoDB, available locally and in their hosted version - &lt;a href="//atlas.mongodb.com"&gt;MongoDB Atlas&lt;/a&gt;. &lt;/p&gt;

&lt;p&gt;In this tutorial, I will focus in running both MongoDB and MindsDB in local docker containers, although it is quite similar to how the cloud version works - you can check out &lt;a href="https://docs.mindsdb.com/connect/mongo-shell/" rel="noopener noreferrer"&gt;the documentation&lt;/a&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  Requirements
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;docker environment. For an introduction on what is Docker and how to use it, checkout &lt;a href="https://docs.docker.com/get-started/" rel="noopener noreferrer"&gt;this tutorial&lt;/a&gt;
&lt;/li&gt;
&lt;li&gt;docker-compose, to create and run the multi-container docker application&lt;/li&gt;
&lt;li&gt;
&lt;a href="https://www.mongodb.com/docs/mongodb-shell/install/#std-label-mdb-shell-install" rel="noopener noreferrer"&gt;mongosh client&lt;/a&gt;, to run queries against the MongoDB instance.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Creating the docker containers
&lt;/h2&gt;

&lt;p&gt;First, we will create the docker containers. Create a folder and create a file named &lt;code&gt;docker-compose.yml&lt;/code&gt;:&lt;br&gt;
&lt;/p&gt;
&lt;div class="ltag_gist-liquid-tag"&gt;
  
&lt;/div&gt;


&lt;p&gt;This will create two containers: mongodb and mindsdb. The first one uses default authentication and everybody will be able to access it. While this should &lt;strong&gt;never&lt;/strong&gt; be publicly available, this is just a first-time approach and will do for the time being. We also use a network bridge between both containers to allow connections between them.&lt;/p&gt;

&lt;p&gt;For making data persistent across restarts, create a folder inside the first one called &lt;code&gt;database&lt;/code&gt;, which will be mounted by both containers and where data will be saved.&lt;/p&gt;

&lt;p&gt;By now we should have the following directory structure:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;.
└── gold-price-estimation/
    ├── docker-compose.yml
    └── database/
        └── ...
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;To start the containers, open a terminal, go inside the &lt;code&gt;gold-price-estimation&lt;/code&gt; folder and run &lt;code&gt;docker-compose up -d&lt;/code&gt; (you might need to have superuser privileges, depending on your configuration). You now have MindsDB and MongoDB running in your system!&lt;/p&gt;

&lt;p&gt;Check this out by going to &lt;a href="//127.0.0.1:47334"&gt;127.0.0.1:47334&lt;/a&gt;. You should find MindDB's web UI. &lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fvq4u7s8zabrn9c5zmrig.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fvq4u7s8zabrn9c5zmrig.png" alt="Screenshot of MindsDB's web UI, featuring a large text box for running SQL queries"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;If something didn't work out, double-check the previous steps and if it persists feel free to leave a comment below.&lt;/p&gt;

&lt;h2&gt;
  
  
  Importing the data in the database
&lt;/h2&gt;

&lt;p&gt;After downloading &lt;a href="https://www.kaggle.com/datasets/arashnic/learn-time-series-forecasting-from-gold-price" rel="noopener noreferrer"&gt;the .csv file from Kaggle&lt;/a&gt;, we need to copy the file from the host to the container by running &lt;code&gt;docker cp file_name.csv mongodb:/file_name.csv&lt;/code&gt;. &lt;/p&gt;

&lt;p&gt;To import the database, we need to first access the docker container with &lt;code&gt;docker exec -it mongodb bash&lt;/code&gt;, after which we will be presented with a bash command line. Then, run:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;mongoimport --type csv -d golddb -c price --headerline --drop gold.csv
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Here, &lt;code&gt;golddb&lt;/code&gt; is the name of the database and &lt;code&gt;price&lt;/code&gt; is the table name.&lt;/p&gt;

&lt;p&gt;Leave the container with &lt;code&gt;exit&lt;/code&gt; and check whether the data is correctly imported:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;mongosh --host mongodb --port 27017
USE golddb;
db.price.find({}); 
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This should output the first entries in the database.&lt;/p&gt;

&lt;h2&gt;
  
  
  Create a connection from MindsDB to MongoDB
&lt;/h2&gt;

&lt;p&gt;We need to specify MindsDB where our database is. For this, run:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;mongosh --host mindsdb --port 47336
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In the client, execute:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;USE mindsdb;
db.databases.insertOne({ name: "mongo_gold", engine: "mongodb", connection_args: { "port": 27017, "host": "mongodb", "database": "golddb" } })
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;With this, we first connect our client to MindsDB’s database. Inside it, we select the database &lt;code&gt;mindsdb&lt;/code&gt; and insert an entry with the parameters of the connection:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;mongo_gold&lt;/code&gt;: name by which mindsdb identifies the connection&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;mongodb&lt;/code&gt;: URL/IP of the host, in this case the name of our docker container.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;This command should return&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;{
  acknowledged: true,
  insertedId: ObjectId("635433076e11662dcd20042c")
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;, which tells us that it has saved the configuration. Runnning&lt;br&gt;
&lt;code&gt;db.databases.find()&lt;/code&gt; confirms it by returning&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;{
    name: 'mongo_gold',
    database_type: 'mongodb',
    host: 'mongodb',
    port: '27017',
    user: null
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;All that remains is to create the predictor!&lt;/p&gt;

&lt;h2&gt;
  
  
  Creating the predictor
&lt;/h2&gt;

&lt;p&gt;While still inside the client, we run:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;db.predictors.insert({ name: "mongo_gold_p", predict: "Value", connection: "mongo_gold", "select_data_query": "db.gold.find()" });
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;What do those parameters mean?&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;name&lt;/code&gt;: name by which mindsdb identifies the predictor&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;predict&lt;/code&gt;: name of the column in the database which values we want to predict&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;connection&lt;/code&gt;: name we created previously by which Mindsdb identifies the connection&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;select_data_query&lt;/code&gt;: this allows to specify specific rows in the database by using standard MongoDB queries. For this example, we will use all rows.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;We should receive an acknowledged message, and then MindsDB will start working on creating the predictor. You can check the status of the model generation with:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;db.predictors.find({name: "model_name"});
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Test it out!
&lt;/h2&gt;

&lt;p&gt;Once the status is &lt;code&gt;complete&lt;/code&gt;, it will be ready to answer to queries. &lt;/p&gt;

&lt;p&gt;Go ahead and predict a value with:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;USE mongo_gold; 
db.mongo_gold_p.find({Date: "1975-07-01"})
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;If it returns the prediction (along with some expressions like confidence, among others - full description &lt;a href="https://docs.mindsdb.com/mongo/find/" rel="noopener noreferrer"&gt;in the documentation&lt;/a&gt;), congratulations! &lt;/p&gt;

&lt;h2&gt;
  
  
  Conclusion
&lt;/h2&gt;

&lt;p&gt;You have successfully created a MongoDB database which, extended thanks to the power of MindsDB, is able to generate predictions based on the data provided. &lt;/p&gt;

&lt;p&gt;For more info, &lt;a href="https://docs.mindsdb.com/mongo/collection-structure/" rel="noopener noreferrer"&gt;make sure to check out the docs&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Thank you for reading my tutorial, if it has been useful for you remember to give it a like and share it with your friends! Also, check out my &lt;a href="https://github.com/yagueto" rel="noopener noreferrer"&gt;Github profile&lt;/a&gt;.&lt;/p&gt;

</description>
      <category>mongodb</category>
      <category>mindsdb</category>
      <category>ai</category>
      <category>tutorial</category>
    </item>
  </channel>
</rss>
