Node.js MongoError:E11000索引的重复错误键。参数显示为null,即使我传入了信息
我一直在查看reddit和n,并尝试了以下链接中的解决方案,但都没有成功。 基本上我有一个列表模式和一个用户模式,我想索引一个用户和一个与之关联的列表名。 以下是我的列表模式:Node.js MongoError:E11000索引的重复错误键。参数显示为null,即使我传入了信息,node.js,mongodb,mongoose,Node.js,Mongodb,Mongoose,我一直在查看reddit和n,并尝试了以下链接中的解决方案,但都没有成功。 基本上我有一个列表模式和一个用户模式,我想索引一个用户和一个与之关联的列表名。 以下是我的列表模式: import mongoose from 'mongoose' const listSchema = new mongoose.Schema( { name: { type: String, required: true, trim: true, maxLe
import mongoose from 'mongoose'
const listSchema = new mongoose.Schema(
{
name: {
type: String,
required: true,
trim: true,
maxLength: 50
},
description: String,
createdBy: {
type: mongoose.SchemaTypes.ObjectId,
ref: 'user',
required: true
}
},
{ timestamps: true }
)
listSchema.index({ user: 1, name: 1 }, { unique: true })
export const List = mongoose.model('list', listSchema)
这是我的控制器(它对其他模式是通用的,但对它们有效)
每次传入该用户时,我都会将该用户视为null,这是一个错误
MongoError: E11000 duplicate key error collection: api-design.lists index: user_1_name_1 dup key: { user: null, name: "08/29/2020" }
driver: true,
index: 0,
code: 11000,
keyPattern: { user: 1, name: 1 },
keyValue: { user: null, name: '08/29/2020' }
即使我从另一个集合中传入了用户的.\u id,该用户仍显示为null
有人能帮我吗?我已尝试从集合中删除索引,并已删除所有列表。您是否可以使用此选项:
listSchema.index({createdBy:1,name:1},{unique:true})代码>
而不是
listSchema.index({user:1,name:1},{unique:true})代码>
MongoError: E11000 duplicate key error collection: api-design.lists index: user_1_name_1 dup key: { user: null, name: "08/29/2020" }
driver: true,
index: 0,
code: 11000,
keyPattern: { user: 1, name: 1 },
keyValue: { user: null, name: '08/29/2020' }