The MongoDB driver depends on several other packages. These are: bson; bson-ext; kerberos; mongodb-client-encryption; Some of these packages include native C extensions, consult the trouble shooting guide here if you run into issues. This guide will show you how to set up a simple application using Node.js and MongoDB. Studio 3T is an amazing MongoDB GUI & IDE client that helps us to speed up tasks like query building, data exploration, import/export and code generation. Studio 3T is the best MongoDB GUI I have found. Full of useful features and a well built query system, it makes my job easier.
31st Jul 2019I always use MongoDB as a database when I work on an app. And I like to connect to a database on my computer because it speeds up dev and test-related work.
Today, I want to share how to create and connect to a local MongoDB Database.
Installing MongoDB
You need to install MongoDB on your computer before you can connect to it. You can install MongoDB by following these instructions (Mac and Windows).
Once you have completed the installation process, try typing mongo --version
into your command line. You should get a response similar to the following:
Starting MongoDB
You can start MongoDB on your computer with the mongod
command.
Keep the mongod
window running when you want to work with your local MongoDB. MongoDB stops when you close the window.
Brief overview of how MongoDB works
MongoDB lets you store things (called documents) inside databases. Each database contains multiple collections.
To make it easier to understand, you can think of MongoDB as a building. It contains many rooms.
Each room is a database. Each database is responsible for storing information about one application. You can store as much information as you want.
You have an unlimited supply of boxes in each room. Each box is a collection. Each collection can only contain one type of data.
For example, one collection can be used for books, one collection for users, one collection for toys, and so on.
Adding items to a database
One way to add items to a MongoDB database is through the Mongo Shell. To open up the Mongo Shell, you open another command line window and run mongo
.
Note: Make sure you keep the mongod
window open! You won’t be able to interact with the Mongo Shell if you close the mongod
window.
First, we need a database to work with. You can see the currently selected database with the db
command. (By default, you should be on the test
database).
Note: The >
in the code above signifies the Mongo Shell. You don’t need to type >
. It is not part of the command.
For this article, we’ll create a database called game-of-thrones
. You can use the use <database>
command to create and switch to a new database.
We’re going to add a character into the game-of-thrones
. Here, we need to put the character into a collection. We’ll use characters
as the name of the collection.
To add an item to a collection, you can pass a JavaScript object into db.<collectionName>.insertOne()
.
Let’s add one character into the database before we continue.
You can see the characters we’ve added by using the find
command. (db.<collectionName>.find()
).
This is all you need to know about the Mongo Shell for now.
Accessing MongoDB with MongoDB Compass
MongoDB Compass gives you another way to access MongoDB. It’s an app that makes checking (and editing) databases easier if you’re not a fan of the command line.
To use MongoDB Compass, you have to install it first. You can download and install MongoDB Compass from the this page.
When you open MongoDB Compass, you’ll see a screen that looks like this:
To connect to your local MongoDB, you set Hostname
to localhost
and Port
to 27017
. These values are the default for all local MongoDB connections (unless you changed them).
Press connect, and you should see the databases in your local MongoDB. Here, you should be able to see game-of-thrones
(the database we created for this tutorial).
If you click on game-of-thrones
, you’ll see a characters
collection.
And if you click on characters
, you’ll see the two characters we created in the earlier section.
This is how you can use MongoDB Compass to connect to a MongoDB that’s running on your own computer.
Connecting to MongoDB with a Node server
When we build applications, we connect to MongoDB through our applications (not through Mongo Shell nor MongoDB Compass).
To connect to MongoDB, we need to use the mongodb package. Alternatively, you can also use Mongoose.
(By the way, I prefer using Mongoose over the MongoDB native driver. I’ll share why in a future article).
Connecting with MongoDB native driver
First you have to install and require the mongodb package.
You can connect to your local MongoDB with this url:
With the Mongo Client, you need to specify the database you’re using after you connect to MongoDB. Here’s what it looks like:
Connecting with Mongoose
To connect with Mongoose, you need to download and require mongoose
.
When you use Mongoose, the connection url
should include the database you’re connecting to:
You can connect to MongoDB with the connect
method:
Here’s how you can check whether the connection succeeds.
If you enjoyed this article, please tell a friend about it! Share it on Twitter. If you spot a typo, I’d appreciate if you can correct it on GitHub. Thank you!
Visual Studio Code has great support for working with MongoDB databases, whether your own instance or in Azure with MongoDB Atlas. With the MongoDB for VS Code extension, you can create, manage, and query MongoDB databases from within VS Code.
Install the extension
MongoDB support for VS Code is provided by the MongoDB for VS Code extension. To install the MongoDB for VS Code extension, open the Extensions view by pressing ⇧⌘X (Windows, Linux Ctrl+Shift+X) and search for 'MongoDB' to filter the results. Select the MongoDB for VS Code extension.
Connect to MongoDB
Once you've installed the MongoDB for VS Code extension, you'll notice there is a new MongoDB Activity Bar view. Select the MongoDB view and you'll see the MongoDB Explorer.
To connect to a MongoDB database, select Add Connection and enter the connection details for the database then Connect, the default is a local MongoDB server at mongodb://127.0.0.1:27017
. You can also enter a connection string, click the 'connect with a connection string' link and paste the connection string.
Note: Make sure your MongoDB server (mongod.exe) is running if you are connecting to a local MongoDB server.
Once attached, you can work with the MongoDB server, managing MongoDB Databases, Collections, and Documents.
You can expand databases to view their collections with their schema and indexes and you can select individual MongoDB Documents to view their JSON.
You can also attach a MongoDB shell to the active connection, simply by right-clicking on the connection itself.
Note: Make sure the MongoDB shell (mongo
or mongosh
) is installed and is on your path. In the extension's settings, you can choose which shell you are using.
MongoDB Commands
There are MongoDB specific commands available in the VS Code Command Palette (⇧⌘P (Windows, Linux Ctrl+Shift+P)) as well as through Explorer context menus.
Using Playgrounds
One of the most powerful features of the VS Code MongoDB integration is Mongo Playgrounds. Playgrounds let you create, run, and save MongoDB commands from a VS Code editor. Create a new playground with the MongoDB: Create MongoDB Playground command.
In a playground, you can reference MongoDB entities and commands and you get rich IntelliSense as you type. Playgrounds are useful for prototyping database operations and queries. Execute selected lines in the playground queries with the MongoDB: Run Selected Lines From Playground command.
MongoDB on Azure
You can easily create a MongoDB cluster on Azure for Free with MongoDB Atlas.
Choose Create a New Cluster from the dashboard and choose Azure as the Cloud Provider. Once the cluster is created, connect to using the connection string provided by MongoDB Atlas.
Next steps
Mongo Client For Windows
- Azure Extensions - The VS Code Marketplace has hundreds of extensions for Azure and the cloud.
- Deploying to Azure - Learn step-by-step how to deploy your application to Azure.
- Working with Docker - Put your application in a Docker container for easy reuse and deployment.