How to setup a local MongoDB Connection | Zell Liew

How to setup a local MongoDB Connection | Zell Liew

I always use mongodb as the database when working on an application. and I like to connect to a database on my computer because it speeds up the work related to development and testing.

Today, I want to share how to create and connect to a local mongodb database.

Reading: How to connect to local mongodb

installing mongodb

you need to install mongodb on your computer before you can connect. you can install mongodb following these instructions (mac and windows).

once you have completed the installation process, try typing mongo -version on your command line. you should get a response similar to the following:

Terminal showing MongoDB Version

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 allows you to store things (called documents) within databases. each database contains several collections.

To make it easier to understand, you can think of mongodb as a building. contains many rooms.

each room is a database. each database is responsible for storing information about an application. you can store as much information as you want.

You have an unlimited supply of crates in each room. each box is a collection. each collection can only contain one data type.

for example, you can use a collection for books, a collection for users, a collection for toys, etc.

add items to a database

One way to add items to a mongodb database is through the mongo shell. to open the mongo shell, open another command line window and run mongo.

See Also:  How to Set NFT Profile Picture on Twitter and Instagram?

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.

See also: How to Connect a Landline Phone to a Modem or Wifi Router

First, we need a database to work with. you can see the currently selected database with the db command. (by default, it should be in the test database).

note: the > in the code above it means mongo shell. you do not need to type >. it is not part of the command.

Terminal returns

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.

Switch to a database named game-of-thrones.

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 element to a collection, you can pass a javascript object to db.<collection name>.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()).

Database returns two characters—Jon Snow and Arya Stark.

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. is an application that makes it easy to check (and edit) databases if you’re not a fan of the command line.

To use mongodb compass, you must first install it. you can download and install mongodb compass from this page.

when you open mongodb compass, you will see a screen like this:

See Also:  How to Use a Portable Jump Starter | Auto Simple

MongoDB Compass startup screen.

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).

Sets Hostname to localhost and Port to 27017.

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).

List of databases in my local MongoDB

See also: Alternator Wiring Diagram: A Complete Tutorial | EdrawMax

If you click on game-of-thrones, you’ll see a characters collection.

The characters collection in MongoDB Compass.

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.

connect to mongodb with a node server

when we build applications, we connect to mongodb through our applications (not through mongo shell or mongodb compass).

To connect to mongodb, we need to use the mongodb package. alternatively, you can also use mongoose.

(by the way, I prefer to use mongoose instead of the native mongodb driver. I’ll share why in a future article).

connecting to mongodb native driver

You must first 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 are using after connecting to mongodb. this is how it looks:

Connected to local MongoDB with MongoDB native driver.

Connecting with Mongoose

to connect with mongoose, you need to download and require mongoose.

when using mongoose, the connection url must include the database you are connecting to:

you can connect to mongodb with the connect method:

See Also:  9 Tips to Install IKEA Kitchen Cabinets | The DIY Playbook

this is how you can check if the connection was successful.

Connected to local MongoDB through Mongoose.

If you enjoyed this article, please support me by sharing this article Twitter or buying me a coffee 😉. If you spot a typo, I’d appreciate if you can correct it on GitHub. Thank you!

See also: Volkano Bluetooth Wireless Stereo Earbuds Instruction Manual – Manuals

Related Articles

Leave a Reply

Your email address will not be published.

Back to top button