I can't connect to the local mongodb server running on localhost:27017 from my express application. I'm pretty sure that I haven't done anything wrong in the code I wrote as I've copied it from a tutorial. The code worked fine on that tutorial. And I've also made sure that the mongodb service is running. I've already created a database called "blog" from mongosh shell and inserted some data to a collection called "authors" on it. I've also installed the MongoDB Node Driver using "npm install mongodb" command. But whenever I try to run npm start, this error shows up and I can't run my application thereafter. Show
This is the code I wrote on database.js to create the connection to the local mongodb server.
This is the code I wrote on app.js to connect the database to it and run the application. I 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 MongoDBYou 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 7 into your command line. You should get a response similar to the following:
Starting MongoDBYou can start MongoDB on your computer with the 8 command.
Keep the 8 window running when you want to work with your local MongoDB. MongoDB stops when you close the window.Brief overview of how MongoDB worksMongoDB 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 databaseOne 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 0.
Note: Make sure you keep the 8 window open! You won’t be able to interact with the Mongo Shell if you close the 8 window.First, we need a database to work with. You can see the currently selected database with the 3 command. (By default, you should be on the 4 database).
Note: The 5 in the code above signifies the Mongo Shell. You don’t need to type 5. It is not part of the command.For this article, we’ll create a database called 7. You can use the 8 command to create and switch to a new database.
We’re going to add a character into the 7. Here, we need to put the character into a collection. We’ll use 0 as the name of the collection.To add an item to a collection, you can pass a JavaScript object into 1.
Let’s add one character into the database before we continue.
You can see the characters we’ve added by using the 2 command. ( 3).
This is all you need to know about the Mongo Shell for now. Accessing MongoDB with MongoDB CompassMongoDB 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 4 to 5 and 6 to 7. 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 7 (the database we created for this tutorial).If you click on 7, you’ll see a 0 collection.And if you click on 0, 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 serverWhen 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 driverFirst you have to install and require the mongodb package.
You can connect to your local MongoDB with this url: 0With the Mongo Client, you need to specify the database you’re using after you connect to MongoDB. Here’s what it looks like: 1Connecting with MongooseTo connect with Mongoose, you need to download and require 2. 2 3When you use Mongoose, the connection 3 should include the database you’re connecting to: 4You can connect to MongoDB with the 4 method: 5Here’s how you can check whether the connection succeeds. 6If 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! MongoDB menggunakan bahasa apa?MongoDB sendiri ditulis dengan bahasa C++ dan telah tersedia untuk berbagai jenis bahasa pemrograman. Fitur utama dari mongoDB antara lain: model document-oriented storage.
MongoDB digunakan untuk apa?3. Cocok Untuk Menampung Data yang Bervariasi
Dynamic schema membuat MongoDB cocok untuk menampung data yang bervariasi baik digunakan untuk menyimpan data yang terstruktur ataupun yang tidak terstruktur.
Apakah MongoDB NoSQL?Mongo DB adalah salah satu jenis database yang menggunakan konsep NoSQL berbasis dokumen. MongoDB ini merupakan basis data NoSQL yang gak menyimpan data dalam tabel, akan tetapi menggunakan dokumen terstruktur layaknya JSON (JavaScript Object Notation).
Mengapa MongoDB disebut database berorientasi dokumen?Berorientasi pada dokumen-Karena MongoDB adalah database tipe NoSQL, alih-alih memiliki data dalam format tipe relasional, MongoDB menyimpan data dalam dokumen. Ini membuat MongoDB sangat fleksibel dan mudah beradaptasi dengan situasi dan persyaratan dunia bisnis nyata.
|