zoukankan      html  css  js  c++  java
  • 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)
        })
    }
    

      

     
     
     
     
     
     
     
     
    
    
    
  • 相关阅读:
    测试篇 尝了一下net5.0桌面开发
    日志篇 vs的文本替换,剔除引号保留数字 将vs2019更新之后无法用ctrl+d
    测试篇 使用 nuget.exe CLI 创建 nuget 包
    日志篇 博客园的下方的女孩透明的,带点击声音的
    测试篇 c#文件类型关联启动程序
    数学篇 求两条直线的交点,说明过程.
    日志篇 原生git笔记
    测试篇 winform Anchor 怎么临时取消关联,窗口边界和控件关联
    cad.net dll动态加载,插件式架构,在dll查找引用了的dll,查找dll依赖,dll热插拔,加载dll运行出错.
    cad.net 图元反应器,图元事件,要加在提交数据库之后哟
  • 原文地址:https://www.cnblogs.com/rsapaper/p/9580051.html
Copyright © 2011-2022 走看看