[英]Why is MongoDB creating indexes for fields that don't exist within that schema?
TL;DR: I am having trouble with my mongo database seemingly creating indexes for my boards collection from fields from my users collection which is causing E11000 errors when I try to create a new board. TL; DR:我的mongo数据库似乎在从用户集合的字段中为我的木板集合创建索引时遇到麻烦,这在尝试创建新的木板时导致E11000错误。
I am building a kanban board (like Jira) and have board, task and user collections (entity relationship diagram : https://imgur.com/a/Nu6Eg91 ). 我正在构建一个看板(如Jira),并具有板,任务和用户集合(实体关系图: https : //imgur.com/a/Nu6Eg91 )。 All of the collections work fine following a .dropIndexes().
遵循.dropIndexes(),所有集合都可以正常工作。 However, when I have been working with tasks via the UI on one board, when I try to create another board I get this E11000 error:
但是,当我一直通过一个板上的UI处理任务时,当我尝试创建另一个板上时,出现此E11000错误:
{ MongoError: insertDocument :: caused by :: 11000 E11000 duplicate key error index: kanban.boards.$username_1 dup key: { : null }
at Function.create (/home/ubuntu/workspace/kanban v1.0/node_modules/mongodb-core/lib/error.js:43:12)
at toError (/home/ubuntu/workspace/kanban v1.0/node_modules/mongodb/lib/utils.js:149:22)
at coll.s.topology.insert (/home/ubuntu/workspace/kanban v1.0/node_modules/mongodb/lib/operations/collection_ops.js:859:39)
at /home/ubuntu/workspace/kanban v1.0/node_modules/mongodb-core/lib/connection/pool.js:532:18
at _combinedTickCallback (internal/process/next_tick.js:73:7)
at process._tickCallback (internal/process/next_tick.js:104:9)
driver: true,
name: 'MongoError',
index: 0,
code: 11000,
errmsg: 'insertDocument :: caused by :: 11000 E11000 duplicate key error index: kanban.boards.$username_1 dup key: { : null }' }
This appears to be because of a null value in the username field in the board schema but there is no such field in this model as can be seen below: 这似乎是由于板模式中的用户名字段中的值为空,但是此模型中没有这样的字段,如下所示:
// Board Schema
// Config
var mongoose = require("mongoose"),
passportLocalMongoose = require("passport-local-mongoose");
// Create Board Schema
var BoardSchema = new mongoose.Schema({
user: {type: mongoose.Schema.Types.ObjectId, ref: "User", unique: true, sparse: true},
todo: [{type: mongoose.Schema.Types.ObjectId, ref: "Task"}],
inProgress: [{type: mongoose.Schema.Types.ObjectId, ref: "Task"}],
testing: [{type: mongoose.Schema.Types.ObjectId, ref: "Task"}],
completed: [{type: mongoose.Schema.Types.ObjectId, ref: "Task"}]
});
// Validation
BoardSchema.plugin(passportLocalMongoose);
// Export Model
module.exports = mongoose.model("Board", BoardSchema);
Here is an example board in the db: 这是数据库中的示例板:
{ "_id" : ObjectId("5c65767c8977670a2366ffe5"),
"todo" : [ ObjectId("5c657a1e451bc80ac315ef35"), ObjectId("5c657a4a451bc80ac315ef37"), ObjectId("5c657a76451bc80ac315ef39") ],
"inProgress" : [ ObjectId("5c6579e1451bc80ac315ef31"), ObjectId("5c657a03451bc80ac315ef33"), ObjectId("5c657a3a451bc80ac315ef36"), ObjectId("5c657a8a451bc80ac315ef3a") ],
"testing" : [ ObjectId("5c657a9a451bc80ac315ef3b") ],
"completed" : [ ObjectId("5c657a60451bc80ac315ef38"), ObjectId("5c65bb8f6f5b731aec27b4a7"), ObjectId("5c65bba06f5b731aec27b4a8"), ObjectId("5c657a11451bc80ac315ef34") ],
"user" : ObjectId("5c65767b8977670a2366ffe4"),
"__v" : 51 }
For some reason, I think a username index has been created by the database for the boards schema. 出于某种原因,我认为数据库已经为板模式创建了用户名索引。 The indexes for the boards collection are below:
木板集合的索引如下:
[
{
"v" : 1,
"key" : {
"_id" : 1
},
"name" : "_id_",
"ns" : "kanban.boards"
},
{
"v" : 1,
"key" : {
"user" : 1
},
"name" : "user_1",
"ns" : "kanban.boards",
"background" : true
},
{
"v" : 1,
"unique" : true,
"key" : {
"username" : 1
},
"name" : "username_1",
"ns" : "kanban.boards",
"background" : true
}
]
For completeness, here is the user schema: 为了完整起见,以下是用户架构:
// User Schema
// Config
var mongoose = require("mongoose"),
uniqueValidator = require("mongoose-unique-validator"),
passportLocalMongoose = require("passport-local-mongoose");
// Create User Schema
var UserSchema = new mongoose.Schema({
username: {type: String, required: true, unique: true},
password: String
});
// Validation and Hash/Salt PW
UserSchema.plugin(uniqueValidator, {message: "A user is already registered with that {PATH}."});
UserSchema.plugin(passportLocalMongoose);
// Export Model
module.exports = mongoose.model("User", UserSchema);
The code causing the error is in the signup route, part of which is shown below: 导致错误的代码在注册路由中,其一部分如下所示:
// create new board for user
var newBoard = {
user: user._id,
todo: [],
inProgress: [],
testing: [],
review: [],
completed: [],
};
Board.create(newBoard, function(err, board) {
if(err) {
console.log(err);
req.flash("error", "Uh oh! Something went wrong.");
return res.redirect("/");
}
// authenticate user
passport.authenticate("local")(req, res, function() {
req.flash("success", "Welcome to your Kanban board, " + user.username + ".");
return res.redirect("/board");
});
});
I don't understand why this index is being created (or indeed if this is what is causing the E11000 errors but I'm pretty sure it is). 我不明白为什么要创建此索引(或者确实是导致E11000错误的原因,但是我很确定是这样)。 Apologies for the spam of code, I am quite inexperienced with using mongo and so don't know what is relevant and what is not.
对垃圾邮件的代码表示歉意,我对使用mongo并不熟悉,因此不知道什么是相关的,什么是不相关的。 I have built mongo databases with multiple collections before but not with a link collection as I have here so I can't work out what is going wrong.
我之前用多个集合构建过mongo数据库,但没有像我在这里那样用链接集合构建过,所以我无法弄清楚出了什么问题。 Please let me know if I have missed anything that is useful or important.
如果我错过了任何有用或重要的内容,请告诉我。 Thanks.
谢谢。
I'm not 100% sure if this is the issue, but you should remove the unique: true
and sparse: true
from the BoardSchema.user parameter. 我不确定这是否是问题的100%,但是您应该从BoardSchema.user参数中删除
unique: true
和sparse: true
。 Since you declare those in the UserSchema, you shouldn't also need them in the BoardSchema. 由于您是在UserSchema中声明的,因此,在BoardSchema中也不需要它们。
EDIT: You might also have to manually delete the index after completing this first step. 编辑:完成第一步后,您可能还必须手动删除索引。
问题是我只将护照本地猫鼬插入到董事会架构中,而这只应位于用户架构中。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.