Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

151
Views
mongoose is not creating db using mongoose.connect

I'm trying to create new database using mongoose.connect('mongodb://localhost:27017/imagesDB'); But is not creating. And please don't answer me saying that I need to add entries to it. I did it. Here's my full code.

const express = require("express");
const mongoose = require('mongoose');

const app = express();

mongoose.connect('mongodb://localhost:27017/imagesDB').then(() => {
console.log("Connected to Database");
}).catch((err) => {
    console.log("Not Connected to Database ERROR! ", err);
});

const imageSchema = new mongoose.Schema ({
    name: String,
    category: String,
    author: String,
    tag: String,
    imageURL: String
});

const Image = mongoose.model("Image", imageSchema);

const image = new Image({
    name: "Cat",
    category: "Animal",
    author:"Cat",
    tag: "Animals",
    imageURL: "https://cdn-media-2.freecodecamp.org/w1280/5f9c9a5d740569d1a4ca2531.jpg"
});

image.save();

app.get("/", function(req, res){
    res.send("Everything is running properly!");
});

app.listen(3000, function(){
    console.log("Server succesfully started on port 3000!");
});

Can you guys please test it for yourself? Does this creates a new database called imagesDB???

For me no, It is not working. I opened new tab in terminal and commanded mongo so it opened mongo client where i typed show dbs but it is not showing except these three admin, config and local.

I also wanna make it clear that mongo service is running, I did it by commanding mongod --ipv6 in terminal.

about 4 years ago · Juan Pablo Isaza
3 answers
Answer question

0

You have to created your database named "imagesDB" in MongoDB Compass, or other GUI tool.

about 4 years ago · Juan Pablo Isaza Report

0

The problem is solved by changing localhost to 127.0.0.1 .

Here's how it looks

mongoose.connect('mongodb://127.0.0.1:27017/imagesDB').then(() => {
console.log("Connected to Database");
}).catch((err) => {
    console.log("Not Connected to Database ERROR! ", err);
});
about 4 years ago · Juan Pablo Isaza Report

0

Are you using Mongoose Compass or Atlas? Secondly, try doing it on VSCode terminal it will work then

about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!