Mongodb localhost connection

Hi - I am trying to view mongodb collections (just to view) in browser URL by accessing localhost:27017/example.

The mongod server started in cmg prompt.

this is the code writtem:

var db = "mongodb://localhost:27017/example";
mongoose.connect(db, { useNewUrlParser: true, useUnifiedTopology: true });

const conSuccess = mongoose.connection
conSuccess.once('open', _ => {
  console.log('Database connected:', db)
})

in the terminal hen I ran it says
“database connected”

when I access the browser localhost as
localhost:27017/example

It looks like you are trying to access MongoDB over HTTP on the native driver port.

please help me how I can view the db collections in browser. Thanks

1 Like

Hi @PP_SS,

There used to be a brief management http interface but it was removed:

https://docs.mongodb.com/manual/core/security-hardening/#http-status-interface-and-rest-api

To access the database via UI we recommend connecting via compass product:

You can also connect this instance to our Cloud Manager service and use data explorer via the automation agent:

Otherwise a driver or a mongo shell can show collections and query them.

mongo ...

Thanks,
Pavel

Hi - Thanks for the quick reply. MongoDb compass is up and running. but not the UI, i am able to access the localhost on example db to view it.

1 Like

@PP_SS, if I understand correctly you are good to go right?

I am stuck why is the localhost:27017/example in UI saying still error as

It looks like you are trying to access MongoDB over HTTP on the native driver port.

mongodb compass inside i am able to see the database and its collections

@PP_SS,

You can’t use localhost:27017/example as url.

It only works via a MongoDB native driver.

Thanks
Pavel

I see that Udemy course that accessed localhost:4000/books but not the localhosst:27017/example

and I accessed like
http://localhost:4000/books it says error

ReferenceError: exec is not defined.

FYI Example is a database and books collection is created in it. in mongodb compas, i am able to access example database and i see books collection in it. not syre why is this error comes. do you have any idea?

They definitely have an application and web server on 4000 port showing web pages.

Its not something coming built-in with MongoDB.

I have created that application. after that only i am running the server.

Here is the code:

var express = require("express");
var app = express();
var bodyParser = require("body-parser");
var mongoose = require("mongoose");
var Book = require("./book.model");


var db = "mongodb://localhost:27017/example";
mongoose.connect(db, { useNewUrlParser: true, useUnifiedTopology: true });

const conSuccess = mongoose.connection
conSuccess.once('open', _ => {
  console.log('Database connected:', db)
})

conSuccess.on('error', err => {
  console.error('connection error:', err)
})

var port = 4000;
//REST get
app.get('/', function(req,res){
  res.send("happy to be here");
});


//get all "books" collections
app.get('/books', function(req,res) {
  console.log("get all books");
  Book.find({})
  exec(function(err, books){
    if(err){
      res.send("error has occured");
    } else {
      console.log(books);
      res.json(books);
    }
  }); 
});


app.listen(port, function () {
  console.log("app listening on port " + port);
});

and book.model.js code

var mongoose = require("mongoose");

var Schema = mongoose.Schema;

var BookSchema = new Schema({

  title: String,

  author: String,

  category: String,

});

module.exports = mongoose.model("book", BookSchema);

can anyone please help me?

Could you share a screenshot of the error and status of the MongoDB server on this local host?

Sure, I have attached three screenshots onfile saved folders, mongodb server start and url entered port.

Uploading: mongodb-server.PNG…Uploading: mongodb-server.PNG(1)…

Sorry that screenshot uploading takes always reading and not able to sent thrid one.

this screenshot is server started by entering mongod in cmd propmpt. i have no idea why it is not working in URL.

@PP_SS,

Error seems to be a js error and not MongoDB related.

Are you sure this mongoose version is compatible with 4.4 MongoDB server?

Thanks
Pavel

Thanks for your reply, Pavel. I will check with javascript code again.

I am using mongoose version 5.11.13 is that compatible with 4.4 mongodb server? I will google on it as well. thanks

Hmmm might be A node bug:

Please try to update node and npm versions…

Thanks,
Pavel