使用稀疏:true仍在获取MongoError:E11000重复键错误 [英] Using sparse: true still getting MongoError: E11000 duplicate key error

查看:68
本文介绍了使用稀疏:true仍在获取MongoError:E11000重复键错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

模式(../models/add.js)

Schema (../models/add.js)

var addSchema = new Schema({
    name: {type: String, unique: true, sparse: true},
    phone: Number,
    email: String,
    country: Number
});

module.exports = mongoose.model('Contact', addSchema);

add-manager.js

add-manager.js

var Add = require('../models/add.js');
var AM = {};
var mongoose = require('mongoose');
module.exports = AM;

AM.notOwned = function(country, callback)
{
    Add.update({country: country}, {country: country}, {upsert: true}, function(err, res){
        if (err) callback (err);
        else callback(null, res);
    })
}

news.js

// if country # is not in the database
    AM.notOwned(country, function(error, resp){
        if (error) console.log("error: "+error);
        else 
        {
            // do stuff
        }
    })

错误:

MongoError: E11000 duplicate key error index: bot.contacts.$name_1  dup key: { : null }

看到错误消息后,我四处搜寻并了解到,在创建文档时,由于未设置名称,因此将其视为null. 请参阅猫鼬Google网上论坛线程调用AM.notOwned时,它将起作用,因为集合中没有名称键就没有任何文档.然后,AM.notOwned将插入一个带有ID字段和国家/地区字段的文档.

After seeing the error message, I googled around and learned that when the document is created, since name isn't set, its treated as null. See Mongoose Google Group Thread The first time AM.notOwned is called it will work as there isn't any documents in the collection without a name key. AM.notOwned will then insert a document with an ID field, and a country field.

随后的AM.notOwned调用失败,因为已经存在一个没有名称字段的文档,因此将其视为名称:null,第二个AM.notOwned调用失败,因为未设置名称"字段并将其视为也为null;因此它不是唯一的.

Subsequent AM.notOwned calls fails because there is already a document with no name field, so its treated as name: null, and the second AM.notOwned is called fails as the field "name" is not set and is treated as null as well; thus it is not unique.

因此,遵循Mongoose线程的建议并阅读 mongo docs 我使用了稀疏模型:true.但是,它仍然抛出相同的错误.进一步调查,我认为可能是与以下问题相同:,但是将模式设置为名称:{类型:字符串,索引:{唯一:真,稀疏:真}}也无法解决.

So, following the advice of the Mongoose thread and reading the mongo docs I looked at using sparse: true. However, its still throwing the same error. Further looking into it, I thought it may be the same issue as: this, but setting schema to name: {type: String, index: {unique: true, sparse: true}} doesn't fix it either.

S.O.问题/答案使我相信它可能是由于索引不正确导致的,但是我不太确定如何从Mongo控制台读取db.collection.getIndexes().

This S.O. question/answer leads me to believe it could be caused by the index not being correct, but I'm not quite sure how to read the the db.collection.getIndexes() from Mongo console.

db.contacts.getIndexes()
[
    {
        "v" : 1,
        "key" : {
            "_id" : 1
        },
        "ns" : "bot.contacts",
        "name" : "_id_"
    },
    {
        "v" : 1,
        "key" : {
            "name" : 1
        },
        "unique" : true,
        "ns" : "bot.contacts",
        "name" : "name_1",
        "background" : true,
        "safe" : null
    }
]

我该怎么办才能解决此错误?

What can I do to resolve this error?

推荐答案

您需要将旧的非稀疏索引删除到shell中,以便Mongoose下次使用应用程序时可以使用sparse: true重新创建它.

You need to drop the old, non-sparse index in the shell so that Mongoose can recreate it with sparse: true the next time your app runs.

> db.contacts.dropIndex('name_1')

这篇关于使用稀疏:true仍在获取MongoError:E11000重复键错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆