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)
})
}
mongo 数据库提前关闭 避免读写任务没有结束,异步任务没有完成,同步指令提前关闭数据库:'MongoError: server instance pool was destroyed'的更多相关文章
- 转 查看磁盘IO负载 - 看哪些进程在读写磁盘 以及oracle 异步I/O 和同步I/O
https://www.cnblogs.com/cloudstorage/archive/2012/11/11/2764623.html #####sample 1: Oracle等待事件db fil ...
- spring Batch实现数据库大数据量读写
spring Batch实现数据库大数据量读写 博客分类: spring springBatchquartz定时调度批处理 1. data-source-context.xml <?xml v ...
- MySQL/MariaDB数据库的PROXY实现读写分离
MySQL/MariaDB数据库的PROXY实现读写分离 作者:尹正杰 版权声明:原创作品,谢绝转载!否则将追究法律责任. 一.ProxySQL概述 1>.各家互联网公司读写分离的解决方案 m ...
- [转]Visual Studio 2008中如何比较二个数据库的架构【Schema】和数据【Data】并同步
使用场景: 在团队开发中,每一个人都有可能随时更新数据库,这时候数据库中数据和架构等信息都会发生变化.如果更新不及时,就会发生数据错误或数据丢失的风险,影响团队的开发效率和 项目进度,这时候我们该怎么 ...
- 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 ...
- Capistrano:自动完成多台服务器上新版本的同步更新,包括数据库的改变
https://baike.baidu.com/item/Capistrano/6844928?fr=aladdin Capistrano是一种在多台服务器上运行脚本的开源工具,它主要用于部署we ...
- 如果数据库上的row格式是mixed或者mixed的格式,如何对比两台数据库服务器上的数据是否一致呢
如果数据库上的row格式是mixed或者mixed的格式,如何对比两台数据库服务器上的数据是否一致呢
- SQL Server数据库 备份A库,然后删除A库,再还原A库,此时数据库一直显示“正在还原”的解决方法
SQL Server数据库 备份A库,然后删除A库,再还原A库,此时数据库一直显示"正在还原"的解决方法: A库一直显示"正在还原". 在这种状态下,由于未提交 ...
- 一个适合变化的产品部署集成包(nginx+jdk+tomcat+nodejs+mysql+redis+mongo+MYSQL主主(读写分离)集群建立+代码包+持续上线+备份)
一.前言 最近公司做了一套新产品,需要发布到不确定的硬件环境中(不同使用单位规模,使用人数,服务器提供的资源不同)若每次进行人工部署耗时费力,周期过长. 二.分析 具体的部署流程如下: 由上图流程进行 ...
随机推荐
- JAVA中的编码分析
在实际编程中可以不用关注JVM中使用的是什么编码,而只需要关注自己输出需要采用的编码,JVM会根据你设置的编码正确操作. 1.String采用的是什么编码? 很多厂家根据规范实现了JVM,JVM只说明 ...
- 2016北京集训测试赛(六)Problem A: 冒泡排序
Solution 观察冒泡排序的过程. 我们注意到, 每一轮的排序都会使得每个数后面比它小的数的个数减\(1\). 我们用\(f(n, m)\)表示对\(1\)到\(n\)的一个排列进行冒泡排序, 满 ...
- setlocale 与 mbstowcs 的问题
C++的字符串转换函数mbstowcs使用时容易产生bug... rapidxml_utils.hpp 的file(const char*filename)函数内会异常宕机... 需要在函数最开始添加 ...
- 开源日历TimesSquare在iOS7下诡异渲染的解决办法
因为没有时间自己写一个日历,所以暂时使用了一个三方的日历https://github.com/square/objc-TimesSquare 但是在iOS7下.突然产生了一个诡异的BUG..如下图: ...
- windows上,python安装非官方包,提示error: Unable to find vcvarsall.bat
在windows机器上安装python非官方包,如果环境只是用于开发,不作任何测试的话,最好的解决办法是: 在Linux上pip安装好之后,把python根目录lib/python3.6/site-p ...
- DIY树莓派之随身工具箱
摆弄树莓派有一年多了,在这里把经验分享给大家,少走弯路. 先放图两张. 搭建目的: wifi信号中转站\网站服务器\IC卡渗透测试\中间人\otr… 基于树莓派3 系统为Kali Linux 2017 ...
- django网站搭建常用的一些代码
from functools import wrapsdef check_user_login(func): @wraps(func) def return_wrapper(request, *arg ...
- http://blog.csdn.net/LANGXINLEN/article/details/50421988
GitHub上史上最全的Android开源项目分类汇总 今天在看博客的时候,无意中发现了 @Trinea在GitHub上的一个项目 Android开源项目分类汇总, 由于类容太多了,我没有一个个完整地 ...
- 2016.8.22 Axure两级下拉框联动的实现
刚学Axure,有些很简单的东西都要弄很久,但是弄出来的总归是很开心的. 参考来自:实现省市县下拉框的三级联动 http://www.woshipm.com/rp/348795.html/commen ...
- 使用FDTemplateLayout框架打造个性App
效果展示 project下载地址 · 进入构建结构 首先我们新建一个project 接下来我们拖进来一个Table View Controller,将Storyboard Entry Point指向我 ...