博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Sparse: MongoError: E11000 duplicate key errorjavascript
阅读量:5796 次
发布时间:2019-06-18

本文共 2516 字,大约阅读时间需要 8 分钟。

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

转载地址:http://jabfx.baihongyu.com/

你可能感兴趣的文章
驳》我自己写的深拷贝
查看>>
吴恩达机器学习系列16:机器学习系统设计
查看>>
从原始到最新的垃圾回收器
查看>>
用于 C# 的 SQL 基本语法总结
查看>>
上一家公司倒闭,为什么我又来了创业公司?
查看>>
CSS 常用布局方式,让你青铜到王者
查看>>
函数节流和防抖
查看>>
开发思维方式浅谈
查看>>
联科首个开源项目启动!未来可期,诚邀加入!
查看>>
什么是一致性Hash算法?
查看>>
React项目实战(四)滚动的数字
查看>>
Found a swap file by the name "~/Documents/XXX/XXX-android/.git/.MERGE_
查看>>
85%的AI项目以失败告终,切记这6条成功原则!
查看>>
webpack总结
查看>>
【译】我是如何学习任意前端框架的
查看>>
开宸携手德国配件大匠闪耀2017汽配盛会:专注的品牌只相信精益求精
查看>>
Android混合开发模式下Cookies的管理
查看>>
JVM知识点——类加载机制相关考点
查看>>
mac修改用户默认bash shell为zsh
查看>>
直播系统app源码经验分享——直播系统PK功能介绍
查看>>