Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 | import { Collection, CreateIndexesOptions, Document, IndexSpecification } from 'mongodb';
export async function getIndexNames(collection: Collection<Document>, indexes: string[]) {
const listIdx = await collection.indexes();
return listIdx
.filter(Boolean)
.filter((i) => indexes.includes(i.name))
.map((index) => index.name);
}
export async function dropOldIndexes(collection: Collection<Document>, indexes: string[]) {
const indexNames = await getIndexNames(collection, indexes);
for (const indexName of indexNames) {
await collection.dropIndex(indexName);
}
}
export async function createIndex(
collection: Collection<Document>,
indexSpec: IndexSpecification,
options: CreateIndexesOptions,
): Promise<void> {
const isIndexExisted = options.name ? await collection.indexExists(options.name) : undefined;
Iif (!isIndexExisted) {
await collection.createIndex(indexSpec, options);
}
}
|