我试图在MongoDB上创建自定义索引,但是当我通过命令行检查mongodb-compass时,它没有创建。
这是 Mongoose 模型
import Mongoose from "mongoose";
// import encrypt from "mongoose-encryption";
import dotenv from "dotenv";
import path from "path";
const __dirname = path.resolve();
dotenv.config({ path: __dirname + "/.env" });
const signupSchema = new Mongoose.Schema(
{
name: { type: String, required: [true, "name required"] },
email: {
type: String,
required: [true, "email required"],
unique: true,
index: true,
},
phoneNumber: {
type: Number,
required: [true, "phone number required"],
unique: true,
index: true,
},
currentLocation: {
type: String,
required: [false, "current location required"],
},
password: { type: String, required: [true, "password requred"] },
createdDate:{
type:Date,
default:Date.now
},
},
{
timestamps: true,
timeseries: true,
}
);
// const secretkey = process.env.MY_SECRET_KEY;
// signupSchema.plugin(encrypt, { secret: secretkey });
signupSchema.index({email:1,type:1})
const SignUpModel = Mongoose.model("SignUp", signupSchema);
export default SignUpModel;
我的索引. js
mongoose.connect(process.env.MONGODB_URL || 'mongodb://localhost/naukridb',{
useNewUrlParser:true,
useUnifiedTopology:true,
})
mongoose.pluralize(null)
查看未创建索引的指南针图像(即使在模型中定义):
这是我要检查的命令行:
没有对电子邮件和电话号码创建索引,唯一示例也不起作用。
久经考验:
Mongoose create multiple indexes
Mongoose not creating index
Mongoose Not Creating Indexes
Mongoose does not create text index
mongoose index don't create
Mongoose Secondary Index is not created
但是没有一个解决方案是有效的。我的MongoDB版本是5.0.10。
1条答案
按热度按时间b91juud31#
看起来这是由于在架构上设置
timeseries : true
造成的。我不确定这是Mongoose问题、MongoDB问题还是预期行为。您可以使用
ensureIndexes
来确保建立次要索引: