小编Kim*_*m K的帖子

MongoError: E11000 重复键错误集合:用户索引:mobile_1 dup key: { mobile: null }

我创建了一个用户架构,其中有一个移动字段。移动字段应该是唯一的,但仍然允许空值,并且应该只与字符串比较唯一性。这是我的调试信息:

我的用户模式中的“移动”键

mobile: {
  type: String,
  index: {
    unique: true,
    partialFilterExpression: { mobile: { $type: 'string' } }
  },
}
Run Code Online (Sandbox Code Playgroud)

猫鼬调试日志

Mongoose: users.createIndex({ mobile: 1 }, { unique: true, partialFilterExpression: { mobile: { '$type': 'string' } }, background: true})
Run Code Online (Sandbox Code Playgroud)

错误

MongoError: E11000 duplicate key error collection: users index: mobile_1 dup key: { mobile: null }
    at Function.create (~/project/node_modules/mongodb/lib/core/error.js:44:12)
    at toError (~/project/node_modules/mongodb/lib/utils.js:150:22)
    at ~/project/node_modules/mongodb/lib/operations/common_functions.js:265:39
    at handler (~/project/node_modules/mongodb/lib/core/sdam/topology.js:971:24)
    at ~/project/node_modules/mongodb/lib/core/sdam/server.js:496:5
    at ~/project/node_modules/mongodb/lib/core/connection/pool.js:420:18
    at processTicksAndRejections (internal/process/task_queues.js:75:11) {
  driver: true,
  name: 'MongoError',
  index: 0, …
Run Code Online (Sandbox Code Playgroud)

collections mobile key duplicates mongoose

4
推荐指数
1
解决办法
2万
查看次数

标签 统计

collections ×1

duplicates ×1

key ×1

mobile ×1

mongoose ×1