Skip to content

Commit 3d54771

Browse files
committed
Force IPv4 address usage when connecting to MongoDB Atlas in parts 3 and 4
1 parent 5924c9a commit 3d54771

File tree

4 files changed

+30
-14
lines changed

4 files changed

+30
-14
lines changed

src/content/3/en/part3c.md

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -137,7 +137,7 @@ const url = `mongodb+srv://fullstack:${password}@cluster0.a5qfl.mongodb.net/?ret
137137

138138
mongoose.set('strictQuery',false)
139139

140-
mongoose.connect(url)
140+
mongoose.connect(url, { family: 4 })
141141

142142
const noteSchema = new mongoose.Schema({
143143
content: String,
@@ -159,7 +159,15 @@ note.save().then(result => {
159159

160160
**NB:** Depending on which region you selected when building your cluster, the <i>MongoDB URI</i> may be different from the example provided above. You should verify and use the correct URI that was generated from MongoDB Atlas.
161161

162-
The code also assumes that it will be passed the password from the credentials we created in MongoDB Atlas, as a command line parameter. We can access the command line parameter like this:
162+
The connection to the database is established with the command:
163+
164+
```js
165+
mongoose.connect(url, { family: 4 })
166+
```
167+
168+
The method takes the database URL as the first argument and an object that defines the required settings as the second argument. MongoDB Atlas supports only IPv4 addresses, so with the object _{ family: 4 }_ we specify that the connection should always use IPv4.
169+
170+
The practice application assumes that it will be passed the password from the credentials we created in MongoDB Atlas, as a command line parameter. We can access the command line parameter like this:
163171

164172
```js
165173
const password = process.argv[2]
@@ -363,7 +371,7 @@ const password = process.argv[2]
363371
const url = `mongodb+srv://fullstack:${password}@cluster0.a5qfl.mongodb.net/noteApp?retryWrites=true&w=majority&appName=Cluster0`
364372

365373
mongoose.set('strictQuery',false)
366-
mongoose.connect(url)
374+
mongoose.connect(url, { family: 4 })
367375

368376
const noteSchema = new mongoose.Schema({
369377
content: String,
@@ -429,7 +437,7 @@ mongoose.set('strictQuery', false)
429437
const url = process.env.MONGODB_URI // highlight-line
430438

431439
console.log('connecting to', url)
432-
mongoose.connect(url)
440+
mongoose.connect(url, { family: 4 })
433441
// highlight-start
434442
.then(result => {
435443
console.log('connected to MongoDB')
@@ -472,7 +480,7 @@ We will soon learn a more sophisticated way to define environment variables.
472480
The way that the connection is made has changed slightly:
473481

474482
```js
475-
mongoose.connect(url)
483+
mongoose.connect(url, { family: 4 })
476484
.then(result => {
477485
console.log('connected to MongoDB')
478486
})

src/content/3/fi/osa3c.md

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -134,7 +134,7 @@ const password = process.argv[2]
134134
const url = `mongodb+srv://fullstack:${password}@cluster0.a5qfl.mongodb.net/?retryWrites=true&w=majority&appName=Cluster0`
135135

136136
mongoose.set('strictQuery', false)
137-
mongoose.connect(url)
137+
mongoose.connect(url, { family: 4 })
138138

139139
const noteSchema = new mongoose.Schema({
140140
content: String,
@@ -154,7 +154,15 @@ note.save().then(result => {
154154
})
155155
```
156156

157-
Koodi siis olettaa, että sille annetaan parametrina MongoDB Atlasissa luodulle käyttäjälle määritelty salasana. Komentoriviparametriin se pääsee käsiksi seuraavasti:
157+
Tietokantaan muodostetaan yhteys komennolla:
158+
159+
```js
160+
mongoose.connect(url, { family: 4 })
161+
```
162+
163+
Metodille annetaan ensimmäisenä argumenttina tietokannan URL-osoite ja toisena argumenttina olio, joka määrittelee tarvittavat asetukset. MongoDB Atlas tukee vain IPv4-osoitteita, joten määrittelemme oliolla _{ family: 4 }_, että yhteyteen käytetään aina IPv4-osoitetta.
164+
165+
Kokeilusovellus olettaa, että sille annetaan parametrina MongoDB Atlasissa luodulle käyttäjälle määritelty salasana. Komentoriviparametriin se pääsee käsiksi seuraavasti:
158166

159167
```js
160168
const password = process.argv[2]
@@ -357,7 +365,7 @@ const password = process.argv[2]
357365
const url = `mongodb+srv://fullstack:${password}@cluster0.a5qfl.mongodb.net/noteApp?retryWrites=true&w=majority&appName=Cluster0`
358366

359367
mongoose.set('strictQuery',false)
360-
mongoose.connect(url)
368+
mongoose.connect(url, { family: 4 })
361369

362370
const noteSchema = new mongoose.Schema({
363371
content: String,
@@ -423,7 +431,7 @@ mongoose.set('strictQuery', false)
423431
const url = process.env.MONGODB_URI // highlight-line
424432

425433
console.log('connecting to', url)
426-
mongoose.connect(url)
434+
mongoose.connect(url, { family: 4 })
427435
// highlight-start
428436
.then(result => {
429437
console.log('connected to MongoDB')
@@ -466,7 +474,7 @@ Opettelemme pian kehittyneemmän tavan määritellä ympäristömuuttujia.
466474
Yhteyden muodostustavassa on pieni muutos aiempaan:
467475

468476
```js
469-
mongoose.connect(url)
477+
mongoose.connect(url, { family: 4 })
470478
.then(result => {
471479
console.log('connected to MongoDB')
472480
})

src/content/4/en/part4a.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -202,7 +202,7 @@ const app = express()
202202
logger.info('connecting to', config.MONGODB_URI)
203203

204204
mongoose
205-
.connect(config.MONGODB_URI)
205+
.connect(config.MONGODB_URI, { family: 4 })
206206
.then(() => {
207207
logger.info('connected to MongoDB')
208208
})
@@ -424,7 +424,7 @@ const blogSchema = mongoose.Schema({
424424
const Blog = mongoose.model('Blog', blogSchema)
425425

426426
const mongoUrl = 'mongodb://localhost/bloglist'
427-
mongoose.connect(mongoUrl)
427+
mongoose.connect(mongoUrl, { family: 4 })
428428

429429
app.use(express.json())
430430

src/content/4/fi/osa4a.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -198,7 +198,7 @@ const app = express()
198198
logger.info('connecting to', config.MONGODB_URI)
199199

200200
mongoose
201-
.connect(config.MONGODB_URI)
201+
.connect(config.MONGODB_URI, { family: 4 })
202202
.then(() => {
203203
logger.info('connected to MongoDB')
204204
})
@@ -415,7 +415,7 @@ const blogSchema = mongoose.Schema({
415415
const Blog = mongoose.model('Blog', blogSchema)
416416

417417
const mongoUrl = 'mongodb://localhost/bloglist'
418-
mongoose.connect(mongoUrl)
418+
mongoose.connect(mongoUrl, { family: 4 })
419419

420420
app.use(express.json())
421421

0 commit comments

Comments
 (0)