我在node / express / mongoose项目中有两种模式-User&Conversation-。我已经在每个架构上添加了索引。在(免费的)Mongo Atlas集群中检查索引时,可以看到已创建了会话的索引。但是,用户不创建所需的索引。
这是我的用户架构:
const mongoose = require("mongoose");
const Schema = mongoose.Schema;
const userSchema = new Schema(
{
username: { type: String, required: true, unique: true },
email: { type: String, required: true, unique: true },
password: { type: String, required: true },
age: { type: String},
location: {
type: { type: String, enum: ["Point"], default: "Point" },
coordinates: { type: [Number], default: [0, 0] },
},
},
{ timestamps: true }
);
userSchema.index({ age: 1, location: "2dsphere", username: 1 });
module.exports = mongoose.model("User", userSchema);
在Mongo Atlas中,用户集合上有两个索引:(id
当然)和email
。我从来没有要求索引电子邮件。age
,location
和username
均未编制索引。如何解决这个问题?
由于猫鼬的文档(https://mongoosejs.com/docs/guide.html#indexes),您可以在架构中的路径级别进行字段级别的索引。例如,如果您需要索引年龄,则可以在架构中执行以下操作:
age: { type: String, index: true},
您正在尝试在由2dsphere索引组成的代码中创建复合索引。我想这不是您想要的。
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句