Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

203
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda