diff --git a/src/operations/distinct.ts b/src/operations/distinct.ts index d006d31133..5fe8585e81 100644 --- a/src/operations/distinct.ts +++ b/src/operations/distinct.ts @@ -1,9 +1,8 @@ import type { Document } from '../bson'; import type { Collection } from '../collection'; -import { MongoCompatibilityError } from '../error'; import type { Server } from '../sdam/server'; import type { ClientSession } from '../sessions'; -import { Callback, decorateWithCollation, decorateWithReadConcern, maxWireVersion } from '../utils'; +import { Callback, decorateWithCollation, decorateWithReadConcern } from '../utils'; import { CommandOperation, CommandOperationOptions } from './command'; import { Aspect, defineAspects } from './operation'; @@ -61,6 +60,12 @@ export class DistinctOperation extends CommandOperation { cmd.maxTimeMS = options.maxTimeMS; } + // we check for undefined specifically here to allow falsy values + // eslint-disable-next-line no-restricted-syntax + if (typeof options.comment !== 'undefined') { + cmd.comment = options.comment; + } + // Do we have a readConcern specified decorateWithReadConcern(cmd, coll, options); @@ -71,13 +76,6 @@ export class DistinctOperation extends CommandOperation { return callback(err); } - if (this.explain && maxWireVersion(server) < 4) { - callback( - new MongoCompatibilityError(`Server ${server.name} does not support explain on distinct`) - ); - return; - } - super.executeCommand(server, session, cmd, (err, result) => { if (err) { callback(err); diff --git a/test/integration/node-specific/comment_with_falsy_values.test.ts b/test/integration/node-specific/comment_with_falsy_values.test.ts index 92c86e1145..9cc8950cec 100644 --- a/test/integration/node-specific/comment_with_falsy_values.test.ts +++ b/test/integration/node-specific/comment_with_falsy_values.test.ts @@ -1,4 +1,6 @@ -import { Long } from '../../../src'; +import { expect } from 'chai'; + +import { Collection, CommandStartedEvent, Long, MongoClient } from '../../../src'; import { TestBuilder, UnifiedTestSuiteBuilder } from '../../tools/utils'; const falsyValues = [0, false, '', Long.ZERO, null, NaN] as const; @@ -173,4 +175,47 @@ describe('Comment with falsy values', () => { .test(testsForChangeStreamsAggregate) .test(testsForGetMore) .run(); + + context('Collection.distinct()', function () { + let client: MongoClient; + let collection: Collection; + let commands: CommandStartedEvent[] = []; + + beforeEach(async function () { + client = this.configuration.newClient({ monitorCommands: true }); + client.on('commandStarted', e => commands.push(e)); + await client.connect(); + collection = await client.db('comment-falsy-values').createCollection('collection'); + commands = []; + }); + + afterEach(async function () { + await collection.drop(); + await client.close(); + }); + + for (const falsyValue of falsyValues) { + it(`distinct should send falsy value ${falsyToString( + falsyValue + )} on the command`, async function () { + await collection.distinct('some-key', {}, { comment: falsyValue }).catch(() => null); + + expect(commands).to.have.lengthOf(1); + const distinctCommand = commands.find(command => command.commandName === 'distinct'); + expect(distinctCommand).to.exist; + + // chai does not narrow types, so TS doesn't know the distinct command exists at this point. + // eslint-disable-next-line @typescript-eslint/no-non-null-assertion + const command = distinctCommand!.command; + + expect(command).to.haveOwnProperty('comment'); + + if (Number.isNaN(falsyValue)) { + expect(command.comment).to.be.NaN; + } else { + expect(command.comment).to.equal(falsyValue); + } + }); + } + }); }); diff --git a/test/spec/crud/unified/distinct-comment.json b/test/spec/crud/unified/distinct-comment.json new file mode 100644 index 0000000000..11bce9ac9d --- /dev/null +++ b/test/spec/crud/unified/distinct-comment.json @@ -0,0 +1,186 @@ +{ + "description": "distinct-comment", + "schemaVersion": "1.0", + "createEntities": [ + { + "client": { + "id": "client0", + "observeEvents": [ + "commandStartedEvent" + ] + } + }, + { + "database": { + "id": "database0", + "client": "client0", + "databaseName": "distinct-comment-tests" + } + }, + { + "collection": { + "id": "collection0", + "database": "database0", + "collectionName": "coll0" + } + } + ], + "initialData": [ + { + "collectionName": "coll0", + "databaseName": "distinct-comment-tests", + "documents": [ + { + "_id": 1, + "x": 11 + }, + { + "_id": 2, + "x": 22 + }, + { + "_id": 3, + "x": 33 + } + ] + } + ], + "tests": [ + { + "description": "distinct with document comment", + "runOnRequirements": [ + { + "minServerVersion": "4.4.14" + } + ], + "operations": [ + { + "name": "distinct", + "object": "collection0", + "arguments": { + "fieldName": "x", + "filter": {}, + "comment": { + "key": "value" + } + }, + "expectResult": [ + 11, + 22, + 33 + ] + } + ], + "expectEvents": [ + { + "client": "client0", + "events": [ + { + "commandStartedEvent": { + "command": { + "distinct": "coll0", + "key": "x", + "query": {}, + "comment": { + "key": "value" + } + }, + "commandName": "distinct", + "databaseName": "distinct-comment-tests" + } + } + ] + } + ] + }, + { + "description": "distinct with string comment", + "runOnRequirements": [ + { + "minServerVersion": "4.4.0" + } + ], + "operations": [ + { + "name": "distinct", + "object": "collection0", + "arguments": { + "fieldName": "x", + "filter": {}, + "comment": "comment" + }, + "expectResult": [ + 11, + 22, + 33 + ] + } + ], + "expectEvents": [ + { + "client": "client0", + "events": [ + { + "commandStartedEvent": { + "command": { + "distinct": "coll0", + "key": "x", + "query": {}, + "comment": "comment" + }, + "commandName": "distinct", + "databaseName": "distinct-comment-tests" + } + } + ] + } + ] + }, + { + "description": "distinct with document comment - pre 4.4, server error", + "runOnRequirements": [ + { + "minServerVersion": "3.6.0", + "maxServerVersion": "4.4.13" + } + ], + "operations": [ + { + "name": "distinct", + "object": "collection0", + "arguments": { + "fieldName": "x", + "filter": {}, + "comment": { + "key": "value" + } + }, + "expectError": { + "isClientError": false + } + } + ], + "expectEvents": [ + { + "client": "client0", + "events": [ + { + "commandStartedEvent": { + "command": { + "distinct": "coll0", + "key": "x", + "query": {}, + "comment": { + "key": "value" + } + }, + "commandName": "distinct", + "databaseName": "distinct-comment-tests" + } + } + ] + } + ] + } + ] +} diff --git a/test/spec/crud/unified/distinct-comment.yml b/test/spec/crud/unified/distinct-comment.yml new file mode 100644 index 0000000000..c082009943 --- /dev/null +++ b/test/spec/crud/unified/distinct-comment.yml @@ -0,0 +1,98 @@ +description: "distinct-comment" + +schemaVersion: "1.0" + +createEntities: + - client: + id: &client0 client0 + observeEvents: [ commandStartedEvent ] + - database: + id: &database0 database0 + client: *client0 + databaseName: &database0Name distinct-comment-tests + - collection: + id: &collection0 collection0 + database: *database0 + collectionName: &collection0Name coll0 + +initialData: + - collectionName: *collection0Name + databaseName: *database0Name + documents: + - { _id: 1, x: 11 } + - { _id: 2, x: 22 } + - { _id: 3, x: 33 } + +tests: + - description: "distinct with document comment" + runOnRequirements: + # https://jira.mongodb.org/browse/SERVER-44847 + # Server supports distinct with comment of any type for comment starting from 4.4.14. + - minServerVersion: "4.4.14" + operations: + - name: distinct + object: *collection0 + arguments: + fieldName: &fieldName x + filter: &filter {} + comment: &documentComment { key: "value"} + expectResult: [ 11, 22, 33 ] + expectEvents: + - client: *client0 + events: + - commandStartedEvent: + command: + distinct: *collection0Name + key: *fieldName + query: *filter + comment: *documentComment + commandName: distinct + databaseName: *database0Name + + - description: "distinct with string comment" + runOnRequirements: + - minServerVersion: "4.4.0" + operations: + - name: distinct + object: *collection0 + arguments: + fieldName: *fieldName + filter: *filter + comment: &stringComment "comment" + expectResult: [ 11, 22, 33 ] + expectEvents: + - client: *client0 + events: + - commandStartedEvent: + command: + distinct: *collection0Name + key: *fieldName + query: *filter + comment: *stringComment + commandName: distinct + databaseName: *database0Name + + - description: "distinct with document comment - pre 4.4, server error" + runOnRequirements: + - minServerVersion: "3.6.0" + maxServerVersion: "4.4.13" + operations: + - name: distinct + object: *collection0 + arguments: + fieldName: *fieldName + filter: *filter + comment: *documentComment + expectError: + isClientError: false + expectEvents: + - client: *client0 + events: + - commandStartedEvent: + command: + distinct: *collection0Name + key: *fieldName + query: *filter + comment: *documentComment + commandName: distinct + databaseName: *database0Name