Skip to content

Instantly share code, notes, and snippets.

@Niffy
Last active February 24, 2020 14:46
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Niffy/d07e33e1301dd793996ba28df8b0182b to your computer and use it in GitHub Desktop.
Save Niffy/d07e33e1301dd793996ba28df8b0182b to your computer and use it in GitHub Desktop.
Mongodb useUnifiedTopology error
// mongodb driver 3.5.3
// mongodb instance 3.6.15 and 4.2.0
const MongoClient = require('mongodb').MongoClient
const Work = class Work {
constructor(db_instance) {
this.db_instance = db_instance
this.collection = 'data'
}
async run (index) {
console.info('working', index)
let opts = {
readPreference: 'secondary'
}
let q = {
name: 'potato adams',
foo: 'bar'
}
let collection = this.db_instance.collection(this.collection)
await collection.find(q, opts).toArray()
console.info('finished', index)
}
}
const DB = class DB {
constructor () {
this.url = 'mongodb://localhost:27017,localhost:27018,localhost:27019/some-db?replicaSet=rs0'
this.db_name = 'some-db'
}
connect() {
return new Promise((resolve, reject) => {
const ops = {
useNewUrlParser: true,
poolSize: 25,
useUnifiedTopology: true
}
console.log('Connecting to database')
this.client = new MongoClient(this.url, ops)
this.client.connect((err) => {
if (err) {
console.error('err')
return reject(err)
}
this.db = this.client.db(this.db_name)
return resolve()
})
})
}
}
const Processor = class Processor {
constructor() {
this.db_instance = new DB()
}
async run( ) {
try {
await this.db_instance.connect()
let prom = []
let count = 20
for (let index = 0; index < count; index++) {
let w = new Work(this.db_instance.db)
prom.push(w.run(index))
}
await Promise.all(prom)
} catch (ex) {
throw ex
}
}
}
let processor = new Processor()
processor.run()
.then(() => {
console.log('finished')
process.exit(0)
})
.catch((err) => {
console.error('err', err)
process.exit(1)
})
Connecting to database
working 0
working 1
working 2
working 3
working 4
working 5
working 6
working 7
working 8
working 9
working 10
working 11
working 12
working 13
working 14
working 15
working 16
working 17
working 18
working 19
err { MongoServerSelectionError: Server selection timed out after 30000 ms
at Timeout.waitQueueMember.timer.setTimeout [as _onTimeout] (/Users/paulrobinson/workspace/akero/investigate/mongoose/native_secondary/node_modules/mongodb/lib/core/sdam/topology.js:428:30)
at ontimeout (timers.js:436:11)
at tryOnTimeout (timers.js:300:5)
at listOnTimeout (timers.js:263:5)
at Timer.processTimers (timers.js:223:10)
name: 'MongoServerSelectionError',
reason:
TopologyDescription {
type: 'ReplicaSetWithPrimary',
setName: 'rs0',
maxSetVersion: 3,
maxElectionId: null,
servers:
Map {
'localhost:27017' => [ServerDescription],
'localhost:27018' => [ServerDescription],
'localhost:27016' => [ServerDescription] },
stale: false,
compatible: true,
compatibilityError: null,
logicalSessionTimeoutMinutes: 30,
heartbeatFrequencyMS: 10000,
localThresholdMS: 15,
commonWireVersion: 6 },
[Symbol(mongoErrorContextSymbol)]: {} }
Connecting to database
working 0
working 1
working 2
working 3
working 4
working 5
working 6
working 7
working 8
working 9
working 10
working 11
working 12
working 13
working 14
working 15
working 16
working 17
working 18
working 19
finished 7
finished 1
finished 10
finished 2
finished 6
finished 9
finished 8
finished 5
finished 4
finished 3
err { MongoServerSelectionError: Server selection timed out after 30000 ms
at Timeout.waitQueueMember.timer.setTimeout [as _onTimeout] (/Users/paulrobinson/workspace/akero/investigate/mongoose/native_secondary/node_modules/mongodb/lib/core/sdam/topology.js:428:30)
at ontimeout (timers.js:436:11)
at tryOnTimeout (timers.js:300:5)
at listOnTimeout (timers.js:263:5)
at Timer.processTimers (timers.js:223:10)
name: 'MongoServerSelectionError',
reason:
TopologyDescription {
type: 'ReplicaSetWithPrimary',
setName: 'rs0',
maxSetVersion: 3,
maxElectionId: null,
servers:
Map {
'localhost:27017' => [ServerDescription],
'localhost:27018' => [ServerDescription],
'localhost:27016' => [ServerDescription] },
stale: false,
compatible: true,
compatibilityError: null,
logicalSessionTimeoutMinutes: 30,
heartbeatFrequencyMS: 10000,
localThresholdMS: 15,
commonWireVersion: 6 },
[Symbol(mongoErrorContextSymbol)]: {} }
Connecting to database
(node:31692) DeprecationWarning: current Server Discovery and Monitoring engine is deprecated, and will be removed in a future version. To use the new Server Discover and Monitoring engine, pass option { useUnifiedTopology: true } to the MongoClient constructor.
working 0
working 1
working 2
working 3
working 4
working 5
working 6
working 7
working 8
working 9
working 10
working 11
working 12
working 13
working 14
working 15
working 16
working 17
working 18
working 19
finished 11
finished 17
finished 1
finished 3
finished 13
finished 19
finished 5
finished 9
finished 15
finished 7
finished 12
finished 18
finished 8
finished 2
finished 0
finished 6
finished 4
finished 14
finished 10
finished 16
finished
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment