diff --git a/lib/storage/metadata/mongoclient/MongoClientInterface.ts b/lib/storage/metadata/mongoclient/MongoClientInterface.ts index 34bcd192b..0a15e4939 100644 --- a/lib/storage/metadata/mongoclient/MongoClientInterface.ts +++ b/lib/storage/metadata/mongoclient/MongoClientInterface.ts @@ -466,21 +466,28 @@ class MongoClientInterface { // "constants.usersBucket" and "PENSIEVE" since it has already // been created if (bucketName !== constants.usersBucket && bucketName !== PENSIEVE) { - return this.db!.createCollection(bucketName).then(() => { - if (this.shardCollections) { - const cmd = { - shardCollection: `${this.database}.${bucketName}`, - key: { _id: 1 }, - }; - return this.adminDb!.command(cmd, {}) - .then(() => cb(null)) - .catch(err => { - log.error('createBucket: enabling sharding', { error: err }); - return cb(errors.InternalError); - }); - } - return cb(null); - }); + return this.db!.createCollection(bucketName) + .catch(err => { + if (err.codeName === 'NamespaceExists') { + return; + } + throw err; + }) + .then(() => { + if (this.shardCollections) { + const cmd = { + shardCollection: `${this.database}.${bucketName}`, + key: { _id: 1 }, + }; + return this.adminDb!.command(cmd, {}) + .then(() => cb(null)) + .catch(err => { + log.error('createBucket: enabling sharding', { error: err }); + return cb(errors.InternalError); + }); + } + return cb(null); + }); } return cb(null); }) diff --git a/tests/unit/storage/metadata/mongoclient/MongoClientInterface.spec.js b/tests/unit/storage/metadata/mongoclient/MongoClientInterface.spec.js index 0323e3647..720d1a71a 100644 --- a/tests/unit/storage/metadata/mongoclient/MongoClientInterface.spec.js +++ b/tests/unit/storage/metadata/mongoclient/MongoClientInterface.spec.js @@ -1900,6 +1900,26 @@ describe('MongoClientInterface, createBucket', () => { }); }); + it('should succeed when createCollection returns NamespaceExists', done => { + const bucketName = 'test-bucket-namespace-exists'; + const nsExistsError = Object.assign( + new Error('Collection already exists. NS: metadata.mpuShadowBucketbucket1'), + { codeName: 'NamespaceExists', code: 48 }, + ); + const originalCreateCollection = client.db.createCollection; + client.db.createCollection = () => Promise.reject(nsExistsError); + + client.createBucket(bucketName, baseBucket, logger, err => { + client.db.createCollection = originalCreateCollection; + try { + assert.ifError(err); + done(); + } catch (assertionError) { + done(assertionError); + } + }); + }); + it('should handle modifiedCount 0 and upsertedCount 0 in createBucket', done => { const bucketName = 'test-bucket-createbucket';