Can not connect Azure Functions to MongoDB (created in azure)

azure functions nodejs mongodb
azure mongodb pricing
azure functions crud
can azure functions access documentdb
azure function context
cosmos db mongodb api c#
azure devops mongodb
azure mongodb rest api

I have a MongoDB setup on azure, and I am tring to connect to it via azure function.

These are the steps I took:

  1. Creating a Simple Azure Function
  2. Installed the MongoDB Driver on Azure, To install the MongoDB Node.js driver, I went go to .scm.azurewebsites.net, and clicked on 'Debug Console' -> 'PowerShell'.
  3. I Navigated to the D:\home\site\wwwroot directory and clicked on the plus icon to create a new file called package.json.
  4. I Created and saved the below package.json file.

      {
      "name": "nameofunction",
      "dependencies": {
       "mongodb": "3.x"
      }
    }
    
  5. Next, I ran npm install from the shell.
  6. From the Azure Function I should be able to connect to MongoDB and execute a query using the below code.

    const  mongodb = require('mongodb');
    const  url = "mongodb://cosmod:  <PASSWORD>==@cosmodb.documents.azure.com:10255/?ssl=true&replicaSet=globaldb";
    
    module.exports = async function (context, req) {
        mongodb.connect(url, function(error, client) {        
          if (error) throw error;
          var dbo = client.db("mydb");
          dbo.createCollection("customers", function(err, res) {
             if (err) throw err;
             context.log("Collection created!");
             db.close();
          });         
       });
     };
    

My code is throwing up a Status: 500 Internal Server Error

The more I look at the code, the more i can not understand why this should not work. The package-lock.jsonhas all the dependencies loaded after I ran npm install in the shell.

I appreciate any help in resolving this.


This seems weired, I also followed same and was able to connect to my db. Can you please check your cosmos connectiondb, mongo compatible connection string? Are you able to connect that from other mongo clients

Can not connect Azure Functions to MongoDB (created in azure , This seems weired, I also followed same and was able to connect to my db. Can you please check your cosmos connectiondb, mongo  This article will walk you through setting up an Azure Function in Node.js that connects to MongoDB and reuses the database connection between requests. Creating a Simple Azure Function. Log in to the Azure Portal and click "Create a resource." Click "Compute" and then "Function App" to create a new Function App.


Status: 500 Internal Server Error

I assume that it dues to the code mongodb.connect(url, function(error, client)

please change the code to

mongodb.MongoClient.connect(uri, function(error, client)

Getting Started With Azure Functions and MongoDB, Does a stateless function mean you have to create a new connection to your mongoose, but I haven't been able to find any information on Azure Functions. Click "Create" and Azure will kick off creating your Function App. I have a problem connecting to a MongoDB from a C# function running on microsoft azure. The function is time triggered, it should collect some data from MongoDB and put them to storage queue. MongoDB.Driver.Core: Could not load file or assembly 'System.Runtime.InteropServices.RuntimeInformation, Version=4.0.0.0, Culture=neutral,


A review of the Azure Cosmos DB account- Quick start documentation accessible via the Azure Cosmos DB account menu side blade; connecting the MongoDB app is among others via:

  1. the Node.js 2.2 driver and
  2. the Node.js 3.0 driver

I was using the Node.js 2.2 driver connection string in the azure function which is not compatible with the Node.js 3+ driver dependency in my app. Using the Node.js 3.0 driver connection string, I was able to connect the MongoDB app, without the error. The double equality sign in the password string is url encoded in the 3+ driver.

Node.js 3+ driver connection string

 var mongoClient = require("mongodb").MongoClient;
 mongoClient.connect("mongodb://cosmodb:<PWD>%3D%3D@cosmodb.documents.azure.com:10255/?ssl=true", function (err, client) {
  client.close();
});

Node.js 2.2 driver connection string

var mongoClient = require("mongodb").MongoClient;
mongoClient.connect("mongodb://cosmodb:<PWD>==@cosmodb.documents.azure.com:10255/?ssl=true", function (err, db) {
  db.close();
});

Creating CRUD with Azure Functions & MongoDB, Azure Functions Core Tools will allow us to develop and test functions locally on our machine from a Here we are creating our local connection to MongoDB! Because without it, we cannot perform CRUD operations! You now have an Azure Function. It’s not great though so let’s do something about that. Connect to MongoDB Cluster. Sweet, now that we’ve set up our function, let’s grab our connection string to our MongoDB Atlas cluster. Head to your Atlas portal (if you’re not there already) and sign in. Click the ‘connect’ button as seen below:


Connect a MongoDB application to Azure Cosmos DB, Learn how to connect a MongoDB app to Azure Cosmos DB by You can then use an Azure Cosmos database as the data store for your If you don't have an Azure account, create a free Azure account now. This is a valid format for connecting to cosmos db, but not all the functionality of the api will be  Learn how to connect your MongoDB app to an Azure Cosmos DB by using a MongoDB connection string. You can then use an Azure Cosmos database as the data store for your MongoDB app. This tutorial provides two ways to retrieve connection string information: The quickstart method, for use with .NET, Node.js, MongoDB Shell, Java, and Python drivers.


MongoDB Atlas free tier is now available on Azure, The newly available free tier on Azure Cloud is known as the M0, grants users 512 Like larger MongoDB Atlas cluster types, M0 clusters can grant users optimal security (CRUD) operations against their data right from their browsers via the built-in Data Explorer. Database Azure Functions Mongodb. Just click the Create button in the MongoDB tile to create a free MongoDB database on Azure. Once the database is created, navigate to Connection String in the portal and retrieve your Azure Cosmos DB connection string for use later in the tutorial.


Manage connections in Azure Functions, Do not create a new client with every function invocation. Do create a single, static client that every function invocation can use. Consider creating  If needed, create a Network Security Group rule to allow access to MongoDB from outside of the existing Azure virtual network subnet. You can create the Network Security Group rules by using the Azure portal or Azure PowerShell. As with the Windows Firewall rules, allow TCP port 27017 to the virtual network interface of your MongoDB VM.