diff --git a/packages/cli-repl/test/e2e-direct.spec.ts b/packages/cli-repl/test/e2e-direct.spec.ts index eb93620c9a..ca3dd4bab5 100644 --- a/packages/cli-repl/test/e2e-direct.spec.ts +++ b/packages/cli-repl/test/e2e-direct.spec.ts @@ -29,6 +29,8 @@ describe('e2e direct connection', () => { }); context('after rs.initiate()', () => { + let dbname: string; + before(async() => { const replSetConfig = { _id: replSetId, @@ -50,6 +52,15 @@ describe('e2e direct connection', () => { shell.assertContainsOutput(`me: '${await rs0.hostport()}'`); shell.assertContainsOutput(`setName: '${replSetId}'`); }); + dbname = `test-${Date.now()}-${(Math.random() * 100000) | 0}`; + await shell.executeLine(`use ${dbname}`); + await shell.executeLine('db.testcollection.insertOne({})'); + shell.writeInputLine('exit'); + }); + after(async() => { + const shell = TestShell.start({ args: [await rs0.connectionString()] }); + await shell.executeLine(`db.getSiblingDB("${dbname}").dropDatabase()`); + shell.writeInputLine('exit'); }); context('connecting to secondary members directly', () => { @@ -103,6 +114,14 @@ describe('e2e direct connection', () => { expect(await shell.executeLine('show collections')).to.include('system.version'); expect(await shell.executeLine('show dbs')).to.include('admin'); }); + it('autocompletes collection names', async() => { + const shell = TestShell.start({ args: [`${await rs1.connectionString()}/${dbname}`], forceTerminal: true }); + await shell.waitForPrompt(); + shell.writeInput('db.testc\u0009\u0009'); + await eventually(() => { + shell.assertContainsOutput('db.testcollection'); + }); + }); }); context('connecting to primary', () => { @@ -139,6 +158,14 @@ describe('e2e direct connection', () => { expect(await shell.executeLine('show collections')).to.include('system.version'); expect(await shell.executeLine('show dbs')).to.include('admin'); }); + it('autocompletes collection names', async() => { + const shell = TestShell.start({ args: [`${await rs1.connectionString()}/${dbname}`], forceTerminal: true }); + await shell.waitForPrompt(); + shell.writeInput('db.testc\u0009\u0009'); + await eventually(() => { + shell.assertContainsOutput('db.testcollection'); + }); + }); }); }); }); diff --git a/packages/shell-api/src/database.ts b/packages/shell-api/src/database.ts index 693e6a1c85..0d0c1fdde2 100644 --- a/packages/shell-api/src/database.ts +++ b/packages/shell-api/src/database.ts @@ -150,7 +150,7 @@ export default class Database extends ShellApiClass { async _getCollectionNamesForCompletion(): Promise { return await Promise.race([ (async() => { - return await this._getCollectionNames(); + return await this._getCollectionNames({ readPreference: 'primaryPreferred' }); })(), (async() => { // 200ms should be a good compromise between giving the server a chance diff --git a/packages/shell-api/src/shell-internal-state.spec.ts b/packages/shell-api/src/shell-internal-state.spec.ts index 8868ebe261..d029586884 100644 --- a/packages/shell-api/src/shell-internal-state.spec.ts +++ b/packages/shell-api/src/shell-internal-state.spec.ts @@ -51,7 +51,10 @@ describe('ShellInternalState', () => { serviceProvider.listCollections .resolves([ { name: 'coll1' }, { name: 'coll2' } ]); expect(run('db = db.getSiblingDB("moo"); db.getName()')).to.equal('moo'); - expect(serviceProvider.listCollections.calledWith('moo', {}, { nameOnly: true })).to.equal(true); + expect(serviceProvider.listCollections.calledWith('moo', {}, { + readPreference: 'primaryPreferred', + nameOnly: true + })).to.equal(true); }); }); diff --git a/packages/shell-api/src/shell-internal-state.ts b/packages/shell-api/src/shell-internal-state.ts index c7ef94a1bc..bc5984e94e 100644 --- a/packages/shell-api/src/shell-internal-state.ts +++ b/packages/shell-api/src/shell-internal-state.ts @@ -133,7 +133,7 @@ export default class ShellInternalState { this.context.sh = new Shard(this.currentDb); this.fetchConnectionInfo().catch(err => this.messageBus.emit('mongosh:error', err)); // Pre-fetch for autocompletion. - this.currentDb._getCollectionNames().catch(err => this.messageBus.emit('mongosh:error', err)); + this.currentDb._getCollectionNamesForCompletion().catch(err => this.messageBus.emit('mongosh:error', err)); return newDb; }