Okay, where do I start. About five years ago I attended a Coding Bootcamp. My memory is fuzzy but I think I got some MongoJS applications to work. I’m coming back to it now, trying to practice, but when I try to start an old program I get this error:
Database Error: MongoServerSelectionError: C ::1:27017
at Timeout._onTimeout (C:\Users\Dennis\Documents\MongoTest\node_modules\mongodb\lib\core\sdam\topology.js:438:30)
at listOnTimeout (node:internal/timers:564:17)
at process.processTimers (node:internal/timers:507:7) {
reason: TopologyDescription {
type: 'Single',
setName: null,
maxSetVersion: null,
maxElectionId: null,
servers: Map(1) { 'localhost:27017' => [ServerDescription] },
stale: false,
compatible: true,
compatibilityError: null,
logicalSessionTimeoutMinutes: null,
heartbeatFrequencyMS: 10000,
localThresholdMS: 15,
commonWireVersion: null
}
}
MongoServerSelectionError: connect ECONNREFUSED ::1:27017
at Timeout._onTimeout (C:\Users\Dennis\Documents\MongoTest\node_modules\mongodb\lib\core\sdam\topology.js:438:30)
at listOnTimeout (node:internal/timers:564:17)
at process.processTimers (node:internal/timers:507:7) {
reason: TopologyDescription {
type: 'Single',
setName: null,
maxSetVersion: null,
maxElectionId: null,
servers: Map(1) { 'localhost:27017' => [ServerDescription] },
stale: false,
compatible: true,
compatibilityError: null,
logicalSessionTimeoutMinutes: null,
heartbeatFrequencyMS: 10000,
localThresholdMS: 15,
commonWireVersion: null
}
}
They say something like I have to update my ip address to 127.0.0.1 but how on Earth do I do that? I’ve tried the bind_ip command in mongod but that doesn’t work.
They say I’m supposed to modify this in mongod.cfg but I don’t have one anywhere in my bin.
I’m not Mongo expert but I’ve been Googling like crazy trying to figure this out. I need specifics. I just want to get a simple MongoJS program running.
Replace localhost with 127.0.0.1 in your code
Where? This is the sum total of my code:
// Dependencies
var express = require("express");
var mongojs = require("mongojs");
// Initialize Express
var app = express();
// Database configuration
// Save the URL of our database as well as the name of our collection
var databaseUrl = "zoo";
var collections = ["animals"];
// Use mongojs to hook the database to the db variable
var db = mongojs(databaseUrl, collections);
// This makes sure that any errors are logged if mongodb runs into an issue
db.on("error", function(error) {
console.log("Database Error:", error);
});
// Routes
// 1. At the root path, send a simple hello world message to the browser
app.get("/", function(req, res) {
res.send("Hello world");
});
// 2. At the "/all" path, display every entry in the animals collection
app.get("/all", function(req, res) {
// Query: In our database, go to the animals collection, then "find" everything
db.animals.find({}, function(err, found) {
// Log any errors if the server encounters one
if (err) {
console.log(err);
}
// Otherwise, send the result of this query to the browser
else {
res.json(found);
}
});
});
// 3. At the "/name" path, display every entry in the animals collection, sorted by name
app.get("/name", function(req, res) {
// Query: In our database, go to the animals collection, then "find" everything,
// but this time, sort it by name (1 means ascending order)
db.animals.find().sort({ name: 1 }, function(err, found) {
// Log any errors if the server encounters one
if (err) {
console.log(err);
}
// Otherwise, send the result of this query to the browser
else {
res.json(found);
}
});
});
// 4. At the "/weight" path, display every entry in the animals collection, sorted by weight
app.get("/weight", function(req, res) {
// Query: In our database, go to the animals collection, then "find" everything,
// but this time, sort it by weight (-1 means descending order)
db.animals.find().sort({ weight: -1 }, function(err, found) {
// Log any errors if the server encounters one
if (err) {
console.log(err);
}
// Otherwise, send the result of this query to the browser
else {
res.json(found);
}
});
});
// Set the app to listen on port 3000
app.listen(3000, function() {
console.log("App running on port 3000!");
});
How do you activate your runtime environment?
Where is uri/mongodb connect string defined.You must be having an env file or node.js,app js etc
Check those files
I have a server.js file, a node_modules folder, a package.json file, and a package-lock.json file. I activate my runtime environment by typing mongod and pressing enter, then opening another window and typing "node server.js.