From 694d9146581a6d4ba0bab9ca932884ee725a45c7 Mon Sep 17 00:00:00 2001 From: hasezoey Date: Sat, 17 Dec 2022 12:02:23 +0100 Subject: [PATCH] chore: replace many occurrences of "localhost" with "127.0.0.1" --- CONTRIBUTING.md | 2 +- README.md | 6 ++--- benchmarks/benchjs/delete.js | 4 ++-- benchmarks/benchjs/insert.js | 4 ++-- benchmarks/benchjs/multiop.js | 4 ++-- benchmarks/benchjs/population.js | 2 +- benchmarks/benchjs/read.js | 4 ++-- benchmarks/benchjs/update.js | 4 ++-- benchmarks/mem.js | 2 +- benchmarks/populate.js | 2 +- docs/connections.md | 16 ++++++------- docs/guide.md | 4 ++-- docs/migrating_to_5.md | 8 +++---- docs/migrating_to_6.md | 2 +- docs/models.md | 4 ++-- docs/populate.md | 4 ++-- docs/tutorials/ssl.md | 14 +++++------ docs/typescript.md | 2 +- docs/typescript/query-helpers.md | 4 ++-- examples/aggregate/aggregate.js | 2 +- examples/doc-methods.js | 4 ++-- examples/express/connection-sharing/README.md | 2 +- examples/express/connection-sharing/app.js | 4 ++-- examples/geospatial/geoJSONexample.js | 2 +- examples/geospatial/geospatial.js | 2 +- examples/globalschemas/gs_example.js | 2 +- examples/lean/lean.js | 2 +- examples/mapreduce/mapreduce.js | 2 +- .../population-across-three-collections.js | 2 +- examples/population/population-basic.js | 4 ++-- .../population/population-of-existing-doc.js | 4 ++-- .../population-of-multiple-existing-docs.js | 4 ++-- examples/population/population-options.js | 4 ++-- .../population/population-plain-objects.js | 4 ++-- examples/promises/promise.js | 2 +- examples/querybuilder/querybuilder.js | 2 +- examples/redis-todo/db/index.js | 2 +- examples/replicasets/replica-sets.js | 2 +- examples/statics/statics.js | 2 +- index.pug | 2 +- lib/connection.js | 24 +++++++++---------- lib/index.js | 16 ++++++------- scripts/static.js | 2 +- test/shard.test.js | 2 +- test/types/base.test.ts | 2 +- test/types/connect.test.ts | 12 +++++----- test/types/connection.test.ts | 24 +++++++++---------- tools/auth.js | 4 ++-- tools/sharded.js | 8 +++---- 49 files changed, 121 insertions(+), 121 deletions(-) diff --git a/CONTRIBUTING.md b/CONTRIBUTING.md index c3a57dbb27d..4b5246b66ce 100644 --- a/CONTRIBUTING.md +++ b/CONTRIBUTING.md @@ -58,7 +58,7 @@ If you'd like to preview your documentation changes, first commit your changes t * `npm install` * `npm run docs:view` -Visit `http://localhost:8089` and you should see the docs with your local changes. Make sure you `npm run docs:clean` before committing, because automated generated files to `docs/*` should **not** be in PRs. +Visit `http://127.0.0.1:8089` and you should see the docs with your local changes. Make sure you `npm run docs:clean` before committing, because automated generated files to `docs/*` should **not** be in PRs. #### Documentation Style Guidelines diff --git a/README.md b/README.md index cda03d3edbf..d62c95b6c60 100644 --- a/README.md +++ b/README.md @@ -66,7 +66,7 @@ const require = createRequire(import.meta.url); const mongoose = require('mongoose'); -mongoose.connect('mongodb://localhost:27017/test') +mongoose.connect('mongodb://127.0.0.1:27017/test') .then(() => console.log('Connected!')); ``` @@ -91,12 +91,12 @@ First, we need to define a connection. If your app uses only one database, you s Both `connect` and `createConnection` take a `mongodb://` URI, or the parameters `host, database, port, options`. ```js -await mongoose.connect('mongodb://localhost/my_database'); +await mongoose.connect('mongodb://127.0.0.1/my_database'); ``` Once connected, the `open` event is fired on the `Connection` instance. If you're using `mongoose.connect`, the `Connection` is `mongoose.connection`. Otherwise, `mongoose.createConnection` return value is a `Connection`. -**Note:** _If the local connection fails then try using 127.0.0.1 instead of localhost. Sometimes issues may arise when the local hostname has been changed._ +**Note:** _If the local connection fails then try using 127.0.0.1 instead of 127.0.0.1. Sometimes issues may arise when the local hostname has been changed._ **Important!** Mongoose buffers all the commands until it's connected to the database. This means that you don't have to wait until it connects to MongoDB in order to define models, run queries, etc. diff --git a/benchmarks/benchjs/delete.js b/benchmarks/benchjs/delete.js index 705c8091fe1..1d717f7f6e3 100644 --- a/benchmarks/benchjs/delete.js +++ b/benchmarks/benchjs/delete.js @@ -17,11 +17,11 @@ const mongoClient = require('mongodb').MongoClient; * These are all the benchmark tests for deleting data */ -mongoose.connect('mongodb://localhost/mongoose-bench', function (err) { +mongoose.connect('mongodb://127.0.0.1/mongoose-bench', function (err) { if (err) { throw err; } - mongoClient.connect('mongodb://localhost', function (err, client) { + mongoClient.connect('mongodb://127.0.0.1', function (err, client) { if (err) { throw err; } diff --git a/benchmarks/benchjs/insert.js b/benchmarks/benchjs/insert.js index 7b13f90dcde..cd01bfb6069 100644 --- a/benchmarks/benchjs/insert.js +++ b/benchmarks/benchjs/insert.js @@ -19,12 +19,12 @@ const ObjectId = Schema.Types.ObjectId; * These are all the benchmark tests for inserting data */ -mongoose.connect('mongodb://localhost/mongoose-bench', function (err) { +mongoose.connect('mongodb://127.0.0.1/mongoose-bench', function (err) { if (err) { throw err; } mongoClient.connect( - 'mongodb://localhost/mongoose-bench', + 'mongodb://127.0.0.1/mongoose-bench', function (err, client) { if (err) { throw err; diff --git a/benchmarks/benchjs/multiop.js b/benchmarks/benchjs/multiop.js index bfe540705fd..c3f73d1f730 100644 --- a/benchmarks/benchjs/multiop.js +++ b/benchmarks/benchjs/multiop.js @@ -19,11 +19,11 @@ const utils = require('../../lib/utils.js'); * These are all the benchmark tests for mixed data operations */ -mongoose.connect('mongodb://localhost/mongoose-bench', function (err) { +mongoose.connect('mongodb://127.0.0.1/mongoose-bench', function (err) { if (err) { throw err; } - mongoClient.connect('mongodb://localhost', function (err, client) { + mongoClient.connect('mongodb://127.0.0.1', function (err, client) { if (err) { throw err; } diff --git a/benchmarks/benchjs/population.js b/benchmarks/benchjs/population.js index 81b46ed2799..dc55ac1cae9 100644 --- a/benchmarks/benchjs/population.js +++ b/benchmarks/benchjs/population.js @@ -18,7 +18,7 @@ const utils = require('../../lib/utils.js'); * These are all the benchmark tests for population ops */ -mongoose.connect('mongodb://localhost/mongoose-bench', function (err) { +mongoose.connect('mongodb://127.0.0.1/mongoose-bench', function (err) { if (err) { throw err; } diff --git a/benchmarks/benchjs/read.js b/benchmarks/benchjs/read.js index e97e2fab842..35d39b90916 100644 --- a/benchmarks/benchjs/read.js +++ b/benchmarks/benchjs/read.js @@ -20,11 +20,11 @@ const utils = require('../../lib/utils.js'); * These are all the benchmark tests for reading data */ -mongoose.connect('mongodb://localhost/mongoose-bench', function (err) { +mongoose.connect('mongodb://127.0.0.1/mongoose-bench', function (err) { if (err) { throw err; } - mongoClient.connect('mongodb://localhost', function (err, client) { + mongoClient.connect('mongodb://127.0.0.1', function (err, client) { if (err) { throw err; } diff --git a/benchmarks/benchjs/update.js b/benchmarks/benchjs/update.js index cb745742a55..4447339cf65 100644 --- a/benchmarks/benchjs/update.js +++ b/benchmarks/benchjs/update.js @@ -20,11 +20,11 @@ const utils = require('../../lib/utils.js'); * These are all the benchmark tests for updating data */ -mongoose.connect('mongodb://localhost/mongoose-bench', function (err) { +mongoose.connect('mongodb://127.0.0.1/mongoose-bench', function (err) { if (err) { throw err; } - mongoClient.connect('mongodb://localhost', function (err, client) { + mongoClient.connect('mongodb://127.0.0.1', function (err, client) { if (err) { throw err; } diff --git a/benchmarks/mem.js b/benchmarks/mem.js index 1864562056d..c73acac33e4 100644 --- a/benchmarks/mem.js +++ b/benchmarks/mem.js @@ -4,7 +4,7 @@ const mongoose = require('../'); const Schema = mongoose.Schema; -mongoose.connect('mongodb://localhost/mongoose-bench'); +mongoose.connect('mongodb://127.0.0.1/mongoose-bench'); const DocSchema = new Schema({ title: String diff --git a/benchmarks/populate.js b/benchmarks/populate.js index a5f7476b04f..2be29e72e0e 100644 --- a/benchmarks/populate.js +++ b/benchmarks/populate.js @@ -18,7 +18,7 @@ const B = mongoose.model('B', Schema({ let start; let count = 0; -mongoose.connect('mongodb://localhost/mongoose-bench', function(err) { +mongoose.connect('mongodb://127.0.0.1/mongoose-bench', function(err) { if (err) { return done(err); } diff --git a/docs/connections.md b/docs/connections.md index a16b9f6161c..b65f6100e6f 100644 --- a/docs/connections.md +++ b/docs/connections.md @@ -3,7 +3,7 @@ You can connect to MongoDB with the `mongoose.connect()` method. ```javascript -mongoose.connect('mongodb://localhost:27017/myapp'); +mongoose.connect('mongodb://127.0.0.1:27017/myapp'); ``` This is the minimum needed to connect the `myapp` database running locally @@ -39,7 +39,7 @@ Mongoose lets you start using your models immediately, without waiting for mongoose to establish a connection to MongoDB. ```javascript -mongoose.connect('mongodb://localhost:27017/myapp'); +mongoose.connect('mongodb://127.0.0.1:27017/myapp'); const MyModel = mongoose.model('Test', new Schema({ name: String })); // Works MyModel.findOne(function(error, result) { /* ... */ }); @@ -56,7 +56,7 @@ const MyModel = mongoose.model('Test', new Schema({ name: String })); MyModel.findOne(function(error, result) { /* ... */ }); setTimeout(function() { - mongoose.connect('mongodb://localhost:27017/myapp'); + mongoose.connect('mongodb://127.0.0.1:27017/myapp'); }, 60000); ``` @@ -101,12 +101,12 @@ There are two classes of errors that can occur with a Mongoose connection. To handle initial connection errors, you should use `.catch()` or `try/catch` with async/await. ```javascript -mongoose.connect('mongodb://localhost:27017/test'). +mongoose.connect('mongodb://127.0.0.1:27017/test'). catch(error => handleError(error)); // Or: try { - await mongoose.connect('mongodb://localhost:27017/test'); + await mongoose.connect('mongodb://127.0.0.1:27017/test'); } catch (error) { handleError(error); } @@ -201,9 +201,9 @@ driver. You **can't** set Mongoose-specific options like `bufferCommands` in the query string. ```javascript -mongoose.connect('mongodb://localhost:27017/test?connectTimeoutMS=1000&bufferCommands=false&authSource=otherdb'); +mongoose.connect('mongodb://127.0.0.1:27017/test?connectTimeoutMS=1000&bufferCommands=false&authSource=otherdb'); // The above is equivalent to: -mongoose.connect('mongodb://localhost:27017/test', { +mongoose.connect('mongodb://127.0.0.1:27017/test', { connectTimeoutMS: 1000 // Note that mongoose will **not** pull `bufferCommands` from the query string }); @@ -453,7 +453,7 @@ using your connection options: mongoose.createConnection(uri, { maxPoolSize: 10 }); // With connection string options -const uri = 'mongodb://localhost:27017/test?maxPoolSize=10'; +const uri = 'mongodb://127.0.0.1:27017/test?maxPoolSize=10'; mongoose.createConnection(uri); ``` diff --git a/docs/guide.md b/docs/guide.md index c11bede3e23..09d57b34363 100644 --- a/docs/guide.md +++ b/docs/guide.md @@ -274,9 +274,9 @@ While nice for development, it is recommended this behavior be disabled in produ Disable the behavior by setting the `autoIndex` option of your schema to `false`, or globally on the connection by setting the option `autoIndex` to `false`. ```javascript -mongoose.connect('mongodb://user:pass@localhost:port/database', { autoIndex: false }); +mongoose.connect('mongodb://user:pass@127.0.0.1:port/database', { autoIndex: false }); // or -mongoose.createConnection('mongodb://user:pass@localhost:port/database', { autoIndex: false }); +mongoose.createConnection('mongodb://user:pass@127.0.0.1:port/database', { autoIndex: false }); // or mongoose.set('autoIndex', false); // or diff --git a/docs/migrating_to_5.md b/docs/migrating_to_5.md index fcf4c0080d0..c909efaefd7 100644 --- a/docs/migrating_to_5.md +++ b/docs/migrating_to_5.md @@ -70,10 +70,10 @@ MyModel.find().exec(function() { // Worked in mongoose 4. Does **not** work in mongoose 5, `mongoose.connect()` // now returns a promise consistently. This is to avoid the horrible things // we've done to allow mongoose to be a thenable that resolves to itself. -mongoose.connect('mongodb://localhost:27017/test').model('Test', new Schema({})); +mongoose.connect('mongodb://127.0.0.1:27017/test').model('Test', new Schema({})); // Do this instead -mongoose.connect('mongodb://localhost:27017/test'); +mongoose.connect('mongodb://127.0.0.1:27017/test'); mongoose.model('Test', new Schema({})); ``` @@ -87,8 +87,8 @@ no longer supports several function signatures for `mongoose.connect()` that worked in Mongoose 4.x if the `useMongoClient` option was off. Below are some examples of `mongoose.connect()` calls that do **not** work in Mongoose 5.x. -* `mongoose.connect('localhost', 27017);` -* `mongoose.connect('localhost', 'mydb', 27017);` +* `mongoose.connect('127.0.0.1', 27017);` +* `mongoose.connect('127.0.0.1', 'mydb', 27017);` * `mongoose.connect('mongodb://host1:27017,mongodb://host2:27017');` In Mongoose 5.x, the first parameter to `mongoose.connect()` and `mongoose.createConnection()`, if specified, **must** be a [MongoDB connection string](https://docs.mongodb.com/manual/reference/connection-string/). The diff --git a/docs/migrating_to_6.md b/docs/migrating_to_6.md index c964705dd86..5b9245d856c 100644 --- a/docs/migrating_to_6.md +++ b/docs/migrating_to_6.md @@ -90,7 +90,7 @@ res.deletedCount; // Number of documents that were deleted. Replaces `res.n` // No longer necessary: mongoose.set('useFindAndModify', false); -await mongoose.connect('mongodb://localhost:27017/test', { +await mongoose.connect('mongodb://127.0.0.1:27017/test', { useNewUrlParser: true, // <-- no longer necessary useUnifiedTopology: true // <-- no longer necessary }); diff --git a/docs/models.md b/docs/models.md index 8f9cc391822..d296684e0ef 100644 --- a/docs/models.md +++ b/docs/models.md @@ -64,13 +64,13 @@ uses is open. Every model has an associated connection. When you use `mongoose.model()`, your model will use the default mongoose connection. ```javascript -mongoose.connect('mongodb://localhost/gettingstarted'); +mongoose.connect('mongodb://127.0.0.1/gettingstarted'); ``` If you create a custom connection, use that connection's `model()` function instead. ```javascript -const connection = mongoose.createConnection('mongodb://localhost:27017/test'); +const connection = mongoose.createConnection('mongodb://127.0.0.1:27017/test'); const Tank = connection.model('Tank', yourSchema); ``` diff --git a/docs/populate.md b/docs/populate.md index 19e40db8bf2..05f6da8edcd 100644 --- a/docs/populate.md +++ b/docs/populate.md @@ -444,8 +444,8 @@ Let's say you have a schema representing events, and a schema representing conversations. Each event has a corresponding conversation thread. ```javascript -const db1 = mongoose.createConnection('mongodb://localhost:27000/db1'); -const db2 = mongoose.createConnection('mongodb://localhost:27001/db2'); +const db1 = mongoose.createConnection('mongodb://127.0.0.1:27000/db1'); +const db2 = mongoose.createConnection('mongodb://127.0.0.1:27001/db2'); const conversationSchema = new Schema({ numMessages: Number }); const Conversation = db2.model('Conversation', conversationSchema); diff --git a/docs/tutorials/ssl.md b/docs/tutorials/ssl.md index e6863640d9d..22aea4b2e99 100644 --- a/docs/tutorials/ssl.md +++ b/docs/tutorials/ssl.md @@ -3,10 +3,10 @@ Mongoose supports connecting to [MongoDB clusters that require SSL connections](https://docs.mongodb.com/manual/tutorial/configure-ssl/). Setting the `ssl` option to `true` in [`mongoose.connect()`](../api/mongoose.html#mongoose_Mongoose-connect) or your connection string is enough to connect to a MongoDB cluster using SSL: ```javascript -mongoose.connect('mongodb://localhost:27017/test', { ssl: true }); +mongoose.connect('mongodb://127.0.0.1:27017/test', { ssl: true }); // Equivalent: -mongoose.connect('mongodb://localhost:27017/test?ssl=true'); +mongoose.connect('mongodb://127.0.0.1:27017/test?ssl=true'); ``` The `ssl` option defaults to `false` for connection strings that start with `mongodb://`. However, @@ -27,7 +27,7 @@ By default, Mongoose validates the SSL certificate against a [certificate author to `false`. ```javascript -mongoose.connect('mongodb://localhost:27017/test', { +mongoose.connect('mongodb://127.0.0.1:27017/test', { ssl: true, sslValidate: false }); @@ -49,7 +49,7 @@ server is not registered with an established certificate authority. The solution [essentially sets a list of allowed SSL certificates](https://mongodb.github.io/node-mongodb-native/2.1/tutorials/connect/ssl/). ```javascript -await mongoose.connect('mongodb://localhost:27017/test', { +await mongoose.connect('mongodb://127.0.0.1:27017/test', { ssl: true, sslValidate: true, // For example, see https://medium.com/@rajanmaharjan/secure-your-mongodb-connections-ssl-tls-92e2addb3c89 @@ -76,7 +76,7 @@ If you're using [X509 authentication](https://www.mongodb.com/docs/drivers/node/ ```javascript // Do this: const username = 'myusername'; -await mongoose.connect(`mongodb://${encodeURIComponent(username)}@localhost:27017/test`, { +await mongoose.connect(`mongodb://${encodeURIComponent(username)}@127.0.0.1:27017/test`, { ssl: true, sslValidate: true, sslCA: `${__dirname}/rootCA.pem`, @@ -84,11 +84,11 @@ await mongoose.connect(`mongodb://${encodeURIComponent(username)}@localhost:2701 }); // Not this: -await mongoose.connect(`mongodb://localhost:27017/test`, { +await mongoose.connect(`mongodb://127.0.0.1:27017/test`, { ssl: true, sslValidate: true, sslCA: `${__dirname}/rootCA.pem`, authMechanism: 'MONGODB-X509'. auth: { username } }); -``` \ No newline at end of file +``` diff --git a/docs/typescript.md b/docs/typescript.md index 1f96873c7af..6a8eba3024f 100644 --- a/docs/typescript.md +++ b/docs/typescript.md @@ -37,7 +37,7 @@ run().catch(err => console.log(err)); async function run() { // 4. Connect to MongoDB - await connect('mongodb://localhost:27017/test'); + await connect('mongodb://127.0.0.1:27017/test'); const user = new User({ name: 'Bill', diff --git a/docs/typescript/query-helpers.md b/docs/typescript/query-helpers.md index de6bb1daf60..1afb2f8af0b 100644 --- a/docs/typescript/query-helpers.md +++ b/docs/typescript/query-helpers.md @@ -69,7 +69,7 @@ const ProjectModel = model('Project', schema); run().catch(err => console.log(err)); async function run(): Promise { - await connect('mongodb://localhost:27017/test'); + await connect('mongodb://127.0.0.1:27017/test'); // Equivalent to `ProjectModel.find({ stars: { $gt: 1000 }, name: 'mongoose' })` await ProjectModel.find().where('stars').gt(1000).byName('mongoose'); @@ -99,4 +99,4 @@ const ProjectModel = model('Project', ProjectSchema); // Equivalent to `ProjectModel.find({ stars: { $gt: 1000 }, name: 'mongoose' })` await ProjectModel.find().where('stars').gt(1000).byName('mongoose'); -``` \ No newline at end of file +``` diff --git a/examples/aggregate/aggregate.js b/examples/aggregate/aggregate.js index 59b315425f6..8a97e1e74d9 100644 --- a/examples/aggregate/aggregate.js +++ b/examples/aggregate/aggregate.js @@ -51,7 +51,7 @@ const data = [ ]; -mongoose.connect('mongodb://localhost/persons', function(err) { +mongoose.connect('mongodb://127.0.0.1/persons', function(err) { if (err) throw err; // create all of the dummy people diff --git a/examples/doc-methods.js b/examples/doc-methods.js index 52f4f6f2e49..d6b34599998 100644 --- a/examples/doc-methods.js +++ b/examples/doc-methods.js @@ -36,12 +36,12 @@ CharacterSchema.methods.attack = function() { const Character = mongoose.model('Character', CharacterSchema); /** - * Connect to the database on localhost with + * Connect to the database on 127.0.0.1 with * the default port (27017) */ const dbname = 'mongoose-example-doc-methods-' + ((Math.random() * 10000) | 0); -const uri = 'mongodb://localhost/' + dbname; +const uri = 'mongodb://127.0.0.1/' + dbname; console.log('connecting to %s', uri); diff --git a/examples/express/connection-sharing/README.md b/examples/express/connection-sharing/README.md index fc709a3ab70..9f732f0965e 100644 --- a/examples/express/connection-sharing/README.md +++ b/examples/express/connection-sharing/README.md @@ -3,4 +3,4 @@ To run: - Execute `npm install` from this directory - Execute `node app.js` -- Navigate to `localhost:8000` +- Navigate to `127.0.0.1:8000` diff --git a/examples/express/connection-sharing/app.js b/examples/express/connection-sharing/app.js index ddae7bd0be2..8c0efae338e 100644 --- a/examples/express/connection-sharing/app.js +++ b/examples/express/connection-sharing/app.js @@ -2,7 +2,7 @@ const express = require('express'); const mongoose = require('../../../lib'); -const uri = 'mongodb://localhost/mongoose-shared-connection'; +const uri = 'mongodb://127.0.0.1/mongoose-shared-connection'; global.db = mongoose.createConnection(uri); const routes = require('./routes'); @@ -12,4 +12,4 @@ app.get('/', routes.home); app.get('/insert', routes.insert); app.get('/name', routes.modelName); -app.listen(8000, () => console.log('listening on http://localhost:8000')); +app.listen(8000, () => console.log('listening on http://127.0.0.1:8000')); diff --git a/examples/geospatial/geoJSONexample.js b/examples/geospatial/geoJSONexample.js index 6c7507c0876..5f1fd2dbb87 100644 --- a/examples/geospatial/geoJSONexample.js +++ b/examples/geospatial/geoJSONexample.js @@ -21,7 +21,7 @@ const data = [ ]; -mongoose.connect('mongodb://localhost/locations', function(err) { +mongoose.connect('mongodb://127.0.0.1/locations', function(err) { if (err) { throw err; } diff --git a/examples/geospatial/geospatial.js b/examples/geospatial/geospatial.js index 4b5b8705b08..396394b91a8 100644 --- a/examples/geospatial/geospatial.js +++ b/examples/geospatial/geospatial.js @@ -55,7 +55,7 @@ const data = [ ]; -mongoose.connect('mongodb://localhost/persons', function(err) { +mongoose.connect('mongodb://127.0.0.1/persons', function(err) { if (err) { throw err; } diff --git a/examples/globalschemas/gs_example.js b/examples/globalschemas/gs_example.js index da1416f0d29..3b9a74f9dd5 100644 --- a/examples/globalschemas/gs_example.js +++ b/examples/globalschemas/gs_example.js @@ -10,7 +10,7 @@ const Person = mongoose.model('Person'); // connect to a server to do a quick write / read example -mongoose.connect('mongodb://localhost/persons', function(err) { +mongoose.connect('mongodb://127.0.0.1/persons', function(err) { if (err) { throw err; } diff --git a/examples/lean/lean.js b/examples/lean/lean.js index f817b1d4f86..95759a40a6b 100644 --- a/examples/lean/lean.js +++ b/examples/lean/lean.js @@ -51,7 +51,7 @@ const data = [ ]; -mongoose.connect('mongodb://localhost/persons', function(err) { +mongoose.connect('mongodb://127.0.0.1/persons', function(err) { if (err) throw err; // create all of the dummy people diff --git a/examples/mapreduce/mapreduce.js b/examples/mapreduce/mapreduce.js index 24c21e42eca..23bf8f4a60e 100644 --- a/examples/mapreduce/mapreduce.js +++ b/examples/mapreduce/mapreduce.js @@ -45,7 +45,7 @@ const data = [ ]; -mongoose.connect('mongodb://localhost/persons', function(err) { +mongoose.connect('mongodb://127.0.0.1/persons', function(err) { if (err) throw err; // create all of the dummy people diff --git a/examples/population/population-across-three-collections.js b/examples/population/population-across-three-collections.js index 61ab4275c64..e3ef031d9b9 100644 --- a/examples/population/population-across-three-collections.js +++ b/examples/population/population-across-three-collections.js @@ -10,7 +10,7 @@ const ObjectId = mongoose.Types.ObjectId; */ const dbname = 'testing_populateAdInfinitum_' + require('../../lib/utils').random(); -mongoose.connect('localhost', dbname); +mongoose.connect('127.0.0.1', dbname); mongoose.connection.on('error', function() { console.error('connection error', arguments); }); diff --git a/examples/population/population-basic.js b/examples/population/population-basic.js index 9be961713b9..a6c7ea88c7f 100644 --- a/examples/population/population-basic.js +++ b/examples/population/population-basic.js @@ -32,11 +32,11 @@ const gameSchema = Schema({ const Game = mongoose.model('Game', gameSchema); /** - * Connect to the console database on localhost with + * Connect to the console database on 127.0.0.1 with * the default port (27017) */ -mongoose.connect('mongodb://localhost/console', function(err) { +mongoose.connect('mongodb://127.0.0.1/console', function(err) { // if we failed to connect, abort if (err) throw err; diff --git a/examples/population/population-of-existing-doc.js b/examples/population/population-of-existing-doc.js index d008f4f313c..4223f3ae9e4 100644 --- a/examples/population/population-of-existing-doc.js +++ b/examples/population/population-of-existing-doc.js @@ -32,11 +32,11 @@ const gameSchema = Schema({ const Game = mongoose.model('Game', gameSchema); /** - * Connect to the console database on localhost with + * Connect to the console database on 127.0.0.1 with * the default port (27017) */ -mongoose.connect('mongodb://localhost/console', function(err) { +mongoose.connect('mongodb://127.0.0.1/console', function(err) { // if we failed to connect, abort if (err) throw err; diff --git a/examples/population/population-of-multiple-existing-docs.js b/examples/population/population-of-multiple-existing-docs.js index f070d6cccd8..310d0a40c05 100644 --- a/examples/population/population-of-multiple-existing-docs.js +++ b/examples/population/population-of-multiple-existing-docs.js @@ -32,11 +32,11 @@ const gameSchema = Schema({ const Game = mongoose.model('Game', gameSchema); /** - * Connect to the console database on localhost with + * Connect to the console database on 127.0.0.1 with * the default port (27017) */ -mongoose.connect('mongodb://localhost/console', function(err) { +mongoose.connect('mongodb://127.0.0.1/console', function(err) { // if we failed to connect, abort if (err) throw err; diff --git a/examples/population/population-options.js b/examples/population/population-options.js index 8bd18fa97a7..2e75556ddd4 100644 --- a/examples/population/population-options.js +++ b/examples/population/population-options.js @@ -32,11 +32,11 @@ const gameSchema = Schema({ const Game = mongoose.model('Game', gameSchema); /** - * Connect to the console database on localhost with + * Connect to the console database on 127.0.0.1 with * the default port (27017) */ -mongoose.connect('mongodb://localhost/console', function(err) { +mongoose.connect('mongodb://127.0.0.1/console', function(err) { // if we failed to connect, abort if (err) throw err; diff --git a/examples/population/population-plain-objects.js b/examples/population/population-plain-objects.js index e0fb91cc6fc..ed5abe03d1e 100644 --- a/examples/population/population-plain-objects.js +++ b/examples/population/population-plain-objects.js @@ -32,11 +32,11 @@ const gameSchema = Schema({ const Game = mongoose.model('Game', gameSchema); /** - * Connect to the console database on localhost with + * Connect to the console database on 127.0.0.1 with * the default port (27017) */ -mongoose.connect('mongodb://localhost/console', function(err) { +mongoose.connect('mongodb://127.0.0.1/console', function(err) { // if we failed to connect, abort if (err) throw err; diff --git a/examples/promises/promise.js b/examples/promises/promise.js index 45f474296a6..a0660c9a1a0 100644 --- a/examples/promises/promise.js +++ b/examples/promises/promise.js @@ -40,7 +40,7 @@ const data = [ ]; -mongoose.connect('mongodb://localhost/persons', function(err) { +mongoose.connect('mongodb://127.0.0.1/persons', function(err) { if (err) { throw err; } diff --git a/examples/querybuilder/querybuilder.js b/examples/querybuilder/querybuilder.js index 5561f9157dc..a05059c001c 100644 --- a/examples/querybuilder/querybuilder.js +++ b/examples/querybuilder/querybuilder.js @@ -41,7 +41,7 @@ const data = [ ]; -mongoose.connect('mongodb://localhost/persons', function(err) { +mongoose.connect('mongodb://127.0.0.1/persons', function(err) { if (err) throw err; // create all of the dummy people diff --git a/examples/redis-todo/db/index.js b/examples/redis-todo/db/index.js index 3305fe8a8d3..c12d30bd323 100644 --- a/examples/redis-todo/db/index.js +++ b/examples/redis-todo/db/index.js @@ -2,4 +2,4 @@ const mongoose = require('mongoose'); -mongoose.connect('mongodb://localhost/redis-todo'); +mongoose.connect('mongodb://127.0.0.1/redis-todo'); diff --git a/examples/replicasets/replica-sets.js b/examples/replicasets/replica-sets.js index 74217bf113e..cb9b91df7e8 100644 --- a/examples/replicasets/replica-sets.js +++ b/examples/replicasets/replica-sets.js @@ -46,7 +46,7 @@ const data = [ const opts = { replSet: { rs_name: 'rs0' } }; -mongoose.connect('mongodb://localhost:27018/persons,localhost:27019,localhost:27020', opts, function(err) { +mongoose.connect('mongodb://127.0.0.1:27018/persons,127.0.0.1:27019,127.0.0.1:27020', opts, function(err) { if (err) throw err; // create all of the dummy people diff --git a/examples/statics/statics.js b/examples/statics/statics.js index 460b194fa86..b1e5aabb867 100644 --- a/examples/statics/statics.js +++ b/examples/statics/statics.js @@ -12,7 +12,7 @@ const Person = mongoose.model('Person'); run().catch(console.error); async function run() { - await mongoose.connect('mongodb://localhost/persons'); + await mongoose.connect('mongodb://127.0.0.1/persons'); const bill = await Person.create({ name: 'bill', age: 25, diff --git a/index.pug b/index.pug index 7cac37cff09..c0788cbdc93 100644 --- a/index.pug +++ b/index.pug @@ -121,7 +121,7 @@ html(lang='en') :markdown ```javascript const mongoose = require('mongoose'); - mongoose.connect('mongodb://localhost:27017/test'); + mongoose.connect('mongodb://127.0.0.1:27017/test'); const Cat = mongoose.model('Cat', { name: String }); diff --git a/lib/connection.js b/lib/connection.js index 137f927d68e..9339f138a05 100644 --- a/lib/connection.js +++ b/lib/connection.js @@ -196,7 +196,7 @@ Connection.prototype.collections; * * #### Example: * - * mongoose.createConnection('mongodb://localhost:27017/mydb').name; // "mydb" + * mongoose.createConnection('mongodb://127.0.0.1:27017/mydb').name; // "mydb" * * @property name * @memberOf Connection @@ -253,7 +253,7 @@ Connection.prototype.id; * * #### Example: * - * const db = mongoose.createConnection('mongodb://localhost:27017/mydb'); + * const db = mongoose.createConnection('mongodb://127.0.0.1:27017/mydb'); * db.plugin(() => console.log('Applied')); * db.plugins.length; // 1 * @@ -277,7 +277,7 @@ Object.defineProperty(Connection.prototype, 'plugins', { * * #### Example: * - * mongoose.createConnection('mongodb://localhost:27017/mydb').host; // "localhost" + * mongoose.createConnection('mongodb://127.0.0.1:27017/mydb').host; // "127.0.0.1" * * @property host * @memberOf Connection @@ -297,7 +297,7 @@ Object.defineProperty(Connection.prototype, 'host', { * * #### Example: * - * mongoose.createConnection('mongodb://localhost:27017/mydb').port; // 27017 + * mongoose.createConnection('mongodb://127.0.0.1:27017/mydb').port; // 27017 * * @property port * @memberOf Connection @@ -316,7 +316,7 @@ Object.defineProperty(Connection.prototype, 'port', { * * #### Example: * - * mongoose.createConnection('mongodb://val:psw@localhost:27017/mydb').user; // "val" + * mongoose.createConnection('mongodb://val:psw@127.0.0.1:27017/mydb').user; // "val" * * @property user * @memberOf Connection @@ -335,7 +335,7 @@ Object.defineProperty(Connection.prototype, 'user', { * * #### Example: * - * mongoose.createConnection('mongodb://val:psw@localhost:27017/mydb').pass; // "psw" + * mongoose.createConnection('mongodb://val:psw@127.0.0.1:27017/mydb').pass; // "psw" * * @property pass * @memberOf Connection @@ -539,7 +539,7 @@ Connection.prototype.dropCollection = _wrapConnHelper(function dropCollection(co * * #### Example: * - * const conn = mongoose.createConnection('mongodb://localhost:27017/mydb'); + * const conn = mongoose.createConnection('mongodb://127.0.0.1:27017/mydb'); * // Deletes the entire 'mydb' database * await conn.dropDatabase(); * @@ -1109,7 +1109,7 @@ Connection.prototype.collection = function(name, options) { * * #### Example: * - * const db = mongoose.createConnection('mongodb://localhost:27017/mydb'); + * const db = mongoose.createConnection('mongodb://127.0.0.1:27017/mydb'); * db.plugin(() => console.log('Applied')); * db.plugins.length; // 1 * @@ -1367,7 +1367,7 @@ Connection.prototype.watch = function(pipeline, options) { * * #### Example: * - * const conn = await mongoose.createConnection('mongodb://localhost:27017/test'). + * const conn = await mongoose.createConnection('mongodb://127.0.0.1:27017/test'). * asPromise(); * conn.readyState; // 1, means Mongoose is connected * @@ -1440,7 +1440,7 @@ Connection.prototype.optionsProvideAuthenticationData = function(options) { * * #### Example: * - * const conn = await mongoose.createConnection('mongodb://localhost:27017/test'). + * const conn = await mongoose.createConnection('mongodb://127.0.0.1:27017/test'). * asPromise(); * * conn.getClient(); // MongoClient { ... } @@ -1460,7 +1460,7 @@ Connection.prototype.getClient = function getClient() { * * #### Example: * - * const client = await mongodb.MongoClient.connect('mongodb://localhost:27017/test'); + * const client = await mongodb.MongoClient.connect('mongodb://127.0.0.1:27017/test'); * * const conn = mongoose.createConnection().setClient(client); * @@ -1538,7 +1538,7 @@ Connection.prototype.syncIndexes = async function syncIndexes(options = {}) { * #### Example: * * // Connect to `initialdb` first - * const conn = await mongoose.createConnection('mongodb://localhost:27017/initialdb').asPromise(); + * const conn = await mongoose.createConnection('mongodb://127.0.0.1:27017/initialdb').asPromise(); * * // Creates an un-cached connection to `mydb` * const db = conn.useDb('mydb'); diff --git a/lib/index.js b/lib/index.js index ec2a5d733b1..5e13901b40d 100644 --- a/lib/index.js +++ b/lib/index.js @@ -309,22 +309,22 @@ Mongoose.prototype.get = Mongoose.prototype.set; * #### Example: * * // with mongodb:// URI - * db = mongoose.createConnection('mongodb://user:pass@localhost:port/database'); + * db = mongoose.createConnection('mongodb://user:pass@127.0.0.1:port/database'); * * // and options * const opts = { db: { native_parser: true }} - * db = mongoose.createConnection('mongodb://user:pass@localhost:port/database', opts); + * db = mongoose.createConnection('mongodb://user:pass@127.0.0.1:port/database', opts); * * // replica sets - * db = mongoose.createConnection('mongodb://user:pass@localhost:port,anotherhost:port,yetanother:port/database'); + * db = mongoose.createConnection('mongodb://user:pass@127.0.0.1:port,anotherhost:port,yetanother:port/database'); * * // and options * const opts = { replset: { strategy: 'ping', rs_name: 'testSet' }} - * db = mongoose.createConnection('mongodb://user:pass@localhost:port,anotherhost:port,yetanother:port/database', opts); + * db = mongoose.createConnection('mongodb://user:pass@127.0.0.1:port,anotherhost:port,yetanother:port/database', opts); * * // initialize now, connect later * db = mongoose.createConnection(); - * db.openUri('localhost', 'database', port, [opts]); + * db.openUri('127.0.0.1', 'database', port, [opts]); * * @param {String} uri mongodb URI to connect to * @param {Object} [options] passed down to the [MongoDB driver's `connect()` function](https://mongodb.github.io/node-mongodb-native/4.9/interfaces/MongoClientOptions.html), except for 4 mongoose-specific options explained below. @@ -367,10 +367,10 @@ Mongoose.prototype.createConnection = function(uri, options, callback) { * * #### Example: * - * mongoose.connect('mongodb://user:pass@localhost:port/database'); + * mongoose.connect('mongodb://user:pass@127.0.0.1:port/database'); * * // replica sets - * const uri = 'mongodb://user:pass@localhost:port,anotherhost:port,yetanother:port/mydatabase'; + * const uri = 'mongodb://user:pass@127.0.0.1:port,anotherhost:port,yetanother:port/mydatabase'; * mongoose.connect(uri); * * // with options @@ -786,7 +786,7 @@ Mongoose.prototype.__defineSetter__('connection', function(v) { * mongoose.connections.length; // 1, just the default connection * mongoose.connections[0] === mongoose.connection; // true * - * mongoose.createConnection('mongodb://localhost:27017/test'); + * mongoose.createConnection('mongodb://127.0.0.1:27017/test'); * mongoose.connections.length; // 2 * * @memberOf Mongoose diff --git a/scripts/static.js b/scripts/static.js index d2218ca2a31..6f2e0a8e8c7 100644 --- a/scripts/static.js +++ b/scripts/static.js @@ -17,7 +17,7 @@ async function main() { app.use('/', express.static(website.cwd)); app.listen(port, () => { - console.log(`now listening on http://localhost:${port}`); + console.log(`now listening on http://127.0.0.1:${port}`); }); } diff --git a/test/shard.test.js b/test/shard.test.js index e3934cc776d..d8f977af149 100644 --- a/test/shard.test.js +++ b/test/shard.test.js @@ -17,7 +17,7 @@ if (!uri) { redColorEscapeCharacter, 'You\'re not testing shards!', 'Please set the MONGOOSE_SHARD_TEST_URI env variable.', - 'e.g: `mongodb://localhost:27017/database', + 'e.g: `mongodb://127.0.0.1:27017/database', 'Sharding must already be enabled on your database', colorResetEscapeCharacter ].join('\n')); diff --git a/test/types/base.test.ts b/test/types/base.test.ts index bf4aa723d92..1dfaaa2ef70 100644 --- a/test/types/base.test.ts +++ b/test/types/base.test.ts @@ -30,7 +30,7 @@ function connectionStates() { m.STATES.connected; m.ConnectionStates.connected; - m.connect('mongodb://localhost:27017/test').then(() => { + m.connect('mongodb://127.0.0.1:27017/test').then(() => { console.log('Connected!'); }); diff --git a/test/types/connect.test.ts b/test/types/connect.test.ts index 5025406f3ed..012ea7137c2 100644 --- a/test/types/connect.test.ts +++ b/test/types/connect.test.ts @@ -2,17 +2,17 @@ import mongoose, { connect } from 'mongoose'; import { expectType } from 'tsd'; // Promise -expectType>(connect('mongodb://localhost:27017/test')); -expectType>(connect('mongodb://localhost:27017/test', {})); -expectType>(connect('mongodb://localhost:27017/test', { bufferCommands: true })); +expectType>(connect('mongodb://127.0.0.1:27017/test')); +expectType>(connect('mongodb://127.0.0.1:27017/test', {})); +expectType>(connect('mongodb://127.0.0.1:27017/test', { bufferCommands: true })); // Callback -expectType(connect('mongodb://localhost:27017/test', (err: Error | null) => { +expectType(connect('mongodb://127.0.0.1:27017/test', (err: Error | null) => { return; })); -expectType(connect('mongodb://localhost:27017/test', {}, (err: Error | null) => { +expectType(connect('mongodb://127.0.0.1:27017/test', {}, (err: Error | null) => { return; })); -expectType(connect('mongodb://localhost:27017/test', { bufferCommands: true }, (err: Error | null) => { +expectType(connect('mongodb://127.0.0.1:27017/test', { bufferCommands: true }, (err: Error | null) => { return; })); diff --git a/test/types/connection.test.ts b/test/types/connection.test.ts index 28c941c8786..0bd9e1636e0 100644 --- a/test/types/connection.test.ts +++ b/test/types/connection.test.ts @@ -4,18 +4,18 @@ import { expectAssignable, expectError, expectType } from 'tsd'; import { AutoTypedSchemaType, autoTypedSchema } from './schema.test'; expectType(createConnection()); -expectType(createConnection('mongodb://localhost:27017/test')); -expectType(createConnection('mongodb://localhost:27017/test', { appName: 'mongoose' })); -expectType(createConnection('mongodb://localhost:27017/test', { appName: 'mongoose' }, (err, res) => (expectType(res)))); +expectType(createConnection('mongodb://127.0.0.1:27017/test')); +expectType(createConnection('mongodb://127.0.0.1:27017/test', { appName: 'mongoose' })); +expectType(createConnection('mongodb://127.0.0.1:27017/test', { appName: 'mongoose' }, (err, res) => (expectType(res)))); const conn = createConnection(); expectAssignable>(conn.model('Test', new Schema<{ name: string }>({ name: { type: String } }))); expectType>(conn.model<{ name: string }>('Test', new Schema({ name: { type: String } }))); -expectType>(conn.openUri('mongodb://localhost:27017/test')); -expectType>(conn.openUri('mongodb://localhost:27017/test', { bufferCommands: true })); -expectType(conn.openUri('mongodb://localhost:27017/test', { bufferCommands: true }, (err, value) => { +expectType>(conn.openUri('mongodb://127.0.0.1:27017/test')); +expectType>(conn.openUri('mongodb://127.0.0.1:27017/test', { bufferCommands: true })); +expectType(conn.openUri('mongodb://127.0.0.1:27017/test', { bufferCommands: true }, (err, value) => { expectType(value); })); @@ -43,22 +43,22 @@ expectType(conn.deleteModel(/.+/)); expectType>(conn.modelNames()); -expectType>(createConnection('mongodb://localhost:27017/test').close()); -expectType>(createConnection('mongodb://localhost:27017/test').close(true)); -expectType(createConnection('mongodb://localhost:27017/test').close(() => { +expectType>(createConnection('mongodb://127.0.0.1:27017/test').close()); +expectType>(createConnection('mongodb://127.0.0.1:27017/test').close(true)); +expectType(createConnection('mongodb://127.0.0.1:27017/test').close(() => { // do nothing. })); -expectType(createConnection('mongodb://localhost:27017/test').close(true, () => { +expectType(createConnection('mongodb://127.0.0.1:27017/test').close(true, () => { // do nothing. })); -expectType(createConnection('mongodb://localhost:27017/test').close(false, () => { +expectType(createConnection('mongodb://127.0.0.1:27017/test').close(false, () => { // do nothing. })); expectType(conn.db); expectType(conn.getClient()); -expectType(conn.setClient(new mongodb.MongoClient('mongodb://localhost:27017/test'))); +expectType(conn.setClient(new mongodb.MongoClient('mongodb://127.0.0.1:27017/test'))); expectType>(conn.transaction(async(res) => { expectType(res); diff --git a/tools/auth.js b/tools/auth.js index 2fddafaf7b2..22730c53233 100644 --- a/tools/auth.js +++ b/tools/auth.js @@ -21,11 +21,11 @@ async function run() { // Start process await server.start(); - const db = await mongodb.MongoClient.connect('mongodb://localhost:27017/admin'); + const db = await mongodb.MongoClient.connect('mongodb://127.0.0.1:27017/admin'); await db.addUser('passwordIsTaco', 'taco', { roles: ['dbOwner'] }); console.log('done'); -} \ No newline at end of file +} diff --git a/tools/sharded.js b/tools/sharded.js index 39aeed3d571..96ad95c6ced 100644 --- a/tools/sharded.js +++ b/tools/sharded.js @@ -17,18 +17,18 @@ async function run() { await topology.addShard([{ options: { - bind_ip: 'localhost', port: 31000, dbpath: '/data/db/31000', shardsvr: null + bind_ip: '127.0.0.1', port: 31000, dbpath: '/data/db/31000', shardsvr: null } }], { replSet: 'rs1' }); await topology.addConfigurationServers([{ options: { - bind_ip: 'localhost', port: 35000, dbpath: '/data/db/35000' + bind_ip: '127.0.0.1', port: 35000, dbpath: '/data/db/35000' } }], { replSet: 'rs0' }); await topology.addProxies([{ - bind_ip: 'localhost', port: 51000, configdb: 'localhost:35000' + bind_ip: '127.0.0.1', port: 51000, configdb: '127.0.0.1:35000' }], { binary: 'mongos' }); @@ -43,4 +43,4 @@ async function run() { await topology.enableSharding('test'); console.log('done'); -} \ No newline at end of file +}