mongo 数据库提前关闭

  1. // mongodb - npm https://www.npmjs.com/package/mongodb
  2. const mongoCfg = {
  3. uri: 'mongodb://hbaseU:123@192.168.3.103:27017/hbase',
  4. dbName: 'hbase',
  5. collectionName: 'todayUrlsTmpURL'
  6. }
  7.  
  8. const MongoClient = require('mongodb').MongoClient
  9. // npm install assert
  10. const assert = require('assert')
  11. // Use connect method to connect to the server
  12.  
  13. MongoClient.connect(mongoCfg.uri, function (err, client) {
  14. assert.equal(null, err)
  15. console.log('Connected successfully to server')
  16. const db = client.db(mongoCfg.dbName)
  17. const collectionName = mongoCfg.collectionName
  18. findDocuments(db, collectionName, {}, function (docs) {
  19. for (let i in docs) {
  20. const ii = docs[i]
  21. let mgid = ii._id
  22. let filter = {
  23. _id: mgid
  24. }
  25. console.log(filter)
  26. let val = {
  27. $set: {
  28. url: 's'
  29. }
  30. }
  31. updateDocument(db, collectionName, filter, val, function (result) {
  32. console.log(result)
  33. })
  34. }
  35. })
  36. client.close()
  37. })
  38.  
  39. const findDocuments = function (db, collectionName, filter, callback) {
  40. // Get the documents collection
  41. const collection = db.collection(collectionName)
  42. // Find some documents
  43. collection.find(filter).toArray(function (err, docs) {
  44. assert.equal(err, null)
  45. console.log("Found the following records")
  46. console.log(docs.length)
  47. callback(docs)
  48. })
  49. }
  50.  
  51. const updateDocument = function (db, collectionName, filter, val, callback) {
  52. // Get the documents collection
  53. const collection = db.collection(collectionName)
  54. collection.updateOne(filter, val, function (err, result) {
  55. console.log(err)
  56. assert.equal(err, null)
  57. assert.equal(1, result.result.n)
  58. callback(result)
  59. })
  60. }
// 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)
})
}
 
  1. // mongodb - npm https://www.npmjs.com/package/mongodb
  2. const mongoCfg = {
  3. uri: 'mongodb://hbaseU:123@192.168.3.103:27017/hbase',
  4. dbName: 'hbase',
  5. collectionName: 'todayUrlsTmpURL'
  6. }
  7.  
  8. const MongoClient = require('mongodb').MongoClient
  9. // npm install assert
  10. const assert = require('assert')
  11. // Use connect method to connect to the server
  12. MongoClient.connect(mongoCfg.uri, function (err, client) {
  13. assert.equal(null, err)
  14. console.log('Connected successfully to server')
  15. const db = client.db(mongoCfg.dbName)
  16. const collectionName = mongoCfg.collectionName
  17. findDocuments(db, collectionName, {}, function (docs) {
  18. for (let i in docs) {
  19. const ii = docs[i]
  20. let mgid = ii._id
  21. let filter = {
  22. _id: mgid
  23. }
  24. console.log(filter)
  25. let val = {
  26. $set: {
  27. url: 'ok-' + mgid
  28. }
  29. }
  30. updateDocument(db, collectionName, filter, val, function (result) {
  31. console.log(result)
  32. })
  33. }
  34. // 在回调中关闭数据库
  35. // 保证读写完全结束后关闭数据库
  36. // 以与之(读写完全结束)同步的方式关闭数据库
  37. client.close()
  38. })
  39. // 避免读写任务没有结束,异步任务没有完成,同步指令提前关闭数据库:'MongoError: server instance pool was destroyed'
  40. // client.close()
  41. })
  42.  
  43. const findDocuments = function (db, collectionName, filter, callback) {
  44. // Get the documents collection
  45. const collection = db.collection(collectionName)
  46. // Find some documents
  47. collection.find(filter).toArray(function (err, docs) {
  48. assert.equal(err, null)
  49. console.log("Found the following records")
  50. console.log(docs.length)
  51. callback(docs)
  52. })
  53. }
  54.  
  55. const updateDocument = function (db, collectionName, filter, val, callback) {
  56. // Get the documents collection
  57. const collection = db.collection(collectionName)
  58. collection.updateOne(filter, val, function (err, result) {
  59. console.log(err)
  60. assert.equal(err, null)
  61. assert.equal(1, result.result.n)
  62. callback(result)
  63. })
  64. }

  

 
 
 
 
 
 
 
 
  1.  

mongo 数据库提前关闭 避免读写任务没有结束,异步任务没有完成,同步指令提前关闭数据库:'MongoError: server instance pool was destroyed'的更多相关文章

  1. 转 查看磁盘IO负载 - 看哪些进程在读写磁盘 以及oracle 异步I/O 和同步I/O

    https://www.cnblogs.com/cloudstorage/archive/2012/11/11/2764623.html #####sample 1: Oracle等待事件db fil ...

  2. spring Batch实现数据库大数据量读写

    spring Batch实现数据库大数据量读写 博客分类: spring springBatchquartz定时调度批处理  1. data-source-context.xml <?xml v ...

  3. MySQL/MariaDB数据库的PROXY实现读写分离

    MySQL/MariaDB数据库的PROXY实现读写分离 作者:尹正杰  版权声明:原创作品,谢绝转载!否则将追究法律责任. 一.ProxySQL概述 1>.各家互联网公司读写分离的解决方案 m ...

  4. [转]Visual Studio 2008中如何比较二个数据库的架构【Schema】和数据【Data】并同步

    使用场景: 在团队开发中,每一个人都有可能随时更新数据库,这时候数据库中数据和架构等信息都会发生变化.如果更新不及时,就会发生数据错误或数据丢失的风险,影响团队的开发效率和 项目进度,这时候我们该怎么 ...

  5. Navicat 连接远程数据库报错:1130 - Host "XX.XX.XX.XX" is not allowed to connect to this MySQL server

    Navicat 连接远程数据库报错:1130 - Host "XX.XX.XX.XX" is not allowed to connect to this MySQL server ...

  6. Capistrano:自动完成多台服务器上新版本的同步更新,包括数据库的改变

    https://baike.baidu.com/item/Capistrano/6844928?fr=aladdin   Capistrano是一种在多台服务器上运行脚本的开源工具,它主要用于部署we ...

  7. 如果数据库上的row格式是mixed或者mixed的格式,如何对比两台数据库服务器上的数据是否一致呢

    如果数据库上的row格式是mixed或者mixed的格式,如何对比两台数据库服务器上的数据是否一致呢

  8. SQL Server数据库 备份A库,然后删除A库,再还原A库,此时数据库一直显示“正在还原”的解决方法

    SQL Server数据库 备份A库,然后删除A库,再还原A库,此时数据库一直显示"正在还原"的解决方法: A库一直显示"正在还原". 在这种状态下,由于未提交 ...

  9. 一个适合变化的产品部署集成包(nginx+jdk+tomcat+nodejs+mysql+redis+mongo+MYSQL主主(读写分离)集群建立+代码包+持续上线+备份)

    一.前言 最近公司做了一套新产品,需要发布到不确定的硬件环境中(不同使用单位规模,使用人数,服务器提供的资源不同)若每次进行人工部署耗时费力,周期过长. 二.分析 具体的部署流程如下: 由上图流程进行 ...

随机推荐

  1. TensorFlow——共享变量的使用方法

    1.共享变量用途 在构建模型时,需要使用tf.Variable来创建一个变量(也可以理解成节点).当两个模型一起训练时,一个模型需要使用其他模型创建的变量,比如,对抗网络中的生成器和判别器.如果使用t ...

  2. usaco-Subset Sums

    题意: 给出一个1-n的数列,求把它分为两组数使得两组数的和相等的方案数. 分析: 如果可能分成两组,那么(n+1)n/2一定为偶数,且n%4=2或3.可以设dp[i][j]表示从1-i中的数拼出的方 ...

  3. 模糊测试工具Simple Fuzzer

    模糊测试工具Simple Fuzzer   模糊测试是一种不同于渗透测试的漏洞检测方式.它向目标系统发送各种非预期的输入,然后通过监视异常结果来发现漏洞.Kali Linux虽然作为渗透测试系统平台, ...

  4. rails 给类添加属性

    steven@ubuntu:~/RubymineProjects/OAONLINE$ rails generate migration AddPasswordToUsers password:stri ...

  5. oracle中的替换函数replace和translate函数

    .translate 语法:TRANSLATE(char, from, to) 用法:返回将出现在from中的每个字符替换为to中的相应字符以后的字符串. 若from比to字符串长,那么在from中比 ...

  6. C语言实现的水仙花数

    #include <stdio.h>void main(){ int ge,shi,bai;      for (int i =100; i < 1000; i++)     {   ...

  7. 算法之美--1.蒙特卡洛方法计算pi

    基本思想: 利用圆与其外接正方形面积之比为pi/4的关系,通过产生大量均匀分布的二维点,计算落在单位圆和单位正方形的数量之比再乘以4便得到pi的近似值.样本点越多,计算出的数据将会越接近真识的pi(前 ...

  8. node在Fedora 22系统下开发环境搭建

    事实上,环境搭建在linux系统还是比較简单的,下载已经编译好的包,配置一下环境变量. 或者下载源代码,自己编译. 这里记录一下,主要是node版本号变化节奏很块的情况下.怎样配置一次环境变量就不要再 ...

  9. 利用Acunetix WVS进行批量网站漏洞评估

    我们知道Acunetix WVS可以对网站进行安全性评估,那么怎么能批量扫描呢?游侠(www.youxia.org)在测试WVS 8 BETA2的时候发现WVS居然支持WEB管理,还是很方便的. 打开 ...

  10. hdu 5444 Elven Postman(长春网路赛——平衡二叉树遍历)

    题目链接:pid=5444http://">http://acm.hdu.edu.cn/showproblem.php?pid=5444 Elven Postman Time Limi ...