成人国产在线小视频_日韩寡妇人妻调教在线播放_色成人www永久在线观看_2018国产精品久久_亚洲欧美高清在线30p_亚洲少妇综合一区_黄色在线播放国产_亚洲另类技巧小说校园_国产主播xx日韩_a级毛片在线免费

資訊專欄INFORMATION COLUMN

Sparse: MongoError: E11000 duplicate key errorjava

chaosx110 / 2573人閱讀

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

文章版權歸作者所有,未經允許請勿轉載,若此文章存在違規(guī)行為,您可以聯(lián)系管理員刪除。

轉載請注明本文地址:http://systransis.cn/yun/108353.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...

    tinyq 評論0 收藏0
  • MongoDB指南---13、索引類型、索引管理

    摘要:復合唯一索引也可以創(chuàng)建復合的唯一索引。中的稀疏索引與關系型數(shù)據庫中的稀疏索引是完全不同的概念。但是這里不會指明索引是否是多鍵索引。上一篇文章指南使用和何時不應該使用索引下一篇文章指南特殊的索引和集合固定集合索引全文本索引 上一篇文章:MongoDB指南---12、使用explain()和hint()、何時不應該使用索引下一篇文章:MongoDB指南---14、特殊的索引和集合:固定集合...

    Enlightenment 評論0 收藏0
  • MongoDB指南---13、索引類型、索引管理

    摘要:復合唯一索引也可以創(chuàng)建復合的唯一索引。中的稀疏索引與關系型數(shù)據庫中的稀疏索引是完全不同的概念。但是這里不會指明索引是否是多鍵索引。上一篇文章指南使用和何時不應該使用索引下一篇文章指南特殊的索引和集合固定集合索引全文本索引 上一篇文章:MongoDB指南---12、使用explain()和hint()、何時不應該使用索引下一篇文章:MongoDB指南---14、特殊的索引和集合:固定集合...

    seanHai 評論0 收藏0
  • MongoDB備份和恢復命令

    摘要:備份備份到文件名為的文件嘗試重復執(zhí)行備份會覆蓋第一次的備份文件立即恢復中刪除了一條記錄后執(zhí)行,刪除的行又出來了,已經存在的行報錯刪除后恢復沒有任何異?;謴统晒? 備份 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 模型詳解與應用

    摘要:我們先看看的初始化函數(shù)的完整定義,看構造一個模型可以輸入哪些參數(shù)我們可以將類的構造函數(shù)中的參數(shù)分為以下幾組基礎參數(shù)我們訓練的模型存放到指定的目錄中??赐昴P偷臉嬙旌瘮?shù)后,我們大概知道和端的模型各對應什么樣的模型,模型需要輸入什么樣的參數(shù)。 Wide and deep 模型是 TensorFlow 在 2016 年 6 月左右發(fā)布的一類用于分類和回歸的模型,并應用到了 Google Play ...

    opengps 評論0 收藏0

發(fā)表評論

0條評論

chaosx110

|高級講師

TA的文章

閱讀更多
最新活動
閱讀需要支付1元查看
<