【发布时间】:2021-10-01 23:35:15
【问题描述】:
这类问题已经问过很多次了。我已经尝试了每一个答案。在大多数情况下,答案就像删除由 mongo 自动创建的用户索引。我做了太多次了。但是每次我在服务器上发出请求时,它都会再次创建(索引)。
当我写db.compnies.getIndexes().我得到
[
{
"v" : 2,
"key" : {
"_id" : 1
},
"name" : "_id_"
},
{
"v" : 2,
"unique" : true,
"key" : {
"username" : 1
},
"name" : "username_1",
"background" : true
}
]
被db.compnies.dropIndexes({"username":1})删除后。我明白了
[ { "v" : 2, "key" : { "_id" : 1 }, "name" : "_id_" } ] 作为db.compnies.getIndexes()。
在重复上述过程的每个新请求后。自过去两天以来我一直面临此错误。我无法提交我的数据。 请帮我。 谢谢。
这是我的用户模型:
const mongoose = require("mongoose");
const passportLocalMongoose = require("passport-local-mongoose");
// const findOrCreate = require('mongoose-findorcreate');
const logoSchema=new mongoose.Schema({
url:String,
filename:String
});
const UserSchema = new mongoose.Schema({
email: {
type: String,
},
username:{
type:String,
unique:false
},
// password:{
// type:String,
// },
googleId : {
type : String,
} ,
name : {
type : String,
} ,
firstName : {
type : String,
} ,
lastName : {
type : String,
},
age:{
type:String
},
compny:{
type:String
},
// logo :[logoSchema],
logo: {
type: String,
required: [true, "Uploaded file must have a name"],
},
createdAt:{
type: Date,
default : Date.now
}
});
UserSchema.plugin(passportLocalMongoose);
// UserSchema.plugin(findOrCreate);
const User = mongoose.model('User', UserSchema);
module.exports = User;
这是我的公司模型:
const mongoose=require("mongoose");
const passportLocalMongoose = require("passport-local-mongoose");
// const ImageSchema = new mongoose.Schema({
// url: String,
// filename: String
// });
const compnySchema= new mongoose.Schema({
name:{
type:String,
// required:true
},
location:{
type:String,
// required:true
},
category:{
type:String,
// required:true
},
about:{
type:String
},
logo: {
type: String,
required: [true, "Uploaded file must have a name"],
},
count:{
type:Number
}
});
compnySchema.plugin(passportLocalMongoose);
const Compny= mongoose.model("Compny", compnySchema);
module.exports=Compny;
【问题讨论】:
-
您的问题不清楚。您是在问“为什么我会收到重复的密钥错误?”或者您是在问“为什么这个过程会创建一个对字段 'username' 具有唯一约束的索引?”
-
我想回答这两个问题。谢谢。
标签: javascript node.js mongodb mongoose ejs