MongoError: server instance in invalid state undefined 解决办法
MongoDB关键点集锦(更新中...)
2017-01-20 09:33:48[其它数据库]点击数:15作者:Real_Bird的博客来源: 网络
随机为您推荐的文章:MongDB索引的介绍及使用
索引的重要性,应该无需多说了,它可以优化我们的查询,而且在某些特定类型的查询中,索引几乎是必不可少的。这篇文章主要介绍了MongoDB中的几种常见的索引以及在使用时候的一些注
1、MongoError: server instance in invalid state undefined
参考segmentfault的一个解答
看起来你用的是node-mongodb-native驱动。老版本确实有使用过DB作为顶级对象,不过现在的驱动通常建议把MongoClient用为顶级对象使用。直接参考驱动文档:
https://github.com/mongodb/node-mongodb-native#connecting-to-mongodb
注意MongoClient维护着连接池,所以通常当你的应用退出前都可以不要关闭,保留一个单例的MongoClient一直用就可以了。
我按照这个建议后,确实修复了这个问题。修改后的代码如下(关键代码):
// 使用mongoClient作为顶级对象,而不是require('mongodb')
var mongodbClient = require('mongodb').MongoClient;
// 连接数据库取代了db.open(...)
mongodbClient.connect(url, function(err, db) {
if(err) {
return callback(err);
}
var collection = db.collection('posts');
collection.insertOne(post, function(err) {
if(err) {
return callback(err);
}
callback(null);
db.close();
})
})
通过collection操作数据库的方法。我打印了collection.__proto__,如下所示:
{ collectionName: [Getter],
namespace: [Getter],
readConcern: [Getter],
writeConcern: [Getter],
hint: [Getter/Setter],
find: [Function],
insertOne: [Function],
insertMany: [Function],
bulkWrite: [Function],
insert: [Function],
updateOne: [Function],
replaceOne: [Function],
updateMany: [Function],
update: [Function],
deleteOne: [Function],
removeOne: [Function],
deleteMany: [Function],
removeMany: [Function],
remove: [Function],
save: [Function],
findOne: [Function],
rename: [Function],
drop: [Function],
options: [Function],
isCapped: [Function],
createIndex: [Function],
createIndexes: [Function],
dropIndex: [Function],
dropIndexes: [Function],
dropAllIndexes: [Function],
reIndex: [Function],
listIndexes: [Function],
ensureIndex: [Function],
indexExists: [Function],
indexInformation: [Function],
count: [Function],
distinct: [Function],
indexes: [Function],
stats: [Function],
findOneAndDelete: [Function],
findOneAndReplace: [Function],
findOneAndUpdate: [Function],
findAndModify: [Function],
findAndRemove: [Function],
aggregate: [Function],
parallelCollectionScan: [Function],
geoNear: [Function],
geoHaystackSearch: [Function],
group: [Function],
mapReduce: [Function],
initializeUnorderedBulkOp: [Function],
initializeOrderedBulkOp: [Function] }
2、mongodb更新一个数组,如commets是一个数组,添加comment
collection.update({
"name": name,
}, {
$push: {"comments": comment}
})
3、distinct(key, query, options, callback){Promise}
查询集合返回一个带有key键的值组成的列表,列表的值是不重复的。其中每个集合需要满足query条件。
var MongoClient = require('mongodb').MongoClient,
test = require('assert');
MongoClient.connect('mongodb://localhost:27017/test', function(err, db) {
// 创建集合
var collection = db.collection('distinctExample2');
// 插入多个文档
collection.insertMany([{a:0, b:{c:'a'}}, {a:1, b:{c:'b'}}, {a:1, b:{c:'c'}},
{a:2, b:{c:'a'}}, {a:3}, {a:3}, {a:5, c:1}], {w:1}, function(err, ids) {
// 返回含有c是1的集合的a属性的值组成的列表。 [5]
collection.distinct('a', {c:1}, function(err, docs) {
test.deepEqual([5], docs.sort());
db.close();
});
})
});
4、查询所有tags中包含tag的文档。tags是数组 [‘a’, ‘b’, ‘c’]的形式
collection.find({tags: 'a'}) // 这样也可以查询到这个集合
5、使用$inc增加某个字段
// 查询到这个文档,将`pv`字段加1,如果不存在`pv`字段,则创建pv字段,初始值为0,并加1
collection.updateOne({
"name": name,
"time.day": day,
"title": title
}, {
$inc: {"pv": 1}
}, function(err) {
if(err) {
return callback(err);
}
db.close();
})
6、mongodb分页,主要用到count和skip和limit属性
Post.getTen = function(name, page, callback) {
mongodbClient.connect(url, function(err, db) {
if(err) {
return callback(err)
}
var collection = db.collection('posts');
var query = {};
if(name) {
query.name = name;
}
// 对于一个query对象,首先使用`count()`来查询到总数目,结果值赋给参数`total`,比如`total`是93,然后使用`find()`查询,并跳过(page-1)*10个结果,返回之后的10个结果,按时间(`time`)降序排序。1是升序,也是默认的。-1是降序
collection.count(query, function(err, total) {
collection.find(query).skip((page - 1) * 10).limit(10).sort({
time: -1
}).toArray(function(err, docs) {
if(err) {
return callback(err);
}
callback(null, docs, total);
db.close();
})
})
})
}
以上就是MongoDB关键点集锦(更新中...)的全文介绍,希望对您学习和使用数据库有所帮助.
索引的重要性,应该无需多说了,它可以优化我们的查询,而且在某些特定类型的查询中,索引几乎是必不可少的。这篇文章主要介绍了MongoDB中的几种常见的索引以及在使用时候的一些注
参考segmentfault的一个解答
看起来你用的是node-mongodb-native驱动。老版本确实有使用过DB作为顶级对象,不过现在的驱动通常建议把MongoClient用为顶级对象使用。直接参考驱动文档:
https://github.com/mongodb/node-mongodb-native#connecting-to-mongodb
注意MongoClient维护着连接池,所以通常当你的应用退出前都可以不要关闭,保留一个单例的MongoClient一直用就可以了。
我按照这个建议后,确实修复了这个问题。修改后的代码如下(关键代码):
// 使用mongoClient作为顶级对象,而不是require('mongodb')
var mongodbClient = require('mongodb').MongoClient;
// 连接数据库取代了db.open(...)
mongodbClient.connect(url, function(err, db) {
if(err) {
return callback(err);
}
var collection = db.collection('posts');
collection.insertOne(post, function(err) {
if(err) {
return callback(err);
}
callback(null);
db.close();
})
})
通过collection操作数据库的方法。我打印了collection.__proto__,如下所示:
{ collectionName: [Getter],
namespace: [Getter],
readConcern: [Getter],
writeConcern: [Getter],
hint: [Getter/Setter],
find: [Function],
insertOne: [Function],
insertMany: [Function],
bulkWrite: [Function],
insert: [Function],
updateOne: [Function],
replaceOne: [Function],
updateMany: [Function],
update: [Function],
deleteOne: [Function],
removeOne: [Function],
deleteMany: [Function],
removeMany: [Function],
remove: [Function],
save: [Function],
findOne: [Function],
rename: [Function],
drop: [Function],
options: [Function],
isCapped: [Function],
createIndex: [Function],
createIndexes: [Function],
dropIndex: [Function],
dropIndexes: [Function],
dropAllIndexes: [Function],
reIndex: [Function],
listIndexes: [Function],
ensureIndex: [Function],
indexExists: [Function],
indexInformation: [Function],
count: [Function],
distinct: [Function],
indexes: [Function],
stats: [Function],
findOneAndDelete: [Function],
findOneAndReplace: [Function],
findOneAndUpdate: [Function],
findAndModify: [Function],
findAndRemove: [Function],
aggregate: [Function],
parallelCollectionScan: [Function],
geoNear: [Function],
geoHaystackSearch: [Function],
group: [Function],
mapReduce: [Function],
initializeUnorderedBulkOp: [Function],
initializeOrderedBulkOp: [Function] }
2、mongodb更新一个数组,如commets是一个数组,添加comment
collection.update({
"name": name,
}, {
$push: {"comments": comment}
})
3、distinct(key, query, options, callback){Promise}
查询集合返回一个带有key键的值组成的列表,列表的值是不重复的。其中每个集合需要满足query条件。
var MongoClient = require('mongodb').MongoClient,
test = require('assert');
MongoClient.connect('mongodb://localhost:27017/test', function(err, db) {
// 创建集合
var collection = db.collection('distinctExample2');
// 插入多个文档
collection.insertMany([{a:0, b:{c:'a'}}, {a:1, b:{c:'b'}}, {a:1, b:{c:'c'}},
{a:2, b:{c:'a'}}, {a:3}, {a:3}, {a:5, c:1}], {w:1}, function(err, ids) {
// 返回含有c是1的集合的a属性的值组成的列表。 [5]
collection.distinct('a', {c:1}, function(err, docs) {
test.deepEqual([5], docs.sort());
db.close();
});
})
});
4、查询所有tags中包含tag的文档。tags是数组 [‘a’, ‘b’, ‘c’]的形式
collection.find({tags: 'a'}) // 这样也可以查询到这个集合
5、使用$inc增加某个字段
// 查询到这个文档,将`pv`字段加1,如果不存在`pv`字段,则创建pv字段,初始值为0,并加1
collection.updateOne({
"name": name,
"time.day": day,
"title": title
}, {
$inc: {"pv": 1}
}, function(err) {
if(err) {
return callback(err);
}
db.close();
})
6、mongodb分页,主要用到count和skip和limit属性
Post.getTen = function(name, page, callback) {
mongodbClient.connect(url, function(err, db) {
if(err) {
return callback(err)
}
var collection = db.collection('posts');
var query = {};
if(name) {
query.name = name;
}
// 对于一个query对象,首先使用`count()`来查询到总数目,结果值赋给参数`total`,比如`total`是93,然后使用`find()`查询,并跳过(page-1)*10个结果,返回之后的10个结果,按时间(`time`)降序排序。1是升序,也是默认的。-1是降序
collection.count(query, function(err, total) {
collection.find(query).skip((page - 1) * 10).limit(10).sort({
time: -1
}).toArray(function(err, docs) {
if(err) {
return callback(err);
}
callback(null, docs, total);
db.close();
})
})
})
}
以上就是MongoDB关键点集锦(更新中...)的全文介绍,希望对您学习和使用数据库有所帮助.
MongoError: server instance in invalid state undefined 解决办法的更多相关文章
- mongo 数据库提前关闭 避免读写任务没有结束,异步任务没有完成,同步指令提前关闭数据库:'MongoError: server instance pool was destroyed'
mongo 数据库提前关闭 // mongodb - npm https://www.npmjs.com/package/mongodb const mongoCfg = { uri: 'mongod ...
- MySQL: Starting MySQL….. ERROR! The server quit without updating PID file解决办法
MySQL: Starting MySQL….. ERROR! The server quit without updating PID file解决办法 1 问题 [root@localhost m ...
- $('#checkbox').attr('checked'); 返回的是checked或者是undefined解决办法
$('#checkbox').attr('checked'); 返回的是checked或者是undefined解决办法 <input type='checkbox' id='cb'/> ...
- Sql Server 2008 卸载重新安装失败的解决办法!(多次偿试,方法均有效!)
Sql Server 2008 卸载重新安装失败的解决办法!(多次偿试,方法均有效!) 1.控制面板中卸载所有带sql server的程序. 2.在C盘C:\Program Files中sqlserv ...
- 在Windows2008下安装SQL Server 2005无法启动服务的解决办法
在Windows2012下安装SQL Server 2005无法启动服务的解决办法 1.正常安装任一版本的SQL Server 2005. 2.安装到SqlServer服务的时候提示启动服务失败 此 ...
- keepalived启动后报错:(VI_1): received an invalid passwd!的解决办法
一.配置好keepalived.conf文件后,启动keepalived,查看/var/log/message日志报错: [root@push-- sbin]# tail -f /var/log/me ...
- Server Tomcat v7.0 Server at localhost failed to start解决办法
今晚搞了下tomcat,在调试的时候发现报了这样一个错误Server Tomcat v7.0 Server at localhost failed to start 首先,确认了端口号8080是不是被 ...
- sql server 2008 评估期已过期解决办法
开始-->所有程序-->Microsoft SQL Server 2008-->配置工具-->SQL Server 安装中心-->维护-->版本升级,接着按照提示一 ...
- macOS下加载动态库dylib报"code signature invalid"错误的解决办法
一.现象描述 在macOS上搞开发也有一段时间了,也积攒了一定的经验.然而,今天在替换工程中的一个动态库时还是碰到了一个问题.原来工程中用的是一个静态库,调试时发现有问题就把它替换成了动态库.这本来没 ...
随机推荐
- ssh框架搭建出现的问题和解决
[说明]今天尝试从头开始搭建ssh 框架, 真心是有点不太容易,可能是第一次吧,之前都是小打小闹. 一:今日完成 搭建 Spring 环境 --> 然后搭建 Hibernate 环境 --> ...
- SVN上新增一个项目和用户
author:headsen chen date:2018-05-04 11:01:08 1,在SVN服务器上,打开SVN的软件,在项目里新建一个文件夹.在Repositories下面 2,use ...
- [LintCode] 合并排序数组
A subroutine of merge sort. class Solution { public: /** * @param A and B: sorted integer array A an ...
- jQuery弹出层layer插件的使用
引入插件layer 触发弹出层的按钮/链接 <a href="javascript:showPop();"> <img src="" /> ...
- 《挑战程序设计竞赛》2.2 贪心法-其它 POJ3617 3069 3253 2393 1017 3040 1862 3262
POJ3617 Best Cow Line 题意 给定长度为N的字符串S,要构造一个长度为N的字符串T.起初,T是一个空串,随后反复进行下列任意操作: 从S的头部(或尾部)删除一个字符,加到T的尾部 ...
- CoffeeScript编译手记
最近折腾bootstrap,看到一个Messager插件挺好的,可这丫的发出来的都是CoffeeScript的DEMO,顿时让我感觉自己已经落后了一百年. 于是各种搜索,各种脑补,原来Coffee最后 ...
- 2 CDuiString的bug
重温了一下 Effective C++,发现这就是条款24所指出的问题,看来读书百遍不如写代码一遍啊 在Notify处理消息时会有很多if语句,我通常喜欢把常量放在双等号前面,变量放在后面,比如: ...
- Java 之继承和 final 关键字
继承的概述 继承的特点 super 关键字 函数覆盖 子类的实例化过程 final 关键字 1. 继承的概述 继承是类与类之间的关系. 继承的好处: 提高了代码的复用性 让类与类之间产生了关系, 给第 ...
- SQL 时间函数 Datepart()与DateName()
1.Datepart() 返回代表指定日期的指定日期部分的整数 语法 Datepart(datepart,date) 返回类型 int datepart: 日期部分 缩写 year yy, yyyy ...
- Redis的LRU机制(转)
原文:Redis的LRU机制 在Redis中,如果设置的maxmemory,那就要配置key的回收机制参数maxmemory-policy,默认volatile-lru,参阅Redis作者的原博客:a ...