From 44be4b491cb8a194aaddb76ac03bb3467fbcc91a Mon Sep 17 00:00:00 2001 From: "Navy.gif" Date: Wed, 16 Aug 2023 02:13:38 +0300 Subject: [PATCH] bugfix make sure a collection exists before trying to create an index on it --- src/MongoDB.ts | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/src/MongoDB.ts b/src/MongoDB.ts index aa4aff0..4ab8afa 100644 --- a/src/MongoDB.ts +++ b/src/MongoDB.ts @@ -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))