• 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)
        })
    }
    

      

     
     
     
     
     
     
     
     
    
    
    
  • 相关阅读:
    javaWeb快速入门+——初体验-HelloWorld
    简单理解Linux系统的挂载是什么鬼
    STM32MP157 Cortex®-M4高性能系列MCU
    常见六种锂电池特性及参数对比
    IIC通信详解
    stm32微秒延时问题
    STM32 HAL库实现微秒级别延时
    开关电源波纹的产生、测量及抑制
    图解DIY 1pA超微电流测试器
    stm32
  • 原文地址:https://www.cnblogs.com/rsapaper/p/9580051.html
Copyright © 2020-2023  润新知