Get Free Trial
  1. API reference
Connecting to Data Source
  1. Supported data sources
  2. Connecting to other data sources
Configuring the component
  • Documentation for older versions
  • Table of contents

    Getting started with the MongoDB Connector

    To quickly and easily connect to a MongoDB database, Flexmonster recommends using our Flexmonster MongoDB Connector, which is based on our custom data source API.

    The Connector needs to be embedded into a middle-layer server that accepts requests from Flexmonster and passes them to the Connector. The Connector then fetches the required data and sends it back to the server, then finally the data reaches Flexmonster and gets displayed in your browser.

    To demonstrate how the Connector, the server, and Flexmonster cooperate, we have prepared a ready-to-use sample project. Using this project as a starting point, you can also connect to your MongoDB database.

    This guide describes how to:

    Prerequisites

    To run this simple application you will need Node.js and npm. Get it here if it’s not already installed on your machine.

    Run a sample GitHub project

    To run our sample project from GitHub, follow these steps:

    Step 1. Download the .zip archive with the sample or clone it from GitHub with the following command:

    git clone https://github.com/flexmonster/pivot-mongo && cd pivot-mongo

    Step 2. Install the npm packages described in package.json:

    npm install

    Step 3. Run the server with the following commands:

    npm run build
    npm run start

    Now that the server is running and ready to accept Flexmonster’s requests, you can open client/index.html in the browser to see the Connector’s output.

    Note In this tutorial, you connected to Flexmonster’s sample database. To connect to your own database, follow the steps from the section below.

    Connect to your MongoDB database

    You can use the sample project as a starting point to connect to your MongoDB database. To accomplish this, complete the following steps:

    Step 1. Open src/controller/mongo.ts and replace the existing connection string with yours:

    MongoClient.connect("your connection string", {
    ...
    });

    Step 2. Then replace the database name with yours:

    dbo = db.db("your database name");

    Step 3. optional If needed, you can configure caching and logging for the MongoDB Connector. Learn more about the Connector’s configurations.

    Step 4. In client/index.html, replace the index value with your collection’s name:

    report: {
        dataSource: {
            type: "api",
            url: "http://localhost:9204/mongo",
            index: "your-collection-name"
        }
    }

    Step 5. Run the server with the following commands:

    npm run build
    npm run start

    Now the data from your MongoDB database is displayed when you open the client/index.html file in a browser.

    You can take our sample project as a ready template and make it work for your application by adding your logic, improving, and expanding the project.

    To learn how to embed the Connector into your server, have a look at this tutorial:

    What’s next?