下面是我的用户模式在user.js模型-

var userSchema = new mongoose.Schema({
    local: {
        name: { type: String },
        email : { type: String, require: true, unique: true },
        password: { type: String, require:true },
    },
    facebook: {
        id           : { type: String },
        token        : { type: String },
        email        : { type: String },
        name         : { type: String }
    }
});

var User = mongoose.model('User',userSchema);

module.exports = User;

这就是我在控制器中使用它的方式

var user = require('./../models/user.js');

这就是我在数据库中保存它的方式

user({'local.email' : req.body.email, 'local.password' : req.body.password}).save(function(err, result){
    if(err)
        res.send(err);
    else {
        console.log(result);
        req.session.user = result;
        res.send({"code":200,"message":"Record inserted successfully"});
    }
});

错误- - - - - -

{"name":"MongoError","code":11000,"err":"insertDocument :: caused by :: 11000 E11000 duplicate key error index: mydb.users.$email_1  dup key: { : null }"} 

我检查了db集合,没有这样的重复条目存在,让我知道我做错了什么?

供您参考- req.body.email和req.body.password是抓取值。

我也检查了这篇文章,但没有帮助

如果我完全删除,然后它插入文档,否则它抛出错误“重复”错误,即使我在local.email中有一个条目


当前回答

错误消息表示已经有一个记录,其中的电子邮件为null。换句话说,您已经有了一个没有电子邮件地址的用户。

相关文件:

If a document does not have a value for the indexed field in a unique index, the index will store a null value for this document. Because of the unique constraint, MongoDB will only permit one document that lacks the indexed field. If there is more than one document without a value for the indexed field or is missing the indexed field, the index build will fail with a duplicate key error. You can combine the unique constraint with the sparse index to filter these null values from the unique index and avoid the error.

唯一索引

稀疏索引仅包含具有索引字段的文档条目,即使索引字段包含空值。

换句话说,稀疏索引适用于多个文档都具有空值的情况。

稀疏索引


评论:

您的错误显示键名为mydb.users。$email_1,这使我怀疑您有两个用户的索引。email和users.local.email(前者是旧的,目前未使用)。从Mongoose模型中删除字段不会影响数据库。检查mydb.users.getIndexes(),如果是这种情况,用mydb.users.dropIndex(<name>)手动删除不需要的索引。

其他回答

错误消息表示已经有一个记录,其中的电子邮件为null。换句话说,您已经有了一个没有电子邮件地址的用户。

相关文件:

If a document does not have a value for the indexed field in a unique index, the index will store a null value for this document. Because of the unique constraint, MongoDB will only permit one document that lacks the indexed field. If there is more than one document without a value for the indexed field or is missing the indexed field, the index build will fail with a duplicate key error. You can combine the unique constraint with the sparse index to filter these null values from the unique index and avoid the error.

唯一索引

稀疏索引仅包含具有索引字段的文档条目,即使索引字段包含空值。

换句话说,稀疏索引适用于多个文档都具有空值的情况。

稀疏索引


评论:

您的错误显示键名为mydb.users。$email_1,这使我怀疑您有两个用户的索引。email和users.local.email(前者是旧的,目前未使用)。从Mongoose模型中删除字段不会影响数据库。检查mydb.users.getIndexes(),如果是这种情况,用mydb.users.dropIndex(<name>)手动删除不需要的索引。

这是因为已经存在一个与配置同名的集合..请通过mongo shell从mongodb中删除该集合,然后重试。

db.collectionName.remove()

现在运行应用程序,它应该可以工作

如果您仍然在开发环境中,我会删除整个db,并使用新的模式重新开始。

从命令行

➜ mongo
use dbName;
db.dropDatabase();
exit

删除您的数据库,然后它将工作。

您可以执行以下步骤删除数据库

步骤1:进入mongodb安装目录,默认目录为“C:\Program Files\ mongodb \Server\4.2\bin”

步骤2:直接启动mongo .exe或使用命令提示符将其最小化。

步骤3:直接或使用命令提示符启动mongo.exe,执行以下命令

i)使用你的数据库名(如果你不记得数据库名,使用显示数据库)

(二)db.dropDatabase ()

这将删除数据库。 现在你可以插入你的数据,它不会显示错误,它会自动添加数据库和集合。

我也有同样的问题。尝试调试不同的方法都没有找到答案。我试过放弃这个系列,之后效果很好。尽管如果您的集合有很多文档,这不是一个好的解决方案。但是如果您处于开发的早期状态,请尝试删除集合。

db.users.drop();