当前位置: 移动技术网 > IT编程>数据库>MongoDB > MongoDB 最近遇到的几个小问题

MongoDB 最近遇到的几个小问题

2019年05月17日  | 移动技术网IT编程  | 我要评论

(1)连接数据库时报错

error topshelf.hosts.consolerunhost.run 1 
an exception occurred
system.timeoutexception: a timeout occured after 30000ms selecting a server using compositeserverselector

错误原因是和转义字符有关。连接字符串使用的url格式,所以其中的密码中的% 需要转义。

知识扩展:

连接mongo使用uri有特殊字符 '@' 或者":"或者‘%’, 连接会报错,需要进行转义。

解决方法:

把 @ 换成 %40 

把 : 换成 %3a

把 % 换成 %25

 (2)时间字段的范围查询,请注意时间字段的类型。

例如,明看到集合中指定时间段内有数据,但是count结果还是显示为0。

最早的一笔数据是20170816,但是使用以下语句查询2017-08-14 到 2018-08-18时间段内的文档数据为0.

 

问题在哪儿哪? 

查看发现query.time字段类型是 string.

我们将查询语句的条件格式转换为字段的存储格式,就ok了。

 

所以,在设计集合模式时,要特别注意存储日期时间的字段的类型,建议为date。另外,查询时也要小心,防止数据异常。

(3)副本集添加节点时报错

错误信息:

{
    "ok" : 0,
    "errmsg" : "quorum check failed because not enough voting nodes responded; required 2 but only the following 1 voting nodes responded: 172.xxx.xxx.xxx:27017; the following nodes did not respond affirmatively: 172.xxx.xxx.xx:27017 failed with no route to host",
    "code" : 74,
    "codename" : "nodenotfound"
}

错误原因是:防火墙没关闭 导致

(4)将常规集合设置为固定集合,既有的索引丢失,需要重建。

db.runcommand({"converttocapped": "集合名字", size: xxxxxx,"max":xxxxx});

设置为固定集合后,原来的索引都丢失了,需要谨记 。

(曾经的一个转换性能测试,及转为固定集合的耗时:1001 w数据,16.8 g 约耗时 6分钟)

(5)mongodb 对字段中有超过 1024 字节的不会建立索引

mongodb will not create an index on a collection if the index entry for an existing document exceeds the index key limit (1024 bytes). you can however create a hashed index or text index instead:

除了上面的介绍外,还可以修改启动配置参数 ailindexkeytoolong。

(6)配置分片的复制集需,在启动的配置文件中需 指定 shardsvr参数。否则,在启动数据库分片时报错。

错误信息如下:

在config文件中,添加 shardsvr=true 即可。

重启服务,再次启动启动分片,执行ok.

 

附注:

mongodb是一种非关系型数据库(nosql),很好的实现了面向对象的思想(oo思想),在mongo db中 每一条记录都是一个document对象。mongo db最大的优势在于所有的数据持久操作都无需开发人员手动编写sql语句,直接调用方法就可以轻松的实现crud操作。

如对本文有疑问, 点击进行留言回复!!

相关文章:

验证码:
移动技术网