mongodb - FeathersJS 为集合创建复合索引
问题描述
FeathersJS 服务有没有办法为 MongoDB 数据库创建复合索引?
解决方案
找到你创建 MongoDB 客户端的地方。如果您使用 FeatherJS 生成器,-它可能位于src/mongodb.js
. 在客户端已经可用的承诺结果中, - 获取集合并添加索引:
// src/app.ts
import mongodb from './mongodb';
...
app.configure(mongodb);
...
// src/mongodb.ts
export default function (app: Application): void {
const connection = app.get(`mongodb`);
const database = connection.substr(connection.lastIndexOf(`/`) + 1);
const mongoClient:Promise<Db> = MongoClient.connect(connection, {
useNewUrlParser: true, useUnifiedTopology: true
}).then((client:MongoClient) => {
const db:Db = client.db(database);
try {
db.collection(`users`).createIndex({ name: 1 });
} catch(error:any) {
console.error(`mongodb: failed to created indexes, error: `, error);
}
return db;
});
app.set(`mongoClient`, mongoClient);
}
推荐阅读
- testing - Testcafe - 如何断言两个不同的 location.pathname
- c# - PostAsync 删除“+”字符
- angular - 如何在角度单元测试中访问@Input 属性
- reactjs - 如何将我的“css”和“js”文件包含在我的 React 项目中的 index.html 中定义的路径中?
- c++ - 使用 yaml-cpp 解析 YAML 文件
- graph - 如何打破 SPARQL 中的路径扩展?
- python - 如何将总和值分组放入新列 pandas
- reactjs - 自动完成 MaterialUI - 如何在分组自动完成中设置默认值
- github - 我的 github 帐户被标记为 spsam,我不知道为什么,请有人告诉我如何恢复?
- elasticsearch - journalbeat 未将日志发送到 ElasticSearch 或未在 Kubernetes 的控制台上输出日志