node.js - 猫鼬不创建索引
问题描述
我在 node/express/mongoose 项目中有两个模式 - 用户和对话。我已经在每个模式上添加了索引。在检查我的(免费)Mongo Atlas Cluster 中的索引时,我可以看到 Conversation 的索引已创建。但是,用户不会创建所需的索引。
这是我的用户架构:
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 索引组成的复合索引。我想这不是你想要的。
推荐阅读
- recaptcha - 闪电社区中的谷歌recaptcha图像选择器样式
- r - 提取文本列中特定单词后的第一个数字
- elasticsearch - 如何使用文件系统移动弹性搜索索引?
- python-3.x - numba jitclass 中的对象比较
- javascript - 单击 php 按钮以全屏显示网页内容
- python - 为什么 Kivy 在我需要时不刷新/更新我的屏幕?
- html - 具有滚动功能的 Wordpress 页面菜单
- qt - QT-QML:如何创建图像(线)并对其进行处理
- css - 使用 CSS 在 Juypter 实验室控制台中自定义 Pandas 数据框的字体
- c# - EFCore.BulkExtensions - 使用 pimarykey 批量更新