make sure a collection exists before trying to create an index on it
This commit is contained in:
Erik 2023-08-16 02:13:38 +03:00
parent 3d877bc1c9
commit 44be4b491c
Signed by: Navy.gif
GPG Key ID: 2532FBBB61C65A68

View File

@ -386,6 +386,10 @@ class MongoDB
if (!(index instanceof Array))
index = [ index ];
const collections = await this.#db.collections();
if (!collections.some((coll) => coll.namespace.split('.')[1] === collection))
await this.#db.createCollection(collection);
const indexes = await this.#db.collection(collection).indexes();
const existing = indexes.find(idx => idx.name.startsWith(index));
if (existing && this.#indexesEqual(existing, options))