MongoDB for VS Code - MongoDB Developer Tools
Rate this video
00:00:00Introduction to MongoDB for VS Code
00:00:42Installation and Database Connection
00:01:51Navigating and Managing Collections
00:02:48Using MongoDB Playgrounds
00:03:51Exporting Queries to Other Languages
00:04:46Creating a Blank Playground Template
00:05:36Advanced Querying with Geospatial Data
00:06:37Enriching Collections with External Data
00:07:37Using the MongoDB Shell in VS Code
00:08:37Automating Atlas Infrastructure with Terraform
00:09:37Conclusion and Call to Action
The main theme of the video is the comprehensive functionality of the MongoDB for VS Code extension, which streamlines the development process by integrating MongoDB's features directly into the VS Code environment.
🔑 Key Points
- MongoDB for VS Code extension is generally available with new updates.
- Features include database navigation, schema overviews, document editing, intelligent auto-completions, and built-in MongoDB shell.
- Playgrounds allow for rapid prototyping and exporting to other languages.
- Terraform templates are provided for automating Atlas infrastructure setup.
- The extension supports npm packages for enriching collections with external data.
🔗 Related Links
Full Video Transcript
the mongodb for vs code extension is now generally available there have been some amazing updates so let me show you what it can do at a high level the extension allows you to navigate your databases Collections and documents in mongodb you can get a quick overview of schemas and indexes you can use playgrounds to Aggregate and to write scripts it's great at rapid prototyping you can edit documents and save changes to the database it has intelligent Auto completions the mongodb shell is built in and you can even export queries and aggregations from your playgrounds to other programming languages right from inside vs code first install the mongodb for vs code extension if you haven't already you can search for mongodb in the extensions panel in vs code or use the link in the video description after installing it go to the new mongodb tab on your sidebar and let's connect to a database using our connection string now if you need help setting up a new mongodb Atlas cluster take a look at this video which is linked in the video description you can also connect to a local or self-hosted mongodb database now that we've connected we can see all of the databases associated with this cluster we can expand one of these and we can see the collections in it this collection for instance has 24 000 documents and we can quickly see how the documents are structured by looking at the schema this is extremely useful when you're trying to build out a query since it lets you quickly identify which Fields you can query and what the field types are now the schema is inferred from a subset of the documents in the collection you can also see indexes on each collection and even add indexes without leaving vs code this makes it easy to ensure that the query that you're writing is covered by an index we can even open one of these documents right here in vs code to get an idea of its structure and we could update and save the document back to the database if we wanted we can also easily see the different types of collections based on their icons here I have a Time series collection alongside other regular collections but one of the best features of this extension is the mongodb playground playgrounds are JavaScript environments where you can run any mongodb shell command and they're really great to prototype queries and aggregations playgrounds also play nicely with JavaScript tools like eslint and prettier so let's go ahead and create a playground now this is just a file and it comes with a template that shows examples of how to use the playground so again this is using standard shell commands so here we're using the mongodb vs code playground DB database I don't currently have that database in this cluster we can go over to the mongodb tab and we can look at this cluster and see that it's not there so when this playground is run it will create this database for me next we're going to get the sales collection and then insert several documents and then we're going to execute a find and then console log the sales count that occurred third on April 4th 2014 and then finally we're going to execute an aggregation pipeline which looks at all of the sales for 2014 and then we'll see the results for that so let's go ahead and run this playground by clicking the play button in the top right and then we're going to get a confirmation message that pops up now this is going to run these commands on our actual cluster so we do need to be careful we'll go ahead and say Yes here and we'll see the results so we can see our console log here that there were two sales and then we can see the results of our aggregation which shows all of the sales now let's say that we want to actually Implement what we've written here in another language the mongodb for vs code extension makes that really easy now all you have to do is select the bits that you want to convert to another language for example I want this aggregation pipeline here now I can open up the command palette and then search for mongodb extract and you can see I can choose from node.js c-sharp Python 3 Java Ruby so I'm going to select Java and I'm going to get the Java code now that I can use in my Java application that's so cool now the playground template is nice to have when you're first getting started but what if you want to just start off with a blank template well we can disable the template in the vs code settings in settings search for playground and then uncheck the mongodb use default template for playground let's go ahead and create a new Playground now and this one will be blank so I'm going to start out by using the sample restaurants database which is part of the sample data that you can load from the atlas dashboard let's do a simple find on the restaurants collection notice the intelligent autocomplete built into the extension as I'm typing it knows the available collections for the database and then as I type find it shows the available methods and even some extra information about the method and a link to the mongodb documentation very convenient let's run this so we can see that this collection contains restaurants their addresses the cuisine type grades and of course their names so we could use this data to find restaurants within a certain geographic area so let's look for restaurants near the mongodb New York office so I'm going to define the geo coordinates for the mongodb office and then we're going to add an aggregation and use the dollar Geo near operator notice again the built-in autocomplete has given me all the parameters for the dollar Geo near operator so we're going to define the field where the distance is going to be stored we'll call that distance the max distance we'll say 100 and we're going to use spherical geometry now before we run this we have to make sure that we have a geospatial index set up for this collection so we can really quickly go over to the mongodb tab in vs code and look at the indexes and see if there's an index set up yet and we can see that there isn't one so let's go ahead and create one just to the right of indexes we can click the plus button and we'll be given a new playground with a template to create an index now this template contains all of the available properties to create an index for the restaurant's collection so we could create a normal index a wildcard index a column store index text index geospatial index or a hashed index and of course all of the other available options so for this we just want a geospatial index so let's copy this bit here we'll paste it up at the top and the location field is going to be address.cord and we know that because if we go over to the mongodb tab again and we go over to our schema for this collection we can see addresses and then core so let's go ahead and run this index playground and then we can refresh our collection and we can see our new index here let's go back to our other playground with our Geo near aggregation and let's run this now we can see that we have a good bunch of restaurants that are very close to the office now to make this even more awesome in mongodb playgrounds you can actually use any npm package you can use any of the Native node.js modules without any further configuration like the fs module for the file system but to use other modules or packages these will need to be installed in a specific directory locally there's an article Linked In the description with those directories I'm on windows so in my terminal I'm going to navigate to my user directory and then run npm install axios we're going to use the axios package and our mongodb playground so I want to show you how we can enrich an existing collection using playgrounds and npm packages back on the mongodb tab I have the sample inflix database from our sample data as well and when we look at the schema for this for the movies collection we see cast countries directors genre IMDb languages plot ratings but there's one thing that's missing there's no movie posters so let's use the omdb API the open Movie Database API to get movie posters for movies that we already have in our sample inflix database now this API does require an API key but you can get one of those for free and they have a generous quota for testing first we'll require the axios package and then we're going to create a helper function to get the movie poster from the API and we'll need to be sure that we use the sample inflix database after that we'll Define a very simple aggregation that's just going to get the first 10 movies from the collection then we're going to Loop over those movies and we're going to surround this loop with an async iffy we'll get the title we'll construct the URL and then we'll get the movie poster from the API and lastly we'll enrich our collection by updating that document that has the same ID and we're going to set the poster equal to the movie poster URL that we receive from the API lastly let's run that aggregation again on the first 10 documents this time we're just going to project the title and the poster and let's just verify that the URLs for the posters were added so let's run this and now we can see the posters for all of these documents are there if we go back to the mongodb tab and we refresh the schema for the movies collection we can now see poster as a field in the schema now did you know that you can also use the mongodb shell right inside vs code 2. one important note here you do need the mongodb shell installed on your machine and have configured your path environment variable to include the file path to your shell binary now for more information on how to do that there is an article Linked In the video description so let's run that last playground aggregation in the mongodb Shell so in vs code open up the command palette and search for mongodb launch mongodb Shell let's make sure that we are using the correct database so we'll type use and then sample inflix and now we can paste in that aggregation and run it and we can see the exact same results as we had from our playground it's really nice to have all of these mongodb tools all in one place did you know you can also automate your Atlas infrastructure sure with terraform you can and with vs code we make it Easy by providing a template to help you get started you will need to have terraform installed on your computer of course and then create a new file named main.tia in this file just start typing Atlas and you're going to see this template pop up hit enter and all of the information that is needed is provided as placeholders and you can just tab through these and replace them so the first placeholder is the project name and then I can hit Tab and now I can enter the cluster name and hit tab now I can choose the cloud provider and so on and then we give you all of the hints that you need to set up your region and cluster size and now you can run this through terraform and you'll have your Atlas infrastructure ready to go so what do you think give this video a like if it was helpful as you can see the mongodb for vs code extension is a powerful tool with tons of features that can boost your productivity and make your mongodb experience smoother and more efficient now it's your turn to try it out remember you can find the mongodb for vs code extend function in the vs code extension panel or use the link provided in the video description start exploring your databases running shell commands using playgrounds export your queries to different programming languages and automate your Atlas infrastructure with terraform we'd love to hear your feedback what do you like what can we improve your input can help shape the future of this tool so please share your experiences and suggestions in the comments below and be sure to subscribe for more mongodb tutorials tips and tricks