MongoDB dup键:{:null}

时间:2018-10-28 14:45:09

标签: mongodb

我没有很多MongoDB经验,并且遇到了一个我拥有的mLab实例:

E11000 duplicate key error index: development.users.$key_1 dup key: { : null } MongoError: E11000 duplicate key error index: development.users.$key_1 dup key: { : null }

我尝试按key字段进行排序,寻找一个null键,但不确定如何找到重复的null键。造成这种困难的原因是,我什至不知道要搜索Google或堆栈溢出以完成此工作。任何帮助将不胜感激,下面是完整的错误:

Mongoose model 'index-single-done' event fired on 'Family' for index:
Object {key: 1}

With error:
    E11000 duplicate key error index: development.users.$key_1 dup key: { : null } MongoError: E11000 duplicate key error index: 
development.users.$key_1 dup key: { : null }
at Function.MongoError.create (/Users/myUserName/development/projectName/node_modules/mongodb-core/lib/error.js:31:11)
at /Users/myUserName/development/projectName/node_modules/mongodb-core/lib/connection/pool.js:497:72
at authenticateStragglers (/Users/myUserName/development/projectName/node_modules/mongodb-core/lib/connection/pool.js:443:16)
at Connection.messageHandler (/Users/myUserName/development/projectName/node_modules/mongodb-core/lib/connection/pool.js:477:5)
at Socket.<anonymous> (/Users/myUserName/development/projectName/node_modules/mongodb-core/lib/connection/connection.js:333:22)
at Socket.emit (events.js:180:13)
at Socket.emit (domain.js:422:20)
at addChunk (_stream_readable.js:274:12)
at readableAddChunk (_stream_readable.js:261:11)
at Socket.Readable.push (_stream_readable.js:218:10)
at TCP.onread (net.js:581:20)
Mongoose model 'index' event fired on 'Family' with error:
E11000 duplicate key error index: development.users.$key_1 dup key: { : null } MongoError: E11000 duplicate key error index: development.users.$key_1 dup key: { : null }
at Function.MongoError.create (/Users/myUserName/development/projectName/node_modules/mongodb-core/lib/error.js:31:11)
at /Users/myUserName/development/projectName/node_modules/mongodb-core/lib/connection/pool.js:497:72
at authenticateStragglers (/Users/myUserName/development/projectName/node_modules/mongodb-core/lib/connection/pool.js:443:16)
at Connection.messageHandler (/Users/myUserName/development/projectName/node_modules/mongodb-core/lib/connection/pool.js:477:5)
at Socket.<anonymous> (/Users/myUserName/development/projectName/node_modules/mongodb-core/lib/connection/connection.js:333:22)
at Socket.emit (events.js:180:13)
at Socket.emit (domain.js:422:20)
at addChunk (_stream_readable.js:274:12)
at readableAddChunk (_stream_readable.js:261:11)
at Socket.Readable.push (_stream_readable.js:218:10)
at TCP.onread (net.js:581:20)

2 个答案:

答案 0 :(得分:0)

duplicate key error index: development.users.$key_1 dup key: { : null }

这意味着您的key字段设置为仅接受唯一值,并且已经有一个记录,其中key的值为null,并且您不能插入另一个带有{{1}的文档}设为key。这类似于在SQL数据库中具有主键。

检查您的猫鼬模式,就像您在使用猫鼬一样。

最有可能:

  1. 保存记录时您没有通过密钥
  2. 如果要传递密钥,则其值为null

答案 1 :(得分:-1)

我遇到了同样的问题

我尝试了不同的技术来查找数据库验证或数据库值,但没有发现无效的

最终删除了集合并解决了问题。尽管这不是一个好的解决方案,但在测试阶段还可以。