FeathersJS服务有没有办法为MongoDB创建复合索引?
找到创建MongoDB客户端的位置。如果您使用的是FeatherJS生成器,那么它可能位于src/mongodb.js
。在客户端已经可用的promise结果中,获取集合并添加索引:
// 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);
}