这种类型的问题已经问过很多次了。我已经尝试过每个答案。在大多数情况下,答案就像删除由 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;
答案 0 :(得分:0)
我能够重现您的情况。我重新创建了一个节点程序...
// npm install mongoose
// npm install passport-local-mongoose
var mongoose = require('mongoose');
const passportLocalMongoose = require("passport-local-mongoose");
mongoose.connect("mongodb://localhost:27017/nodetest", {useNewUrlParser: true, useUnifiedTopology: true});
var db = mongoose.connection;
//Bind connection to error event (to get notification of connection errors)
db.on('error', console.error.bind(console, 'MongoDB connection error:'));
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;
let compny = new Compny;
compny.name = "some company";
compny.logo = "some logo";
compny.save(function(err) {
console.log(err);
});
如果我运行这个程序两次,我会得到一个重复的键异常。这是因为护照。程序通行证在字段 username
上强制执行唯一索引。但是您没有名为 username
的字段。这意味着第一条记录将被添加为文档中字段 username
的空值(缺失)。插入的第二个文档的字段 username
也有一个空值(缺失),因此违反了唯一性约束。
如果模式通行证中不存在字段 username
将自动添加唯一索引。但是,如果该字段确实存在于架构中,则不会为该字段添加索引 - 唯一或不唯一。如果字段 username
存在,我想它希望索引的完整定义包括唯一属性应用于架构定义。
建议: