mongo常见错误

  1. BulkWriteError: batch op errors occurred
    原因:一般90%以上都是id重复了
    解决方式: 字符串拼接的id要格外注意,尤其 ip,port拼接

  2. mongo AutoReconnect: 127.0.0.1:19868: [Errno 24] Too many open files
    原因:多线程操作mongo,linux文件默认打开数较小
    解决方式:
    1)修改当前交互终端的limit值
    查询当前终端的文件句柄数: ulimit -n 回车,一般的系统默认的1024.
    修改文件句柄数为65535,ulimit -n 65535.此时系统的文件句柄数为65535.

    2)将ulimit 值添加到/etc/profile文件中(适用于有root权限登录的系统)
    为了每次系统重新启动时,都可以获取更大的ulimit值,将ulimit 加入到/etc/profile 文件底部。
    echo ulimit -n 65535 >>/etc/profile
    source /etc/profile #加载修改后的profile
    ulimit -n #显示65535,修改完毕!

    3)都以为大功告成了,可以突然发现自己再次登录进来的时候,ulimit的值还是1024,这是为什么呢?
    关键的原因是你登录的用户是什么身份,是不是root用户,由于服务器的root用户权限很大,一般是不能用来登录的,都是通过自己本人的登录权限进行登录,并通过sudo方式切换到root用户下进行工作。 用户登录的时候执行sh脚本的顺序:
    /etc/profile.d/file
    /etc/profile
    /etc/bashrc
    /mingjie/.bashrc
    /mingjie/.bash_profile
    感谢 weixin_33755847 的分享。 https://blog.csdn.net/weixin_33755847/article/details/92928384
    3.mongo插入单条数据报错如下:
    2020-07-13T20:11:16.274+0800 E QUERY [thread1] SyntaxError: missing } after property list @(shell):1:35
    原因:在mongo中存储时,列表类型的数据要转换为字符串

  3. 插入数据报错:
    E11000 duplicate key error collection: measurescan.measure_all index: id d
    解决方式:删除索引,指定唯一索引,或者不指定索引,自己生成id
    db.users.getIndexes() 输出结果是包含多个对象的数组
    db.users.dropIndex({"username":1})
    db.users.dropIndex({你项目的key:value}) 只保留一个 _id作为主键
    db.users.getIndexes() 查看是否只有一个_id

  4. 数据已插入,且能查到,但是报 batch op errors occurred 错误
    原因:

  5. document must be an instance of dict, bson.son.SON, bson.raw_bson.RawBSONDocument, or a type that inherits from collections.MutableMapping
    原因:传递给insertMany或者insertOne的参数不是一个真正的json结构,一般如果使用bulk_write,则需要转换为 from pymongo import InsertOne 类型,但insertMany或者insertOne不需要
    可在插入前进行json.dumps和json.loads操作

原文地址:https://www.cnblogs.com/zhanghaibin16/p/13263765.html