Develop a JavaScript application with MongoDB on Azure

To create, move, or use a mongoDB database to Azure, you need a Cosmos DB resource. Learn how to create the resource and use your database.

Create and use database with VS Code extension

Create a Cosmos resource first because this will take several minutes.

  1. In Visual Studio Code, select the Azure icon in the left-most menu, then select the Databases section.

    If the Databases section isn't visible, make sure you have checked the section in the top Azure ... menu.

    Partial screenshot of Visual Studio Code's remote container icon

  2. In the Databases section of the Azure explorer, select your subscription with a right-click, then select Create Server.

  3. In the Create new Azure Database Server Command Palette, select Azure Cosmos DB for MongoDB API.

  4. Follow the prompts using the following table to understand how your values are used. The database may take up to 15 minutes to create.

    Property Value
    Enter a globally unique Account name name for the new resource. Enter a value such as cosmos-mongodb-YOUR-NAME, for your resource. Replace YOUR-NAME with your name or unique ID. This unique name is also used as part of the URL to access the resource in a browser.
    Select or create a resource group. Create a new resource group named js-demo-mongodb-web-app-resource-group-YOUR-NAME-HERE.
    Location The location of the resource. For this tutorial, select a regional location close to you.

    Creating the resource may take up to 15 minutes. You can skip to the next section if you are time-restricted but remember to come back to finish this section in a few minutes.

Add firewall rule for your client IP address

You need to use the Azure CLI or the Azure portal to configure a firewall rule.

Get the MongoDB connection string for your resource

  1. Select the Azure explorer from the Activity bar.
  2. Select the Cosmos DB from the Side bar.
  3. Right-click on the database and select Copy connection string.

Partial screenshot of VSCode displaying Azure Cosmos DB database in Azure explorer.

Use the Azure Cosmos DB emulator for local development

Learn more about the Azure Cosmos DB emulator:

Use native SDK packages to connect to MongoDB on Azure

The mongoDB database on Cosmos DB uses npm packages already available, such as:

Install mongodb SDK

To connect and use your mongoDB on Azure Cosmos DB with JavaScript and mongodb, use the following procedure.

  1. Make sure Node.js and npm are installed.

  2. Create a Node.js project in a new folder:

    mkdir DataDemo && \
        cd DataDemo && \
        npm init -y && \
        npm install mongodb &&
        code .
    

    The command:

    • Creates a project folder named DataDemo
    • Changes the Bash terminal into that folder
    • Initializes the project, which creates the package.json file
    • Installs the SDK
    • Opens the project in Visual Studio Code

Create JavaScript file to bulk insert data into MongoDB database

  1. In Visual Studio Code, create a bulk_insert.js file.

  2. Download the MOCK_DATA.csv file and place it in the same directory as bulk_insert.js.

  3. Copy the following JavaScript code into bulk_insert.js:

    const { MongoClient } = require('mongodb');
    const ObjectId = require('mongodb').ObjectID;
    require('dotenv').config();
    
    const fs = require('fs');
    const parse = require('csv-parser')
    const { finished } = require('stream/promises');
    
    const DATABASE_URL = process.env.YOUR_RESOURCE_PRIMARY_CONNECTION_STRING
        ? process.env.YOUR_RESOURCE_PRIMARY_CONNECTION_STRING
        : 'mongodb://localhost:27017';
    const DATABASE_NAME = process.env.DATABASE_NAME || 'my-tutorial-db';
    const DATABASE_COLLECTION_NAME =
        process.env.DATABASE_COLLECTION_NAME || 'my-collection';
    
    const csvFile = './books.csv'    
        
    let mongoConnection = null;
    let db = null;
    let collection = null;
    
    
    // insert each row into MongoDB
    const insertData = async (readable) =>{
        
        let i = 0;
        
        for await (const row of readable) {
            console.log(`${i++} = ${JSON.stringify(row.goodreads_book_id)}`);
            await collection.insertOne(row);
        }
    }
    const bulkInsert = async () => {
        
        mongoConnection = await MongoClient.connect(DATABASE_URL, { useUnifiedTopology: true });
        db = mongoConnection.db(DATABASE_NAME);
        collection = await db.collection(DATABASE_COLLECTION_NAME);
        
        // read file, parse CSV, each row is a chunk
        const readable = fs
        .createReadStream(csvFile)
        .pipe(parse());
    
        // Pipe rows to insert function
        await insertData(readable)
        await mongoConnection.close();
    }
    
    bulkInsert().then(() => {
        console.log('done');
    
    }).catch(err => {
        console.log(`done +  failed ${err}`)
    })
    
  4. Replace the following in the script with your resource information:

    • YOUR_RESOURCE_PRIMARY_CONNECTION_STRING
  5. Run the script.

    node bulk_insert.js
    

Create JavaScript code to use MongoDB

  1. In Visual Studio Code, create a index.js file.

  2. Copy the following JavaScript code into index.js:

    const { MongoClient } = require('mongodb');
    const ObjectId = require('mongodb').ObjectID;
    
    // read .env file
    require('dotenv').config();
    
    /* eslint no-return-await: 0 */
    
    const DATABASE_URL = process.env."YOUR_RESOURCE_PRIMARY_CONNECTION_STRING"
        ? process.env."YOUR_RESOURCE_PRIMARY_CONNECTION_STRING"
        : 'mongodb://localhost:27017';
    const DATABASE_NAME = process.env.DATABASE_NAME || 'my-tutorial-db';
    const DATABASE_COLLECTION_NAME =
        process.env.DATABASE_COLLECTION_NAME || 'my-collection';
    
    let mongoConnection = null;
    let db = null;
    
    const insertDocuments = async (
        documents = [{ a: 1 }, { a: 2 }, { a: 3 }]
    ) => {
        // check params
        if (!db || !documents)
            throw Error('insertDocuments::missing required params');
    
        // Get the collection
        const collection = await db.collection(DATABASE_COLLECTION_NAME);
    
        // Insert some documents
        return await collection.insertMany(documents);
    };
    const findDocuments = async (
        query = { a: 3 }
    ) => {
        
        // check params
        if (!db)
            throw Error('findDocuments::missing required params');
    
        // Get the collection
        const collection = await db.collection(DATABASE_COLLECTION_NAME );
    
        // find documents
        return await collection.find(query).toArray();
    };
    
    const removeDocuments = async (
        docFilter = {}
    ) => {
        
        // check params
        if (!db )
            throw Error('removeDocuments::missing required params');
    
        // Get the documents collection
        const collection = await db.collection(DATABASE_COLLECTION_NAME);
    
        // Delete document
        return await collection.deleteMany(docFilter);
    };
    
    const connect = async (url) => {
        
        // check params
        if (!url) throw Error('connect::missing required params');
    
        return MongoClient.connect(url, { useUnifiedTopology: true });
    };
    /* 
    eslint consistent-return: [0, { "treatUndefinedAsUnspecified": false }]
    */
    const connectToDatabase = async () => {
        try {
            if (!DATABASE_URL || !DATABASE_NAME) {
                console.log('DB required params are missing');
                console.log(`DB required params DATABASE_URL = ${DATABASE_URL}`);
                console.log(`DB required params DATABASE_NAME = ${DATABASE_NAME}`);
            }
    
            mongoConnection = await connect(DATABASE_URL);
            db = mongoConnection.db(DATABASE_NAME);
    
            console.log(`DB connected = ${!!db}`);
            
            return !!db;
    
        } catch (err) {
            console.log('DB not connected - err');
            console.log(err);
        }
    };
    module.exports = {
        insertDocuments,
        findDocuments,
        removeDocuments,
        ObjectId,
        connectToDatabase
    };
    
  3. Replace the following in the script with your resource information:

    • YOUR_RESOURCE_PRIMARY_CONNECTION_STRING
  4. Run the script.

    node index.js
    

Next steps