Skip to content

Commit bed1fe3

Browse files
feat(NODE-4078): allow comment with estimated doc count (#3301)
Co-authored-by: Bailey Pearson <[email protected]>
1 parent f92895f commit bed1fe3

File tree

4 files changed

+273
-1
lines changed

4 files changed

+273
-1
lines changed

src/operations/estimated_document_count.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,12 @@ export class EstimatedDocumentCountOperation extends CommandOperation<number> {
3838
cmd.maxTimeMS = this.options.maxTimeMS;
3939
}
4040

41+
// we check for undefined specifically here to allow falsy values
42+
// eslint-disable-next-line no-restricted-syntax
43+
if (this.options.comment !== undefined) {
44+
cmd.comment = this.options.comment;
45+
}
46+
4147
super.executeCommand(server, session, cmd, (err, response) => {
4248
if (err) {
4349
callback(err);

test/integration/node-specific/comment_with_falsy_values.test.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,8 @@ function* generateTestCombinations() {
2424
['aggregate', { pipeline: [] }] as const,
2525
['insertMany', { documents: [{ name: 'john' }] }] as const,
2626
['deleteOne', { filter: { toBeDeleted: true } }] as const,
27-
['findOneAndReplace', { filter: { _id: 1 }, replacement: { x: 12 } }] as const
27+
['findOneAndReplace', { filter: { _id: 1 }, replacement: { x: 12 } }] as const,
28+
['estimatedDocumentCount', {}] as const
2829
]) {
2930
for (const falsyValue of falsyValues) {
3031
yield { name, args: { ...args, comment: falsyValue } };
Lines changed: 170 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,170 @@
1+
{
2+
"description": "estimatedDocumentCount-comment",
3+
"schemaVersion": "1.0",
4+
"createEntities": [
5+
{
6+
"client": {
7+
"id": "client0",
8+
"observeEvents": [
9+
"commandStartedEvent"
10+
]
11+
}
12+
},
13+
{
14+
"database": {
15+
"id": "database0",
16+
"client": "client0",
17+
"databaseName": "edc-comment-tests"
18+
}
19+
},
20+
{
21+
"collection": {
22+
"id": "collection0",
23+
"database": "database0",
24+
"collectionName": "coll0"
25+
}
26+
}
27+
],
28+
"initialData": [
29+
{
30+
"collectionName": "coll0",
31+
"databaseName": "edc-comment-tests",
32+
"documents": [
33+
{
34+
"_id": 1,
35+
"x": 11
36+
},
37+
{
38+
"_id": 2,
39+
"x": 22
40+
},
41+
{
42+
"_id": 3,
43+
"x": 33
44+
}
45+
]
46+
}
47+
],
48+
"tests": [
49+
{
50+
"description": "estimatedDocumentCount with document comment",
51+
"runOnRequirements": [
52+
{
53+
"minServerVersion": "4.4.14"
54+
}
55+
],
56+
"operations": [
57+
{
58+
"name": "estimatedDocumentCount",
59+
"object": "collection0",
60+
"arguments": {
61+
"comment": {
62+
"key": "value"
63+
}
64+
},
65+
"expectResult": 3
66+
}
67+
],
68+
"expectEvents": [
69+
{
70+
"client": "client0",
71+
"events": [
72+
{
73+
"commandStartedEvent": {
74+
"command": {
75+
"count": "coll0",
76+
"comment": {
77+
"key": "value"
78+
}
79+
},
80+
"commandName": "count",
81+
"databaseName": "edc-comment-tests"
82+
}
83+
}
84+
]
85+
}
86+
]
87+
},
88+
{
89+
"description": "estimatedDocumentCount with string comment",
90+
"runOnRequirements": [
91+
{
92+
"minServerVersion": "4.4.0"
93+
}
94+
],
95+
"operations": [
96+
{
97+
"name": "estimatedDocumentCount",
98+
"object": "collection0",
99+
"arguments": {
100+
"comment": "comment"
101+
},
102+
"expectResult": 3
103+
}
104+
],
105+
"expectEvents": [
106+
{
107+
"client": "client0",
108+
"events": [
109+
{
110+
"commandStartedEvent": {
111+
"command": {
112+
"count": "coll0",
113+
"comment": "comment"
114+
},
115+
"commandName": "count",
116+
"databaseName": "edc-comment-tests"
117+
}
118+
}
119+
]
120+
}
121+
]
122+
},
123+
{
124+
"description": "estimatedDocumentCount with document comment - pre 4.4.14, server error",
125+
"runOnRequirements": [
126+
{
127+
"minServerVersion": "3.6.0",
128+
"maxServerVersion": "4.4.13",
129+
"topologies": [
130+
"single",
131+
"replicaset"
132+
]
133+
}
134+
],
135+
"operations": [
136+
{
137+
"name": "estimatedDocumentCount",
138+
"object": "collection0",
139+
"arguments": {
140+
"comment": {
141+
"key": "value"
142+
}
143+
},
144+
"expectError": {
145+
"isClientError": false
146+
}
147+
}
148+
],
149+
"expectEvents": [
150+
{
151+
"client": "client0",
152+
"events": [
153+
{
154+
"commandStartedEvent": {
155+
"command": {
156+
"count": "coll0",
157+
"comment": {
158+
"key": "value"
159+
}
160+
},
161+
"commandName": "count",
162+
"databaseName": "edc-comment-tests"
163+
}
164+
}
165+
]
166+
}
167+
]
168+
}
169+
]
170+
}
Lines changed: 95 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
1+
description: "estimatedDocumentCount-comment"
2+
3+
schemaVersion: "1.0"
4+
5+
createEntities:
6+
- client:
7+
id: &client0 client0
8+
observeEvents: [ commandStartedEvent ]
9+
- database:
10+
id: &database0 database0
11+
client: *client0
12+
databaseName: &database0Name edc-comment-tests
13+
- collection:
14+
id: &collection0 collection0
15+
database: *database0
16+
collectionName: &collection0Name coll0
17+
18+
initialData:
19+
- collectionName: *collection0Name
20+
databaseName: *database0Name
21+
documents:
22+
- { _id: 1, x: 11 }
23+
- { _id: 2, x: 22 }
24+
- { _id: 3, x: 33 }
25+
26+
tests:
27+
- description: "estimatedDocumentCount with document comment"
28+
runOnRequirements:
29+
# https://jira.mongodb.org/browse/SERVER-63315
30+
# Server supports count with comment of any type for comment starting from 4.4.14.
31+
- minServerVersion: "4.4.14"
32+
operations:
33+
- name: estimatedDocumentCount
34+
object: *collection0
35+
arguments:
36+
comment: &documentComment { key: "value"}
37+
expectResult: 3
38+
expectEvents:
39+
- client: *client0
40+
events:
41+
- commandStartedEvent:
42+
command:
43+
count: *collection0Name
44+
comment: *documentComment
45+
commandName: count
46+
databaseName: *database0Name
47+
48+
- description: "estimatedDocumentCount with string comment"
49+
runOnRequirements:
50+
- minServerVersion: "4.4.0"
51+
operations:
52+
- name: estimatedDocumentCount
53+
object: *collection0
54+
arguments:
55+
comment: &stringComment "comment"
56+
expectResult: 3
57+
expectEvents:
58+
- client: *client0
59+
events:
60+
- commandStartedEvent:
61+
command:
62+
count: *collection0Name
63+
comment: *stringComment
64+
commandName: count
65+
databaseName: *database0Name
66+
67+
- description: "estimatedDocumentCount with document comment - pre 4.4.14, server error"
68+
runOnRequirements:
69+
- minServerVersion: "3.6.0"
70+
maxServerVersion: "4.4.13"
71+
# Server does not raise an error if topology is sharded.
72+
# https://jira.mongodb.org/browse/SERVER-65954
73+
topologies: [ single, replicaset ]
74+
operations:
75+
- name: estimatedDocumentCount
76+
object: *collection0
77+
arguments:
78+
# Even though according to the docs count command does not support any
79+
# comment for server version less than 4.4, no error is raised by such
80+
# servers. Therefore, we have only one test with a document comment
81+
# to test server errors.
82+
# https://jira.mongodb.org/browse/SERVER-63315
83+
# Server supports count with comment of any type for comment starting from 4.4.14.
84+
comment: *documentComment
85+
expectError:
86+
isClientError: false
87+
expectEvents:
88+
- client: *client0
89+
events:
90+
- commandStartedEvent:
91+
command:
92+
count: *collection0Name
93+
comment: *documentComment
94+
commandName: count
95+
databaseName: *database0Name

0 commit comments

Comments
 (0)