Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion packages/cli-repl/src/cli-repl.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -191,7 +191,7 @@ describe('CliRepl', () => {

it('returns the list of available config options when asked to', () => {
expect(cliRepl.listConfigOptions()).to.deep.equal([
'batchSize',
'displayBatchSize',
'enableTelemetry',
'snippetIndexSourceURLs',
'snippetRegistryURL',
Expand Down
10 changes: 3 additions & 7 deletions packages/i18n/src/locales/en_US.ts
Original file line number Diff line number Diff line change
Expand Up @@ -251,7 +251,7 @@ const translations: Catalog = {
description: 'Deprecated. The shell provides auto-formatting so this method is no longer useful'
},
batchSize: {
description: 'Specifies the number of documents that mongosh displays at once.',
description: 'Specifies the number of documents to return in each batch of the response from the MongoDB instance.',
example: 'db.collection.aggregate(pipeline, options).batchSize(10)'
},
projection: {
Expand Down Expand Up @@ -711,10 +711,6 @@ const translations: Catalog = {
},
isExhausted: {
description: 'This method is deprecated because because after closing a cursor, the remaining documents in the batch are no longer accessible. If you want to see if the cursor is closed use cursor.isClosed. If you want to see if there are documents left in the batch, use cursor.tryNext. This is a breaking change'
},
batchSize: {
description: 'Specifies the number of documents that mongosh displays at once.',
example: 'db.collection.aggregate(pipeline, options).batchSize(10)'
}
}
}
Expand Down Expand Up @@ -744,7 +740,7 @@ const translations: Catalog = {
},
batchSize: {
link: 'https://docs.mongodb.com/manual/reference/method/cursor.batchSize',
description: 'Specifies the number of documents to return in each batch of the response from the MongoDB instance. In most cases, modifying the batch size will not affect the user or the application, as the mongo shell and most drivers return results as if MongoDB returned a single batch. This also specifies how many entries mongosh displays at once.',
description: 'Specifies the number of documents to return in each batch of the response from the MongoDB instance. In most cases, modifying the batch size will not affect the user or the application, as the mongo shell and most drivers return results as if MongoDB returned a single batch.',
example: 'db.collection.find(query, projection).batchSize(10)'
},
close: {
Expand Down Expand Up @@ -1296,7 +1292,7 @@ const translations: Catalog = {
},
DBQuery: {
help: {
description: 'Deprecated -- use cursor.batchSize(value) or config.set("batchSize", value) instead of DBQuery.batchSize = value',
description: 'Deprecated -- use config.set("displayBatchSize", value) instead of DBQuery.shellBatchSize = value',
}
},
Explainable: {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -496,7 +496,7 @@ describe('worker', () => {
getConfig() {},
setConfig() {},
validateConfig() {},
listConfigOptions() { return ['batchSize']; },
listConfigOptions() { return ['displayBatchSize']; },
onRunInterruptible() {}
};

Expand Down Expand Up @@ -576,9 +576,9 @@ describe('worker', () => {

await init('mongodb://nodb/', {}, { nodb: true });

await evaluate('config.set("batchSize", 200)');
expect(evalListener.validateConfig).to.have.been.calledWith('batchSize', 200);
expect(evalListener.setConfig).to.have.been.calledWith('batchSize', 200);
await evaluate('config.set("displayBatchSize", 200)');
expect(evalListener.validateConfig).to.have.been.calledWith('displayBatchSize', 200);
expect(evalListener.setConfig).to.have.been.calledWith('displayBatchSize', 200);
});
});

Expand Down
13 changes: 7 additions & 6 deletions packages/shell-api/src/abstract-cursor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,16 +18,17 @@ import { CursorIterationResult } from './result';
import { iterate, validateExplainableVerbosity, markAsExplainOutput } from './helpers';

@shellApiClassNoHelp
export abstract class AbstractCursor extends ShellApiWithMongoClass {
export abstract class AbstractCursor<CursorType extends ServiceProviderAggregationCursor | ServiceProviderCursor> extends ShellApiWithMongoClass {
_mongo: Mongo;
abstract _cursor: ServiceProviderAggregationCursor | ServiceProviderCursor;
_cursor: CursorType;

_currentIterationResult: CursorIterationResult | null = null;
_batchSize: number | null = null;
_mapError: Error | null = null;

constructor(mongo: Mongo) {
constructor(mongo: Mongo, cursor: CursorType) {
super();
this._mongo = mongo;
this._cursor = cursor;
}

// Wrap a function with checks before and after that verify whether a .map()
Expand Down Expand Up @@ -63,14 +64,14 @@ export abstract class AbstractCursor extends ShellApiWithMongoClass {

async _it(): Promise<CursorIterationResult> {
const results = this._currentIterationResult = new CursorIterationResult();
await iterate(results, this, this._batchSize ?? await this._mongo._batchSize());
await iterate(results, this, await this._mongo._displayBatchSize());
results.cursorHasMore = !this.isExhausted();
return results;
}

@returnType('this')
batchSize(size: number): this {
this._batchSize = size;
this._cursor.batchSize(size);
return this;
}

Expand Down
19 changes: 12 additions & 7 deletions packages/shell-api/src/aggregation-cursor.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ describe('AggregationCursor', () => {
};
cursor = new AggregationCursor({
_serviceProvider: { platform: ReplPlatform.CLI },
_batchSize: () => 20
_displayBatchSize: () => 20
} as any, wrappee);
});

Expand Down Expand Up @@ -75,7 +75,7 @@ describe('AggregationCursor', () => {

describe('Cursor Internals', () => {
const mongo = {
_batchSize: () => 20
_displayBatchSize: () => 20
} as any;
describe('#close', () => {
let spCursor: StubbedInstance<SPAggregationCursor>;
Expand Down Expand Up @@ -212,8 +212,9 @@ describe('AggregationCursor', () => {
});

describe('toShellResult', () => {
let shellApiCursor;
let i;
let shellApiCursor: AggregationCursor;
let i: number;
let batchSize: number | undefined;

beforeEach(() => {
i = 0;
Expand All @@ -226,6 +227,9 @@ describe('AggregationCursor', () => {
if (prop === 'tryNext') {
return async() => ({ key: i++ });
}
if (prop === 'batchSize') {
return (size: number) => { batchSize = size; };
}
return (target as any)[prop];
}
});
Expand All @@ -243,11 +247,12 @@ describe('AggregationCursor', () => {
expect(i).to.equal(40);
});

it('lets .batchSize() control the output length', async() => {
it('.batchSize() does not control the output length', async() => {
shellApiCursor.batchSize(10);
const result = (await toShellResult(shellApiCursor)).printable;
expect(i).to.equal(10);
expect(result).to.have.nested.property('documents.length', 10);
expect(i).to.equal(20);
expect(batchSize).to.equal(10);
expect(result).to.have.nested.property('documents.length', 20);
});
});
});
Expand Down
7 changes: 2 additions & 5 deletions packages/shell-api/src/aggregation-cursor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,8 @@ import type {
import { AbstractCursor } from './abstract-cursor';

@shellApiClassDefault
export default class AggregationCursor extends AbstractCursor {
_cursor: ServiceProviderAggregationCursor;

export default class AggregationCursor extends AbstractCursor<ServiceProviderAggregationCursor> {
constructor(mongo: Mongo, cursor: ServiceProviderAggregationCursor) {
super(mongo);
this._cursor = cursor;
super(mongo, cursor);
}
}
10 changes: 0 additions & 10 deletions packages/shell-api/src/change-stream-cursor.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -97,16 +97,6 @@ describe('ChangeStreamCursor', () => {
expect(spCursor.next.calledWith()).to.equal(true);
expect(warnSpy.calledOnce).to.equal(true);
});
it('lets .batchSize() control iteration batch size', async() => {
const cursor2 = new ChangeStreamCursor({
tryNext: sinon.stub().resolves({ doc: 1 })
} as any, 'source', {
_internalState: {}
} as Mongo);
cursor2.batchSize(3);
const results = await cursor2._it();
expect(results.documents).to.deep.equal([{ doc: 1 }, { doc: 1 }, { doc: 1 }]);
});
});
describe('integration', () => {
const [ srv0 ] = startTestCluster(['--replicaset'] );
Expand Down
9 changes: 1 addition & 8 deletions packages/shell-api/src/change-stream-cursor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@ export default class ChangeStreamCursor extends ShellApiWithMongoClass {
_cursor: ChangeStream<Document>;
_currentIterationResult: CursorIterationResult | null = null;
_on: string;
_batchSize: number | null = null;

constructor(cursor: ChangeStream<Document>, on: string, mongo: Mongo) {
super();
Expand All @@ -41,7 +40,7 @@ export default class ChangeStreamCursor extends ShellApiWithMongoClass {
throw new MongoshRuntimeError('ChangeStreamCursor is closed');
}
const result = this._currentIterationResult = new CursorIterationResult();
return iterate(result, this, this._batchSize ?? await this._mongo._batchSize());
return iterate(result, this, await this._mongo._displayBatchSize());
}

/**
Expand Down Expand Up @@ -130,10 +129,4 @@ export default class ChangeStreamCursor extends ShellApiWithMongoClass {
pretty(): ChangeStreamCursor {
return this;
}

@returnType('ChangeStreamCursor')
batchSize(size: number): ChangeStreamCursor {
this._batchSize = size;
return this;
}
}
6 changes: 3 additions & 3 deletions packages/shell-api/src/collection.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,7 @@ describe('Collection', () => {
expect(serviceProvider.aggregate).to.have.been.calledWith(
collection._database._name,
collection._name,
[{ $piplelineStage: {} }],
[{ $piplelineStage: {} } ],
{}
);
});
Expand Down Expand Up @@ -169,13 +169,13 @@ describe('Collection', () => {
it('calls serviceProvider.aggregate with pipleline and options', async() => {
await collection.aggregate(
[{ $piplelineStage: {} }],
{ options: true });
{ options: true, batchSize: 10 });

expect(serviceProvider.aggregate).to.have.been.calledWith(
collection._database._name,
collection._name,
[{ $piplelineStage: {} }],
{ options: true }
{ options: true, batchSize: 10 }
);
});

Expand Down
14 changes: 12 additions & 2 deletions packages/shell-api/src/collection.ts
Original file line number Diff line number Diff line change
Expand Up @@ -414,7 +414,12 @@ export default class Collection extends ShellApiWithMongoClass {
this._emitCollectionApiCall('find', { query, options });
const cursor = new Cursor(
this._mongo,
this._mongo._serviceProvider.find(this._database._name, this._name, query, { ...this._database._baseOptions, ...options })
this._mongo._serviceProvider.find(
this._database._name,
this._name,
query,
{ ...this._database._baseOptions, ...options }
)
);

this._mongo._internalState.currentCursor = cursor;
Expand Down Expand Up @@ -470,7 +475,12 @@ export default class Collection extends ShellApiWithMongoClass {
this._emitCollectionApiCall('findOne', { query, options });
return new Cursor(
this._mongo,
this._mongo._serviceProvider.find(this._database._name, this._name, query, { ...this._database._baseOptions, ...options })
this._mongo._serviceProvider.find(
this._database._name,
this._name,
query,
{ ...this._database._baseOptions, ...options }
)
).limit(1).tryNext();
}

Expand Down
10 changes: 5 additions & 5 deletions packages/shell-api/src/cursor.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ describe('Cursor', () => {
};
cursor = new Cursor({
_serviceProvider: { platform: ReplPlatform.CLI },
_batchSize: () => 20
_displayBatchSize: () => 20
} as any, wrappee);
});

Expand Down Expand Up @@ -82,7 +82,7 @@ describe('Cursor', () => {
});
describe('Cursor Internals', () => {
const mongo = {
_batchSize: () => 20
_displayBatchSize: () => 20
} as any;
describe('#addOption', () => {
let spCursor: StubbedInstance<ServiceProviderCursor>;
Expand Down Expand Up @@ -777,11 +777,11 @@ describe('Cursor', () => {
expect(i).to.equal(40);
});

it('lets .batchSize() control the output length', async() => {
it('.batchSize() does not control the output length', async() => {
shellApiCursor.batchSize(10);
const result = (await toShellResult(shellApiCursor)).printable;
expect(i).to.equal(10);
expect(result).to.have.nested.property('documents.length', 10);
expect(i).to.equal(20);
expect(result).to.have.nested.property('documents.length', 20);
});
});
});
Expand Down
13 changes: 2 additions & 11 deletions packages/shell-api/src/cursor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,13 +26,11 @@ import { printWarning } from './deprecation-warning';
import { AbstractCursor } from './abstract-cursor';

@shellApiClassDefault
export default class Cursor extends AbstractCursor {
_cursor: ServiceProviderCursor;
export default class Cursor extends AbstractCursor<ServiceProviderCursor> {
_tailable = false;

constructor(mongo: Mongo, cursor: ServiceProviderCursor) {
super(mongo);
this._cursor = cursor;
super(mongo, cursor);
}

/**
Expand Down Expand Up @@ -75,13 +73,6 @@ export default class Cursor extends AbstractCursor {
return this;
}

@returnType('Cursor')
batchSize(size: number): this {
super.batchSize(size);
this._cursor.batchSize(size);
return this;
}

@returnType('Cursor')
@serverVersions(['3.4.0', ServerVersions.latest])
collation(spec: CollationOptions): Cursor {
Expand Down
11 changes: 11 additions & 0 deletions packages/shell-api/src/database.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -285,6 +285,17 @@ describe('Database', () => {
);
});

it('calls serviceProvider.aggregateDb with explicit batchSize', async() => {
await database.aggregate(
[{ $piplelineStage: {} }], { options: true, batchSize: 10 });

expect(serviceProvider.aggregateDb).to.have.been.calledWith(
database._name,
[{ $piplelineStage: {} }],
{ options: true, batchSize: 10 }
);
});

it('returns an AggregationCursor that wraps the service provider one', async() => {
const toArrayResult = [];
serviceProviderCursor.toArray.resolves(toArrayResult);
Expand Down
10 changes: 5 additions & 5 deletions packages/shell-api/src/dbquery.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,13 @@ export class DBQuery extends ShellApiClass {
this._internalState = internalState;
}

get batchSize(): number | undefined {
return this._internalState.batchSizeFromDBQuery;
get shellBatchSize(): number | undefined {
return this._internalState.displayBatchSizeFromDBQuery;
}

set batchSize(value: number | undefined) {
set shellBatchSize(value: number | undefined) {
printDeprecationWarning(
'DBQuery.batchSize is deprecated, please use \'batchSize\' on the cursor instead: db.coll.find().batchSize(<size>)');
this._internalState.batchSizeFromDBQuery = value;
'DBQuery.shellBatchSize is deprecated, please use config.set("displayBatchSize") instead');
this._internalState.displayBatchSizeFromDBQuery = value;
}
}
2 changes: 1 addition & 1 deletion packages/shell-api/src/helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -533,7 +533,7 @@ export function addHiddenDataProperty<T = any>(target: T, key: string|symbol, va

export async function iterate(
results: CursorIterationResult,
cursor: AbstractCursor | ChangeStreamCursor,
cursor: AbstractCursor<any> | ChangeStreamCursor,
batchSize: number): Promise<CursorIterationResult> {
if (cursor.isClosed()) {
return results;
Expand Down
Loading