find_cursor.js
14.6 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.FindCursor = exports.FLAGS = void 0;
const error_1 = require("../error");
const count_1 = require("../operations/count");
const execute_operation_1 = require("../operations/execute_operation");
const find_1 = require("../operations/find");
const sort_1 = require("../sort");
const utils_1 = require("../utils");
const abstract_cursor_1 = require("./abstract_cursor");
/** @internal */
const kFilter = Symbol('filter');
/** @internal */
const kNumReturned = Symbol('numReturned');
/** @internal */
const kBuiltOptions = Symbol('builtOptions');
/** @public Flags allowed for cursor */
exports.FLAGS = [
'tailable',
'oplogReplay',
'noCursorTimeout',
'awaitData',
'exhaust',
'partial'
];
/** @public */
class FindCursor extends abstract_cursor_1.AbstractCursor {
/** @internal */
constructor(topology, namespace, filter, options = {}) {
super(topology, namespace, options);
this[kFilter] = filter || {};
this[kBuiltOptions] = options;
if (options.sort != null) {
this[kBuiltOptions].sort = (0, sort_1.formatSort)(options.sort);
}
}
clone() {
const clonedOptions = (0, utils_1.mergeOptions)({}, this[kBuiltOptions]);
delete clonedOptions.session;
return new FindCursor(this.topology, this.namespace, this[kFilter], {
...clonedOptions
});
}
map(transform) {
return super.map(transform);
}
/** @internal */
_initialize(session, callback) {
const findOperation = new find_1.FindOperation(undefined, this.namespace, this[kFilter], {
...this[kBuiltOptions],
...this.cursorOptions,
session
});
(0, execute_operation_1.executeOperation)(this, findOperation, (err, response) => {
if (err || response == null)
return callback(err);
// TODO: We only need this for legacy queries that do not support `limit`, maybe
// the value should only be saved in those cases.
if (response.cursor) {
this[kNumReturned] = response.cursor.firstBatch.length;
}
else {
this[kNumReturned] = response.documents ? response.documents.length : 0;
}
// TODO: NODE-2882
callback(undefined, { server: findOperation.server, session, response });
});
}
/** @internal */
_getMore(batchSize, callback) {
// NOTE: this is to support client provided limits in pre-command servers
const numReturned = this[kNumReturned];
if (numReturned) {
const limit = this[kBuiltOptions].limit;
batchSize =
limit && limit > 0 && numReturned + batchSize > limit ? limit - numReturned : batchSize;
if (batchSize <= 0) {
return this.close(callback);
}
}
super._getMore(batchSize, (err, response) => {
if (err)
return callback(err);
// TODO: wrap this in some logic to prevent it from happening if we don't need this support
if (response) {
this[kNumReturned] = this[kNumReturned] + response.cursor.nextBatch.length;
}
callback(undefined, response);
});
}
count(options, callback) {
(0, utils_1.emitWarningOnce)('cursor.count is deprecated and will be removed in the next major version, please use `collection.estimatedDocumentCount` or `collection.countDocuments` instead ');
if (typeof options === 'boolean') {
throw new error_1.MongoInvalidArgumentError('Invalid first parameter to count');
}
if (typeof options === 'function')
(callback = options), (options = {});
options = options !== null && options !== void 0 ? options : {};
return (0, execute_operation_1.executeOperation)(this, new count_1.CountOperation(this.namespace, this[kFilter], {
...this[kBuiltOptions],
...this.cursorOptions,
...options
}), callback);
}
explain(verbosity, callback) {
if (typeof verbosity === 'function')
(callback = verbosity), (verbosity = true);
if (verbosity == null)
verbosity = true;
return (0, execute_operation_1.executeOperation)(this, new find_1.FindOperation(undefined, this.namespace, this[kFilter], {
...this[kBuiltOptions],
...this.cursorOptions,
explain: verbosity
}), callback);
}
/** Set the cursor query */
filter(filter) {
(0, abstract_cursor_1.assertUninitialized)(this);
this[kFilter] = filter;
return this;
}
/**
* Set the cursor hint
*
* @param hint - If specified, then the query system will only consider plans using the hinted index.
*/
hint(hint) {
(0, abstract_cursor_1.assertUninitialized)(this);
this[kBuiltOptions].hint = hint;
return this;
}
/**
* Set the cursor min
*
* @param min - Specify a $min value to specify the inclusive lower bound for a specific index in order to constrain the results of find(). The $min specifies the lower bound for all keys of a specific index in order.
*/
min(min) {
(0, abstract_cursor_1.assertUninitialized)(this);
this[kBuiltOptions].min = min;
return this;
}
/**
* Set the cursor max
*
* @param max - Specify a $max value to specify the exclusive upper bound for a specific index in order to constrain the results of find(). The $max specifies the upper bound for all keys of a specific index in order.
*/
max(max) {
(0, abstract_cursor_1.assertUninitialized)(this);
this[kBuiltOptions].max = max;
return this;
}
/**
* Set the cursor returnKey.
* If set to true, modifies the cursor to only return the index field or fields for the results of the query, rather than documents.
* If set to true and the query does not use an index to perform the read operation, the returned documents will not contain any fields.
*
* @param value - the returnKey value.
*/
returnKey(value) {
(0, abstract_cursor_1.assertUninitialized)(this);
this[kBuiltOptions].returnKey = value;
return this;
}
/**
* Modifies the output of a query by adding a field $recordId to matching documents. $recordId is the internal key which uniquely identifies a document in a collection.
*
* @param value - The $showDiskLoc option has now been deprecated and replaced with the showRecordId field. $showDiskLoc will still be accepted for OP_QUERY stye find.
*/
showRecordId(value) {
(0, abstract_cursor_1.assertUninitialized)(this);
this[kBuiltOptions].showRecordId = value;
return this;
}
/**
* Add a query modifier to the cursor query
*
* @param name - The query modifier (must start with $, such as $orderby etc)
* @param value - The modifier value.
*/
addQueryModifier(name, value) {
(0, abstract_cursor_1.assertUninitialized)(this);
if (name[0] !== '$') {
throw new error_1.MongoInvalidArgumentError(`${name} is not a valid query modifier`);
}
// Strip of the $
const field = name.substr(1);
// NOTE: consider some TS magic for this
switch (field) {
case 'comment':
this[kBuiltOptions].comment = value;
break;
case 'explain':
this[kBuiltOptions].explain = value;
break;
case 'hint':
this[kBuiltOptions].hint = value;
break;
case 'max':
this[kBuiltOptions].max = value;
break;
case 'maxTimeMS':
this[kBuiltOptions].maxTimeMS = value;
break;
case 'min':
this[kBuiltOptions].min = value;
break;
case 'orderby':
this[kBuiltOptions].sort = (0, sort_1.formatSort)(value);
break;
case 'query':
this[kFilter] = value;
break;
case 'returnKey':
this[kBuiltOptions].returnKey = value;
break;
case 'showDiskLoc':
this[kBuiltOptions].showRecordId = value;
break;
default:
throw new error_1.MongoInvalidArgumentError(`Invalid query modifier: ${name}`);
}
return this;
}
/**
* Add a comment to the cursor query allowing for tracking the comment in the log.
*
* @param value - The comment attached to this query.
*/
comment(value) {
(0, abstract_cursor_1.assertUninitialized)(this);
this[kBuiltOptions].comment = value;
return this;
}
/**
* Set a maxAwaitTimeMS on a tailing cursor query to allow to customize the timeout value for the option awaitData (Only supported on MongoDB 3.2 or higher, ignored otherwise)
*
* @param value - Number of milliseconds to wait before aborting the tailed query.
*/
maxAwaitTimeMS(value) {
(0, abstract_cursor_1.assertUninitialized)(this);
if (typeof value !== 'number') {
throw new error_1.MongoInvalidArgumentError('Argument for maxAwaitTimeMS must be a number');
}
this[kBuiltOptions].maxAwaitTimeMS = value;
return this;
}
/**
* Set a maxTimeMS on the cursor query, allowing for hard timeout limits on queries (Only supported on MongoDB 2.6 or higher)
*
* @param value - Number of milliseconds to wait before aborting the query.
*/
maxTimeMS(value) {
(0, abstract_cursor_1.assertUninitialized)(this);
if (typeof value !== 'number') {
throw new error_1.MongoInvalidArgumentError('Argument for maxTimeMS must be a number');
}
this[kBuiltOptions].maxTimeMS = value;
return this;
}
/**
* Add a project stage to the aggregation pipeline
*
* @remarks
* In order to strictly type this function you must provide an interface
* that represents the effect of your projection on the result documents.
*
* By default chaining a projection to your cursor changes the returned type to the generic
* {@link Document} type.
* You should specify a parameterized type to have assertions on your final results.
*
* @example
* ```typescript
* // Best way
* const docs: FindCursor<{ a: number }> = cursor.project<{ a: number }>({ _id: 0, a: true });
* // Flexible way
* const docs: FindCursor<Document> = cursor.project({ _id: 0, a: true });
* ```
*
* @remarks
*
* **Note for Typescript Users:** adding a transform changes the return type of the iteration of this cursor,
* it **does not** return a new instance of a cursor. This means when calling project,
* you should always assign the result to a new variable in order to get a correctly typed cursor variable.
* Take note of the following example:
*
* @example
* ```typescript
* const cursor: FindCursor<{ a: number; b: string }> = coll.find();
* const projectCursor = cursor.project<{ a: number }>({ _id: 0, a: true });
* const aPropOnlyArray: {a: number}[] = await projectCursor.toArray();
*
* // or always use chaining and save the final cursor
*
* const cursor = coll.find().project<{ a: string }>({
* _id: 0,
* a: { $convert: { input: '$a', to: 'string' }
* }});
* ```
*/
project(value) {
(0, abstract_cursor_1.assertUninitialized)(this);
this[kBuiltOptions].projection = value;
return this;
}
/**
* Sets the sort order of the cursor query.
*
* @param sort - The key or keys set for the sort.
* @param direction - The direction of the sorting (1 or -1).
*/
sort(sort, direction) {
(0, abstract_cursor_1.assertUninitialized)(this);
if (this[kBuiltOptions].tailable) {
throw new error_1.MongoTailableCursorError('Tailable cursor does not support sorting');
}
this[kBuiltOptions].sort = (0, sort_1.formatSort)(sort, direction);
return this;
}
/**
* Allows disk use for blocking sort operations exceeding 100MB memory. (MongoDB 3.2 or higher)
*
* @remarks
* {@link https://docs.mongodb.com/manual/reference/command/find/#find-cmd-allowdiskuse | find command allowDiskUse documentation}
*/
allowDiskUse(allow = true) {
(0, abstract_cursor_1.assertUninitialized)(this);
if (!this[kBuiltOptions].sort) {
throw new error_1.MongoInvalidArgumentError('Option "allowDiskUse" requires a sort specification');
}
// As of 6.0 the default is true. This allows users to get back to the old behaviour.
if (!allow) {
this[kBuiltOptions].allowDiskUse = false;
return this;
}
this[kBuiltOptions].allowDiskUse = true;
return this;
}
/**
* Set the collation options for the cursor.
*
* @param value - The cursor collation options (MongoDB 3.4 or higher) settings for update operation (see 3.4 documentation for available fields).
*/
collation(value) {
(0, abstract_cursor_1.assertUninitialized)(this);
this[kBuiltOptions].collation = value;
return this;
}
/**
* Set the limit for the cursor.
*
* @param value - The limit for the cursor query.
*/
limit(value) {
(0, abstract_cursor_1.assertUninitialized)(this);
if (this[kBuiltOptions].tailable) {
throw new error_1.MongoTailableCursorError('Tailable cursor does not support limit');
}
if (typeof value !== 'number') {
throw new error_1.MongoInvalidArgumentError('Operation "limit" requires an integer');
}
this[kBuiltOptions].limit = value;
return this;
}
/**
* Set the skip for the cursor.
*
* @param value - The skip for the cursor query.
*/
skip(value) {
(0, abstract_cursor_1.assertUninitialized)(this);
if (this[kBuiltOptions].tailable) {
throw new error_1.MongoTailableCursorError('Tailable cursor does not support skip');
}
if (typeof value !== 'number') {
throw new error_1.MongoInvalidArgumentError('Operation "skip" requires an integer');
}
this[kBuiltOptions].skip = value;
return this;
}
}
exports.FindCursor = FindCursor;
//# sourceMappingURL=find_cursor.js.map