Today I'm going to show you how to interact with
MongoDB without ever leaving VS code. We're going to be
using MongoDB playgrounds to prototype aggregations and queries, all while
getting Intellisense from within your editor. All you need to
get started is a MongoDB connection string and the MongoDB
for VS code extension. So I'm going to start from scratch
and create my database and I'm going to use Azure
to host it. I have the Azure Tools extension installed,
which gives me the Azure databases extension. I can now
quickly create an Azure cosmos DB for the MongoDB
API, which essentially lets you use Azure Cosmos DB as
if it were a MongoDB database. I'm already signed into
my Azure subscription, so I'll just type in the command
palette Azure databases create new server. We'll then select the
Azure Cosmos DB for MongoDB API option. We'll give it
a name, I'm going to give it playgrounds, select a
capacity model and select a resource group, and I'm going
to create a new resource group named playgrounds. Select a
location, and we'll give that just a few minutes to
get spun up.
Now that we have that database created, we can dive
into the MongoDB for VS code extension which can
be found in the extension marketplace by searching for MongoDB.
Once you have this extension installed, you'll get this MongoDB
icon in your sidebar and if we click on that
we see here that it prompts me to add a
connection. We're just going to use the connect with a
connection string option, so I'm going to head over to
the Azure blade to copy the connection string. I'm going
to right click on that playgrounds DB that we created
and select copy connection string.
I'll paste it into the connection string prompt and now
we're properly connected. If we go back to the MongoDB
blade, we can see that we have this new connection.
Now I'm going to use MongoDB playgrounds to add a
new collection to this database. In your playground you need
to specify what database you're using, so I'm doing that
with this use('playgrounds') statement. So I'm going to create
my collection by using the insert method, which will automatically
create the collection if it does not exist and then
insert the documents. So I want my collection to be
called students and I'm going to do
db.students.insertmany
and then I have this test data already set
up that I want to have in my collection. The
data includes a student ID, the section of the course
that they are in, the grade that they got on
the most recent exam, and their current GPA. I can
then hit this play button and that will run the
playground and we can see the result of our playground
run here and it successfully inserted these 30 documents. And
now in the MongoDB sidebar we can expand the playgrounds
connection and see that our database has a new collection
with 30 documents in it. Now that I have this collection created,
I'm going to create a new playground and take a
look at some of the other CRUD operations I can
use. To read, I would use the find method. So
let's say I want to look up the details of
the student with the ID 8. I would do
db.students and notice the Intellisense that pops up as
I do that, .find, and I'm going to use
the find one method because I just want the single
document and I'm just going to specify that I want
to find the student with the ID 8. And if
we run this playground we can see that I get
the document back for the
student with ID 8. So student 8 actually did
some extra credit and I want to update their grade
from an 88 to a 90. So to do that I'm
actually going to change this find one method to find
one and update so that we can do it all
in one step. And then I'll just add my set
grade to 90 clause to it. And after running that
we can see that the student's grade has been updated
to 90. And I can also delete documents. So for
instance if student 16 dropped my class, I'd want to
delete them from my data set. If we take a
look in the MongoDB
side panel, we can see that student 16 currently exists
in our collection. So I'm going to type
db.students.deleteOne and specify that I want to
drop the student with ID 16. And so far I've
been running the entire script by clicking the play button,
but I can also choose to run just the selected
lines. So I'm going to do that for this delete
statement, but I need to make sure that my selected
lines include the use statement so that the playground knows
which database we are querying against. Once I have
those lines selected, I can open the command palette and
type MongoDB, run selected lines and we can see that
the document with ID 16 is no longer available in
our collection. The last thing I want to show you
is something called aggregation pipelines. Aggregation pipelines process documents through
stages where each stage performs an operation on the documents.
So I'm going to use aggregation pipelines to calculate some
averages on our collection. For example, if I want to
see what the average was for each section, I could
do this aggregation here.
And what this is doing is in this first stage,
it's filtering out the data to only include students in
section A. Then in the second stage, it's taking that
filtered data from stage one and then performing an average
calculation on the grade field. So if we run this,
section A had an average of about 78, Section B
had an average of 71, and section C also had
an average of about 78. So section B had almost
a whole letter grade difference in those averages. Next, I
want to see if there's a correlation between each student's
GPA and how they performed on this exam. So we
can update this query to match based on GPAs
less than 3.0 and we can see that the average
there was 65, and if we change the operator to
be greater than 3.0, the average jumps to 82. So
we can see that there does seem to be a
correlation in higher exam scores for students with higher GPAs.
The Mongo DB for VS code extension lets you seamlessly
interact with your database, benefiting from rich Intellisense while prototyping
queries and aggregations. If you want to try out MongoDB
for the first time,
it's quick and easy to create a new database using
Azure Cosmos DB for the MongoDB API. Make sure to
like this video and subscribe to our channel for all
things VS Code. Happy coding!