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