有没有办法使用FeathersJS服务为MongoDB DB创建复合索引?
发布于 2021-03-11 05:32:41
找到您创建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);
}https://stackoverflow.com/questions/53280897
复制相似问题