mongodb报Write failed with error code 17280 and error message 'WiredTigerIndex::insert: key too large

错误描述

最近在使用mongodb的过程中,项目已经上线了但是报了错,怎么办呢?
公司日志系统使用kafka发送消息,我这边接受消息存到mongodb里面,但是在启动的过程中报了题目上的错,WiredTigerIndex::insert: key too large to index。大致意思是:“键太大而无法索引”,网上说,mongodb3.x对于超过1024kb的数据不会建立索引。

错误分析解决

亲自测试了下,比如字段name如果没有创建索引,就不会报错,如果建立了一般的单键索引或者复合索引,如果插入数据超过1024(我测的超过1011就报错了)就会报错。
对于这个问题,有两种解决方案
- 设置mongodb忽略这个错误
- 对于报错的键设置hash或者text索引

1 忽略错误

启动程序的时候加入参数

mongod --setParameter failIndexKeyTooLong=false //后面加入其他参数

//当然也可以将其他启动参数放到一个配置文件中
mongod --setParameter failIndexKeyTooLong=false -f mongod.conf

mongod.conf的详细使用参考这篇文章
https://blog.csdn.net/GY325416/article/details/80970016

2 建立hash或者text索引

只能匹配索引,不能进行范围查询
db.Log.createIndex({"content":"hashed"})
db.Log.createIndex({"content":"text"})
原文地址:https://www.cnblogs.com/paper-man/p/13284758.html