资讯专栏INFORMATION COLUMN

Sparse: MongoError: E11000 duplicate key errorjava

tinyq / 467人阅读

Question: 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
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
        }
    })
error:
MongoError: E11000 duplicate key error index: bot.contacts.$name_1  dup key: { : null }

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.

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.

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.

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
    }
]
Answer

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")

文章版权归作者所有,未经允许请勿转载,若此文章存在违规行为,您可以联系管理员删除。

转载请注明本文地址:https://www.ucloud.cn/yun/19338.html

相关文章

  • Sparse: MongoError: E11000 duplicate key errorjava

    Question: Schema (../models/add.js) var addSchema = new Schema({ name: {type: String, unique: true, sparse: true}, phone: Number, email: String, country: Number }); module.exports = m...

    chaosx110 评论0 收藏0
  • MongoDB指南---13、索引类型、索引管理

    摘要:复合唯一索引也可以创建复合的唯一索引。中的稀疏索引与关系型数据库中的稀疏索引是完全不同的概念。但是这里不会指明索引是否是多键索引。上一篇文章指南使用和何时不应该使用索引下一篇文章指南特殊的索引和集合固定集合索引全文本索引 上一篇文章:MongoDB指南---12、使用explain()和hint()、何时不应该使用索引下一篇文章:MongoDB指南---14、特殊的索引和集合:固定集合...

    Enlightenment 评论0 收藏0
  • MongoDB指南---13、索引类型、索引管理

    摘要:复合唯一索引也可以创建复合的唯一索引。中的稀疏索引与关系型数据库中的稀疏索引是完全不同的概念。但是这里不会指明索引是否是多键索引。上一篇文章指南使用和何时不应该使用索引下一篇文章指南特殊的索引和集合固定集合索引全文本索引 上一篇文章:MongoDB指南---12、使用explain()和hint()、何时不应该使用索引下一篇文章:MongoDB指南---14、特殊的索引和集合:固定集合...

    seanHai 评论0 收藏0
  • MongoDB备份和恢复命令

    摘要:备份备份到文件名为的文件尝试重复执行备份会覆盖第一次的备份文件立即恢复中删除了一条记录后执行,删除的行又出来了,已经存在的行报错删除后恢复没有任何异常恢复成功 备份 mongodump --archive=gt_ut.archive --db gt_ut 2018-03-22T18:11:33.555+0800 writing gt_ut.gt_counting_data to ...

    PingCAP 评论0 收藏0
  • TensorFlow Wide And Deep 模型详解与应用

    摘要:我们先看看的初始化函数的完整定义,看构造一个模型可以输入哪些参数我们可以将类的构造函数中的参数分为以下几组基础参数我们训练的模型存放到指定的目录中。看完模型的构造函数后,我们大概知道和端的模型各对应什么样的模型,模型需要输入什么样的参数。 Wide and deep 模型是 TensorFlow 在 2016 年 6 月左右发布的一类用于分类和回归的模型,并应用到了 Google Play ...

    opengps 评论0 收藏0

发表评论

0条评论

最新活动
阅读需要支付1元查看
<