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://specialneedsforspecialkids.com/yun/19338.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...
摘要:復合唯一索引也可以創建復合的唯一索引。中的稀疏索引與關系型數據庫中的稀疏索引是完全不同的概念。但是這里不會指明索引是否是多鍵索引。上一篇文章指南使用和何時不應該使用索引下一篇文章指南特殊的索引和集合固定集合索引全文本索引 上一篇文章:MongoDB指南---12、使用explain()和hint()、何時不應該使用索引下一篇文章:MongoDB指南---14、特殊的索引和集合:固定集合...
摘要:復合唯一索引也可以創建復合的唯一索引。中的稀疏索引與關系型數據庫中的稀疏索引是完全不同的概念。但是這里不會指明索引是否是多鍵索引。上一篇文章指南使用和何時不應該使用索引下一篇文章指南特殊的索引和集合固定集合索引全文本索引 上一篇文章:MongoDB指南---12、使用explain()和hint()、何時不應該使用索引下一篇文章:MongoDB指南---14、特殊的索引和集合:固定集合...
摘要:備份備份到文件名為的文件嘗試重復執行備份會覆蓋第一次的備份文件立即恢復中刪除了一條記錄后執行,刪除的行又出來了,已經存在的行報錯刪除后恢復沒有任何異常恢復成功 備份 mongodump --archive=gt_ut.archive --db gt_ut 2018-03-22T18:11:33.555+0800 writing gt_ut.gt_counting_data to ...
摘要:我們先看看的初始化函數的完整定義,看構造一個模型可以輸入哪些參數我們可以將類的構造函數中的參數分為以下幾組基礎參數我們訓練的模型存放到指定的目錄中。看完模型的構造函數后,我們大概知道和端的模型各對應什么樣的模型,模型需要輸入什么樣的參數。 Wide and deep 模型是 TensorFlow 在 2016 年 6 月左右發布的一類用于分類和回歸的模型,并應用到了 Google Play ...
閱讀 1633·2021-11-02 14:42
閱讀 527·2021-10-18 13:24
閱讀 958·2021-10-12 10:12
閱讀 1820·2021-09-02 15:41
閱讀 3209·2019-08-30 15:56
閱讀 2880·2019-08-29 16:09
閱讀 2063·2019-08-29 11:13
閱讀 3623·2019-08-28 18:06