DEV Community 👩‍💻👨‍💻

Cover image for How To Integrate MindsDB Into Your MongoDB Database.
EphraimX
EphraimX

Posted on

How To Integrate MindsDB Into Your MongoDB Database.

If you're new to MindsDB and seeing this blog topic, you might be asking why you need to work with two distinct databases. Wouldn't this result in data duplication between the two databases? Well, no. It will not result in data duplication. MindsDB is not a traditional Relational Database Management System (RDBMS) such as MySQL, nor is it a NoSQL database such as MongoDB or Cassandra.

So, what exactly is it?

MindsDB is a service for integrating machine learning into your database. Previously, you had to work with a framework like Pytorch or Tensorflow to develop machine learning models, going through many steps in the process. MindsDB, on the other hand, allows you to train models and generate predictions using SQL, the most well-known and longest-running database language.

To learn more about MindsDB and how it works, read this article, in which I introduce the basics of MindsDB and guide you through the process of making predictions.

This goal of this article is to explain how to integrate a MongoDB database with MindsDB. To do this, you will learn how to:

  • Connect MongoDB to MindsDB Cloud with MongoDB Compass.
  • Connect To A MongoDB Database
  • Create A Predictor in MongoDB.

Connect MongoDB to MindsDB Cloud with MongoDB Compass.

You can connect MindsDB to MongoDB through MongoDB Compass or MongoDB Shell. This article will use MongoDB Compass to set up a connection, but if you're interested in using Mongo Shell, check out the official guide here.

What is MongoDB Compass?

MongoDB compass is an interactive GUI, built by the team at MongoDB to make querying, optimizing, aggregating, and analyzing data stored in MongoDB databases easier and more efficient. If you're new to MongoDB Compass you can learn more about it here and download here.

Initiating the Connection.

To connect MindsDB to MongoDB, launch MongoDB Compass and follow the steps below:

  1. To make a new connection, click the New Connection button in the upper left corner of the application window. New Connection
  2. Next, click the Advanced Connection Options dropdown. Advanced Connection Options
  3. On the General tab, use cloud.mindsdb.com as the host to connect Mongo to MindsDB Cloud. General Tab to connect to MindsDB cloud
  4. Next, on the Authentication tab, select Username/Password as the authentication method. Then input your cloud account email and password in the username and password field respectively. Authentication - Username/Password
  5. Finally, click the Connect button below to initiate a connection with the credentials provided.
  6. On successful connection, you should see all the databases currently present in your MindsDB cloud account. Here's mine 👇. 200 - Connection Successful

Connecting MongoDB Databases to MindsDB

In the previous section, you connected MongoDB to your MindsDB cloud account. This allowed you to view all your MindsDB databases in Mongo. While this is a step in the right direction, it does not allow MindsDB to use MongoDB databases to make predictions or forecasts.

To grant MindsDB this permission, add the database name and other parameters to the MindsDB databases collection. To gain a better understanding, follow the steps below:

  • Select the mindsdb database from the current information displayed on MongoDB Compass. The database includes collections of previously created predictors as well as other collections containing information about databases, predictors, and predictor versions. MindsDB Databases Collections in mindsdb MindsDB Databases Collection
  • The databases collection contains information about which databases or integrations MindsDB can connect to and use. Databases collections The integrations MindsDB has access to are the default ones, which are the lightwood, files, view, and example data integrations, as shown in the image above.

To grant permission for MindsDB to connect to an external database, follow the steps below:

  • First, open the Mongosh terminal in Mongo Compass. Mongosh Terminal
  • Next, select mindsdb as the database of choice with the use keyword.

    use mindsdb
    

    Use MindsDB

  • Next, pass the parameters to insert the external database into the databases collection with the insertOne keyword.
    The syntax for this is:

    db.databases.insertOne({
        name: "tesla", // name of database
        engine: "mongodb", // databaase engine to use,
        connection_args: {
            "port": 27017, // connection port
            "host": "mongodb://localhost:27017", // connection host
            "database": "tesla_stock_price" // connecting database           
        }
    });
    
  • If everything went well, you should see the message below, which includes the object ID of the data that was inserted:

    {
    "acknowledged" : true,
    "insertedId" : ObjectId("634d7e64daf075c8f1b4e090")
    }
    

Create A Predictor

Creating a predictor from MongoDB is similar to the process described in the previous section. To create a predictor, insert its name and associated parameters into the predictors collection of the mindsdb database.

Use the syntax below to create the predictor:

db.predictors.insert({
     name: "predictor_name",
     predict: "target_column", 
     connection: "integration_name",
     select_data_query: {
        "collection": "collection_name",
        "call": [
            "method": "find",
            "args": []
        ]
     }
});
Enter fullscreen mode Exit fullscreen mode

Here's what the above parameters mean:

  • name: A name uniquely identifying the predictor you are creating.
  • predict: The feature of the data that you want to predict, such as the class of an iris flower.
  • connection: The database or integration where the data is present. You can establish this connection through the files or db.databases.insertOne() method.
  • select_data_query: Saves the collection for training and validation with additional parameters.

Conclusion.

You learned how MindsDB and MongoDB work together in this article by understanding:

  • How to connect MongoDB to MindsDB through Mongo Compass.
  • How to insert the database parameters to give MindsDB permission to connect to it.
  • How to create a predictor by inserting the data into the predictors collection.

Check out the official MongoDB integration documentation here to learn more about how to work with MongoDB and MindsDB.

Thank you for reading this article; if you liked it, please like and share it with others. If you want to learn more about MindsDB, visit their official documentation and/or talk to the team behind it on Slack

Before you leave, you can read my previous articles on Hashnode and Dev.to, where I regularly write about machine learning and artificial intelligence.

See you in my next article. Bye.

Top comments (0)

Regex for lazy developers

regex for lazy devs

You know who you are. Sorry for the callout 😆