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

202
Views
Unable to query MongoDB using a localhost URL and MongoJS

This is the tasks.js code I'm trying to run:

/*jslint node:true*/

var express = require('express');
var router = express.Router();

var mongojs = require('mongojs');
var db = mongojs('mongodb://localhost:27017/tasks', ['tasks']);

router.get('/tasks', function (req, res, next) {
'use strict';
db.tasks.find(function(err, tasks) {
   if(err){
       res.send(err);
   }
   res.json(tasks);
});
});

module.exports = router;

The code is meant to query and display all the contents of the json file.

When I replace the db localhost URL with this mLab URL:

var db = mongojs('mongodb://username:password@ds161008.mlab.com:61008/mytasklist_muhab', ['tasks']);

It works perfectly.

I assume there is a problem with the string. I looked up the connectionString standards in MongoDB docs and I couldn't locate the problem.

I haven't assigned any username or password to the local database.

Mongod is running fine and I am able to run commands on the same database using the Mongo shell without any problem.

over 4 years ago · Santiago Trujillo
1 answers
Answer question

0

According to mongojs documentation you may no need to use mongodb://localhost:27017 as part of your connectionString for local db can try by only dbName

like:

var db = mongojs('tasks', ['tasks'])

or

var db = mongojs('tasks')
var mycollection = db.collection('tasks')

and checked your connection established or not by using error or connect event

var db = mongojs('tasks', ['tasks'])

db.on('error', function (err) {
    console.log('database error', err)
})

db.on('connect', function () {
    console.log('database connected')
})
over 4 years ago · Santiago Trujillo 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!