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")
文章版權(quán)歸作者所有,未經(jīng)允許請(qǐng)勿轉(zhuǎn)載,若此文章存在違規(guī)行為,您可以聯(lián)系管理員刪除。
轉(zhuǎn)載請(qǐng)注明本文地址:http://specialneedsforspecialkids.com/yun/108353.html
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...
摘要:復(fù)合唯一索引也可以創(chuàng)建復(fù)合的唯一索引。中的稀疏索引與關(guān)系型數(shù)據(jù)庫中的稀疏索引是完全不同的概念。但是這里不會(huì)指明索引是否是多鍵索引。上一篇文章指南使用和何時(shí)不應(yīng)該使用索引下一篇文章指南特殊的索引和集合固定集合索引全文本索引 上一篇文章:MongoDB指南---12、使用explain()和hint()、何時(shí)不應(yīng)該使用索引下一篇文章:MongoDB指南---14、特殊的索引和集合:固定集合...
摘要:復(fù)合唯一索引也可以創(chuàng)建復(fù)合的唯一索引。中的稀疏索引與關(guān)系型數(shù)據(jù)庫中的稀疏索引是完全不同的概念。但是這里不會(huì)指明索引是否是多鍵索引。上一篇文章指南使用和何時(shí)不應(yīng)該使用索引下一篇文章指南特殊的索引和集合固定集合索引全文本索引 上一篇文章:MongoDB指南---12、使用explain()和hint()、何時(shí)不應(yīng)該使用索引下一篇文章:MongoDB指南---14、特殊的索引和集合:固定集合...
摘要:備份備份到文件名為的文件嘗試重復(fù)執(zhí)行備份會(huì)覆蓋第一次的備份文件立即恢復(fù)中刪除了一條記錄后執(zhí)行,刪除的行又出來了,已經(jīng)存在的行報(bào)錯(cuò)刪除后恢復(fù)沒有任何異常恢復(fù)成功 備份 mongodump --archive=gt_ut.archive --db gt_ut 2018-03-22T18:11:33.555+0800 writing gt_ut.gt_counting_data to ...
摘要:我們先看看的初始化函數(shù)的完整定義,看構(gòu)造一個(gè)模型可以輸入哪些參數(shù)我們可以將類的構(gòu)造函數(shù)中的參數(shù)分為以下幾組基礎(chǔ)參數(shù)我們訓(xùn)練的模型存放到指定的目錄中。看完模型的構(gòu)造函數(shù)后,我們大概知道和端的模型各對(duì)應(yīng)什么樣的模型,模型需要輸入什么樣的參數(shù)。 Wide and deep 模型是 TensorFlow 在 2016 年 6 月左右發(fā)布的一類用于分類和回歸的模型,并應(yīng)用到了 Google Play ...
閱讀 369·2023-04-25 16:38
閱讀 1482·2021-09-26 09:46
閱讀 3326·2021-09-08 09:35
閱讀 2778·2019-08-30 12:54
閱讀 3248·2019-08-29 17:06
閱讀 1017·2019-08-29 14:06
閱讀 3344·2019-08-29 13:00
閱讀 3466·2019-08-28 17:53