Skip to content

Commit 325c4bc

Browse files
feat(NODE-4970): deprecate collStats collection helper (#3638)
Co-authored-by: Durran Jordan <[email protected]>
1 parent fcff6fe commit 325c4bc

File tree

8 files changed

+166
-24
lines changed

8 files changed

+166
-24
lines changed

src/collection.ts

+3
Original file line numberDiff line numberDiff line change
@@ -784,6 +784,9 @@ export class Collection<TSchema extends Document = Document> {
784784
/**
785785
* Get all the collection statistics.
786786
*
787+
* @deprecated the `collStats` operation will be removed in the next major release. Please
788+
* use an aggregation pipeline with the [`$collStats`](https://www.mongodb.com/docs/manual/reference/operator/aggregation/collStats/) stage instead
789+
*
787790
* @param options - Optional settings for the command
788791
*/
789792
async stats(options?: CollStatsOptions): Promise<CollStats> {

src/operations/stats.ts

+11-2
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,11 @@ import type { Callback } from '../utils';
77
import { CommandOperation, CommandOperationOptions } from './command';
88
import { Aspect, defineAspects } from './operation';
99

10-
/** @public */
10+
/**
11+
* @public
12+
* @deprecated the `collStats` operation will be removed in the next major release. Please
13+
* use an aggregation pipeline with the [`$collStats`](https://www.mongodb.com/docs/manual/reference/operator/aggregation/collStats/) stage instead
14+
*/
1115
export interface CollStatsOptions extends CommandOperationOptions {
1216
/** Divide the returned sizes by scale value. */
1317
scale?: number;
@@ -77,6 +81,8 @@ export class DbStatsOperation extends CommandOperation<Document> {
7781
}
7882

7983
/**
84+
* @deprecated the `collStats` operation will be removed in the next major release. Please
85+
* use an aggregation pipeline with the [`$collStats`](https://www.mongodb.com/docs/manual/reference/operator/aggregation/collStats/) stage instead
8086
* @public
8187
* @see https://www.mongodb.com/docs/manual/reference/command/collStats/
8288
*/
@@ -130,7 +136,10 @@ export interface CollStats extends Document {
130136
scaleFactor: number;
131137
}
132138

133-
/** @public */
139+
/**
140+
* @public
141+
* @deprecated This type is only used for the deprecated `collStats` operation and will be removed in the next major release.
142+
*/
134143
export interface WiredTigerData extends Document {
135144
LSM: {
136145
'bloom filter false positives': number;

test/spec/client-side-encryption/tests/legacy/bypassedCommand.json

+5-4
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@
7878
]
7979
},
8080
{
81-
"description": "current op is not bypassed",
81+
"description": "kill op is not bypassed",
8282
"clientOptions": {
8383
"autoEncryptOpts": {
8484
"kmsProviders": {
@@ -90,14 +90,15 @@
9090
{
9191
"name": "runCommand",
9292
"object": "database",
93-
"command_name": "currentOp",
93+
"command_name": "killOp",
9494
"arguments": {
9595
"command": {
96-
"currentOp": 1
96+
"killOp": 1,
97+
"op": 1234
9798
}
9899
},
99100
"result": {
100-
"errorContains": "command not supported for auto encryption: currentOp"
101+
"errorContains": "command not supported for auto encryption: killOp"
101102
}
102103
}
103104
]

test/spec/client-side-encryption/tests/legacy/bypassedCommand.yml

+5-4
Original file line numberDiff line numberDiff line change
@@ -26,17 +26,18 @@ tests:
2626
command:
2727
ping: 1
2828
command_name: ping
29-
- description: "current op is not bypassed"
29+
- description: "kill op is not bypassed"
3030
clientOptions:
3131
autoEncryptOpts:
3232
kmsProviders:
3333
aws: {} # Credentials filled in from environment.
3434
operations:
3535
- name: runCommand
3636
object: database
37-
command_name: currentOp
37+
command_name: killOp
3838
arguments:
3939
command:
40-
currentOp: 1
40+
killOp: 1
41+
op: 1234
4142
result:
42-
errorContains: "command not supported for auto encryption: currentOp"
43+
errorContains: "command not supported for auto encryption: killOp"
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
{
2+
"description": "collectionData-createOptions",
3+
"schemaVersion": "1.9",
4+
"runOnRequirements": [
5+
{
6+
"minServerVersion": "3.6",
7+
"serverless": "forbid"
8+
}
9+
],
10+
"createEntities": [
11+
{
12+
"client": {
13+
"id": "client0"
14+
}
15+
},
16+
{
17+
"database": {
18+
"id": "database0",
19+
"client": "client0",
20+
"databaseName": "database0"
21+
}
22+
},
23+
{
24+
"collection": {
25+
"id": "collection0",
26+
"database": "database0",
27+
"collectionName": "coll0"
28+
}
29+
}
30+
],
31+
"initialData": [
32+
{
33+
"collectionName": "coll0",
34+
"databaseName": "database0",
35+
"createOptions": {
36+
"capped": true,
37+
"size": 4096
38+
},
39+
"documents": [
40+
{
41+
"_id": 1,
42+
"x": 11
43+
}
44+
]
45+
}
46+
],
47+
"tests": [
48+
{
49+
"description": "collection is created with the correct options",
50+
"operations": [
51+
{
52+
"object": "collection0",
53+
"name": "aggregate",
54+
"arguments": {
55+
"pipeline": [
56+
{
57+
"$collStats": {
58+
"storageStats": {}
59+
}
60+
},
61+
{
62+
"$project": {
63+
"capped": "$storageStats.capped",
64+
"maxSize": "$storageStats.maxSize"
65+
}
66+
}
67+
]
68+
},
69+
"expectResult": [
70+
{
71+
"capped": true,
72+
"maxSize": 4096
73+
}
74+
]
75+
}
76+
]
77+
}
78+
]
79+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
description: collectionData-createOptions
2+
3+
schemaVersion: "1.9"
4+
5+
runOnRequirements:
6+
- minServerVersion: "3.6"
7+
# Capped collections cannot be created on serverless instances.
8+
serverless: forbid
9+
10+
createEntities:
11+
- client:
12+
id: &client0 client0
13+
- database:
14+
id: &database0 database0
15+
client: *client0
16+
databaseName: &database0Name database0
17+
- collection:
18+
id: &collection0 collection0
19+
database: *database0
20+
collectionName: &collection0Name coll0
21+
22+
initialData:
23+
- collectionName: *collection0Name
24+
databaseName: *database0Name
25+
createOptions:
26+
capped: true
27+
# With MMAPv1, the size field cannot be less than 4096.
28+
size: &cappedSize 4096
29+
documents:
30+
- { _id: 1, x: 11 }
31+
32+
tests:
33+
- description: collection is created with the correct options
34+
operations:
35+
- object: *collection0
36+
name: aggregate
37+
arguments:
38+
pipeline:
39+
- $collStats: { storageStats: {} }
40+
- $project: { capped: '$storageStats.capped', maxSize: '$storageStats.maxSize'}
41+
expectResult:
42+
- { capped: true, maxSize: *cappedSize }

test/tools/unified-spec-runner/runner.ts

+20-14
Original file line numberDiff line numberDiff line change
@@ -118,38 +118,44 @@ async function runUnifiedTest(
118118
// The test runner MUST use the internal MongoClient for these operations.
119119
if (unifiedSuite.initialData) {
120120
trace('initialData');
121-
for (const collData of unifiedSuite.initialData) {
122-
const db = utilClient.db(collData.databaseName);
123-
const collection = db.collection(collData.collectionName, {
121+
for (const { databaseName, collectionName } of unifiedSuite.initialData) {
122+
const db = utilClient.db(databaseName);
123+
const collection = db.collection(collectionName, {
124124
writeConcern: { w: 'majority' }
125125
});
126126

127127
trace('listCollections');
128-
const collectionList = await db
129-
.listCollections({ name: collData.collectionName })
130-
.toArray();
128+
const collectionList = await db.listCollections({ name: collectionName }).toArray();
131129
if (collectionList.length !== 0) {
132130
trace('drop');
133131
expect(await collection.drop()).to.be.true;
134132
}
135133
}
136134

137-
for (const collData of unifiedSuite.initialData) {
138-
const db = utilClient.db(collData.databaseName);
139-
const collection = db.collection(collData.collectionName, {
135+
for (const {
136+
databaseName,
137+
collectionName,
138+
createOptions,
139+
documents = []
140+
} of unifiedSuite.initialData) {
141+
const db = utilClient.db(databaseName);
142+
const collection = db.collection(collectionName, {
140143
writeConcern: { w: 'majority' }
141144
});
142145

143-
if (!collData.documents?.length) {
146+
if (createOptions || !documents.length) {
144147
trace('createCollection');
145-
await db.createCollection(collData.collectionName, {
148+
const options = createOptions ?? {};
149+
await db.createCollection(collectionName, {
150+
...options,
146151
writeConcern: { w: 'majority' }
147152
});
148-
continue;
149153
}
150154

151-
trace('insertMany');
152-
await collection.insertMany(collData.documents);
155+
if (documents.length > 0) {
156+
trace('insertMany');
157+
await collection.insertMany(documents);
158+
}
153159
}
154160
}
155161

test/tools/unified-spec-runner/schema.ts

+1
Original file line numberDiff line numberDiff line change
@@ -238,6 +238,7 @@ export interface CollectionOrDatabaseOptions {
238238
export interface CollectionData {
239239
collectionName: string;
240240
databaseName: string;
241+
createOptions?: Document;
241242
documents: Document[];
242243
}
243244
export interface Test {

0 commit comments

Comments
 (0)