>> Hi everyone. My name is Simona Cotin,
and I'm web developer here at Microsoft.
In this video we're going to learn how to connect to
a MongoDB database from our serverless API.
In order to do that,
we first need to have a MongoDB database.
You can see here in my VSCode instance,
in going into the Azure extensions,
I have in my list of databases of beginner products,
MongoDB database, which is called serverless,
that includes product collection with a list of two documents.
This is an Azure Cosmos DB database that uses a MongoDB model.
To connect to our database,
the first thing that we need to do in our serverless function is
run NPM install MongoDB.
This will update our package.json file to include
our dependency on the MongoDB Node.js driver.
Then going into the index.js file,
first thing that we want to do is actually
import the MongoDB driver.
I'm going to type here const mongo client = require(''Mongodb'').
We no longer need to read our data
from the static file that we used earlier.
I'm going to delete this import here of the product data file.
Then I will replace
the products object here to return an empty array for now.
Now we need to connect to our database and to do that,
we're going to define connect function.
That's an asynchronous function.
Here we want to initialize client object that uses
MongoClient to connect to
our database and we need to pass in a connection string.
To get that connection string,
I'll go back into my Azure extension, the databases extension.
Right here on the Beginnerproduct database,
I can right-click and you can see that I have the option of
copying the connection string directly here from VSCode.
I'll click ''Copy connection string.''
We have two options.
I can either paste in
the connection string directly here in my code,
but I would highly advise against that because our code will be
committed to GitHub and we don't want to
expose our keys right here in our code.
I'll go back into the File Explorer and we
can see here that we have a local that's settings that JSON file.
This is a file where we can safely
store our environment variables.
I'll add a new value here that's called DB_CONNECTION,
and this is where I will paste in
the connection string I've copied earlier.
In order to use it in our code,
I'm going to have to use process.env.DB_CONNECTION.
Once we connect to our database,
we want to return our client object,
which we're going to use in our main method here in our function.
First, I want to initialize the client object and await
for our connection to execute.
Then I want to initialize or create a new object called database.
Here I'll use our client to select the serverless database.
Just to test out that this is working correctly,
I will console.log(''connected to our database'').
Let me just go run this directly in VSCode in
the Debug panel here I will click the "Play" button.
What this will do is it will kick off
the Azure Functions runtime directly in my local machine,
which will then allow me to run my application locally
and debug my serverless functions very easily using VSCode.
Let's go and first inspect
what does our serverless API endpoint return.
Now, you can see here it's an empty array,
which makes sense because that's what we
define our list of products to be.
We can also see the console log message connected to our database,
so this is great.
Next thing that we want to do here is use our database object
here and select the product collection.
Here we want to retrieve all products so we won't have
any filter function and we want that to be an array.
Once we restart our server,
we'll be able to see the updated list of products.
Going back into the browser and now refreshing the web-page.
We can see that our list is still the same.
That's because we want to actually await
on the query results to be returned.
Going back in our browser and refreshing the page here,
and now we can see the two products that
we have created or we have in our database.
Let me just go back into the database itself
and into the sliced bread document.
I've changed my mind,
this is not the best loaf of fresh sliced wheat bread.
I'm going to change
that description and upload that data to Cosmos DB,
and by refreshing the webpage,
we can now see that the data is being retrieved dynamically.
[MUSIC]