diff --git a/src/cursor/aggregation_cursor.ts b/src/cursor/aggregation_cursor.ts index ca206826c98..d774f823ea2 100644 --- a/src/cursor/aggregation_cursor.ts +++ b/src/cursor/aggregation_cursor.ts @@ -1,6 +1,7 @@ import { AggregateOperation, AggregateOptions } from '../operations/aggregate'; import { AbstractCursor, assertUninitialized } from './abstract_cursor'; import { executeOperation, ExecutionResult } from '../operations/execute_operation'; +import { mergeOptions } from '../utils'; import type { Document } from '../bson'; import type { Sort } from '../sort'; import type { Topology } from '../sdam/topology'; @@ -52,9 +53,9 @@ export class AggregationCursor extends AbstractCursor { } clone(): AggregationCursor { + const clonedOptions = mergeOptions({}, this[kOptions]); return new AggregationCursor(this[kParent], this.topology, this.namespace, this[kPipeline], { - ...this[kOptions], - ...this.cursorOptions + ...clonedOptions }); } diff --git a/src/cursor/find_cursor.ts b/src/cursor/find_cursor.ts index b760deccbff..f217b2abda2 100644 --- a/src/cursor/find_cursor.ts +++ b/src/cursor/find_cursor.ts @@ -4,6 +4,7 @@ import type { ExplainVerbosityLike } from '../explain'; import { CountOperation, CountOptions } from '../operations/count'; import { executeOperation, ExecutionResult } from '../operations/execute_operation'; import { FindOperation, FindOptions } from '../operations/find'; +import { mergeOptions } from '../utils'; import type { Hint } from '../operations/operation'; import type { CollationOptions } from '../operations/command'; import type { Topology } from '../sdam/topology'; @@ -52,9 +53,9 @@ export class FindCursor extends AbstractCursor { } clone(): FindCursor { + const clonedOptions = mergeOptions({}, this[kBuiltOptions]); return new FindCursor(this.topology, this.namespace, this[kFilter], { - ...this[kBuiltOptions], - ...this.cursorOptions + ...clonedOptions }); } diff --git a/test/functional/cursor.test.js b/test/functional/cursor.test.js index 4afae2eb16a..f33ebd1063c 100644 --- a/test/functional/cursor.test.js +++ b/test/functional/cursor.test.js @@ -3755,6 +3755,65 @@ describe('Cursor', function () { } ); + describe('#clone', function () { + let client; + let db; + let collection; + + beforeEach(function () { + client = this.configuration.newClient({ w: 1 }); + + return client.connect().then(client => { + db = client.db(this.configuration.db); + collection = db.collection('test_coll'); + }); + }); + + afterEach(function () { + return client.close(); + }); + + context('when executing on a find cursor', function () { + it('removes the existing session from the cloned cursor', function () { + const docs = [{ name: 'test1' }, { name: 'test2' }]; + return collection.insertMany(docs).then(() => { + const cursor = collection.find({}, { batchSize: 1 }); + return cursor + .next() + .then(doc => { + expect(doc).to.exist; + const clonedCursor = cursor.clone(); + expect(clonedCursor.cursorOptions.session).to.not.exist; + expect(clonedCursor.session).to.not.exist; + }) + .finally(() => { + return cursor.close(); + }); + }); + }); + }); + + context('when executing on an aggregation cursor', function () { + it('removes the existing session from the cloned cursor', function () { + const docs = [{ name: 'test1' }, { name: 'test2' }]; + return collection.insertMany(docs).then(() => { + const cursor = collection.aggregate([{ $match: {} }], { batchSize: 1 }); + return cursor + .next() + .then(doc => { + expect(doc).to.exist; + const clonedCursor = cursor.clone(); + expect(clonedCursor.cursorOptions.session).to.not.exist; + expect(clonedCursor.session).to.not.exist; + }) + .finally(() => { + return cursor.close(); + }); + }); + }); + }); + }); + it('should return a promise when no callback supplied to forEach method', function () { const configuration = this.configuration; const client = configuration.newClient({ w: 1 }, { maxPoolSize: 1 });