mongo 数据库提前关闭 避免读写任务没有结束,异步任务没有完成,同步指令提前关闭数据库:'MongoError: server instance pool was destroyed'
mongo 数据库提前关闭
// mongodb - npm https://www.npmjs.com/package/mongodb const mongoCfg = { uri: 'mongodb://hbaseU:123@192.168.3.103:27017/hbase', dbName: 'hbase', collectionName: 'todayUrlsTmpURL' } const MongoClient = require('mongodb').MongoClient // npm install assert const assert = require('assert') // Use connect method to connect to the server MongoClient.connect(mongoCfg.uri, function (err, client) { assert.equal(null, err) console.log('Connected successfully to server') const db = client.db(mongoCfg.dbName) const collectionName = mongoCfg.collectionName findDocuments(db, collectionName, {}, function (docs) { for (let i in docs) { const ii = docs[i] let mgid = ii._id let filter = { _id: mgid } console.log(filter) let val = { $set: { url: 's' } } updateDocument(db, collectionName, filter, val, function (result) { console.log(result) }) } }) client.close() }) const findDocuments = function (db, collectionName, filter, callback) { // Get the documents collection const collection = db.collection(collectionName) // Find some documents collection.find(filter).toArray(function (err, docs) { assert.equal(err, null) console.log("Found the following records") console.log(docs.length) callback(docs) }) } const updateDocument = function (db, collectionName, filter, val, callback) { // Get the documents collection const collection = db.collection(collectionName) collection.updateOne(filter, val, function (err, result) { console.log(err) assert.equal(err, null) assert.equal(1, result.result.n) callback(result) }) }
// mongodb - npm https://www.npmjs.com/package/mongodb
const mongoCfg = {
uri: 'mongodb://hbaseU:123@192.168.3.103:27017/hbase',
dbName: 'hbase',
collectionName: 'todayUrlsTmpURL'
}
const MongoClient = require('mongodb').MongoClient
// npm install assert
const assert = require('assert')
// Use connect method to connect to the server
MongoClient.connect(mongoCfg.uri, function (err, client) {
assert.equal(null, err)
console.log('Connected successfully to server')
const db = client.db(mongoCfg.dbName)
const collectionName = mongoCfg.collectionName
findDocuments(db, collectionName, {}, function (docs) {
for (let i in docs) {
const ii = docs[i]
let mgid = ii._id
let filter = {
_id: mgid
}
console.log(filter)
let val = {
$set: {
url: 'ok-' + mgid
}
}
updateDocument(db, collectionName, filter, val, function (result) {
console.log(result)
})
}
// 在回调中关闭数据库
// 保证读写完全结束后关闭数据库
// 以与之(读写完全结束)同步的方式关闭数据库
client.close()
})
// 避免读写任务没有结束,异步任务没有完成,同步指令提前关闭数据库:'MongoError: server instance pool was destroyed'
// client.close()
})
const findDocuments = function (db, collectionName, filter, callback) {
// Get the documents collection
const collection = db.collection(collectionName)
// Find some documents
collection.find(filter).toArray(function (err, docs) {
assert.equal(err, null)
console.log("Found the following records")
console.log(docs.length)
callback(docs)
})
}
const updateDocument = function (db, collectionName, filter, val, callback) {
// Get the documents collection
const collection = db.collection(collectionName)
collection.updateOne(filter, val, function (err, result) {
console.log(err)
assert.equal(err, null)
assert.equal(1, result.result.n)
callback(result)
})
}
// mongodb - npm https://www.npmjs.com/package/mongodb const mongoCfg = { uri: 'mongodb://hbaseU:123@192.168.3.103:27017/hbase', dbName: 'hbase', collectionName: 'todayUrlsTmpURL' } const MongoClient = require('mongodb').MongoClient // npm install assert const assert = require('assert') // Use connect method to connect to the server MongoClient.connect(mongoCfg.uri, function (err, client) { assert.equal(null, err) console.log('Connected successfully to server') const db = client.db(mongoCfg.dbName) const collectionName = mongoCfg.collectionName findDocuments(db, collectionName, {}, function (docs) { for (let i in docs) { const ii = docs[i] let mgid = ii._id let filter = { _id: mgid } console.log(filter) let val = { $set: { url: 'ok-' + mgid } } updateDocument(db, collectionName, filter, val, function (result) { console.log(result) }) } // 在回调中关闭数据库 // 保证读写完全结束后关闭数据库 // 以与之(读写完全结束)同步的方式关闭数据库 client.close() }) // 避免读写任务没有结束,异步任务没有完成,同步指令提前关闭数据库:'MongoError: server instance pool was destroyed' // client.close() }) const findDocuments = function (db, collectionName, filter, callback) { // Get the documents collection const collection = db.collection(collectionName) // Find some documents collection.find(filter).toArray(function (err, docs) { assert.equal(err, null) console.log("Found the following records") console.log(docs.length) callback(docs) }) } const updateDocument = function (db, collectionName, filter, val, callback) { // Get the documents collection const collection = db.collection(collectionName) collection.updateOne(filter, val, function (err, result) { console.log(err) assert.equal(err, null) assert.equal(1, result.result.n) callback(result) }) }