mongodb 3.3.0-beta2 → 3.3.3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (66) hide show
  1. package/HISTORY.md +155 -0
  2. package/index.js +1 -0
  3. package/lib/admin.js +6 -6
  4. package/lib/aggregation_cursor.js +180 -228
  5. package/lib/bulk/common.js +150 -73
  6. package/lib/bulk/ordered.js +1 -1
  7. package/lib/bulk/unordered.js +1 -0
  8. package/lib/change_stream.js +66 -66
  9. package/lib/collection.js +131 -44
  10. package/lib/command_cursor.js +87 -167
  11. package/lib/core/connection/connection.js +7 -3
  12. package/lib/core/connection/msg.js +5 -3
  13. package/lib/core/connection/pool.js +114 -143
  14. package/lib/core/cursor.js +569 -435
  15. package/lib/core/error.js +63 -12
  16. package/lib/core/index.js +1 -1
  17. package/lib/core/sdam/monitoring.js +8 -1
  18. package/lib/core/sdam/server.js +58 -14
  19. package/lib/core/sdam/topology.js +66 -31
  20. package/lib/core/sdam/topology_description.js +1 -1
  21. package/lib/core/sessions.js +68 -12
  22. package/lib/core/topologies/mongos.js +81 -26
  23. package/lib/core/topologies/replset.js +56 -10
  24. package/lib/core/topologies/replset_state.js +5 -5
  25. package/lib/core/topologies/server.js +7 -2
  26. package/lib/core/topologies/shared.js +23 -0
  27. package/lib/core/transactions.js +1 -0
  28. package/lib/core/uri_parser.js +8 -2
  29. package/lib/core/utils.js +13 -1
  30. package/lib/core/wireprotocol/get_more.js +4 -0
  31. package/lib/core/wireprotocol/query.js +8 -1
  32. package/lib/cursor.js +782 -844
  33. package/lib/db.js +27 -75
  34. package/lib/gridfs-stream/download.js +12 -5
  35. package/lib/gridfs-stream/index.js +1 -1
  36. package/lib/mongo_client.js +27 -11
  37. package/lib/operations/aggregate.js +75 -101
  38. package/lib/operations/close.js +12 -13
  39. package/lib/operations/collection_ops.js +3 -916
  40. package/lib/operations/command_v2.js +70 -4
  41. package/lib/operations/common_functions.js +48 -14
  42. package/lib/operations/connect.js +29 -36
  43. package/lib/operations/count.js +8 -8
  44. package/lib/operations/count_documents.js +24 -29
  45. package/lib/operations/cursor_ops.js +22 -32
  46. package/lib/operations/db_ops.js +0 -178
  47. package/lib/operations/estimated_document_count.js +43 -18
  48. package/lib/operations/execute_operation.js +51 -18
  49. package/lib/operations/explain.js +2 -2
  50. package/lib/operations/find.js +35 -0
  51. package/lib/operations/insert_one.js +1 -37
  52. package/lib/operations/list_collections.js +106 -0
  53. package/lib/operations/list_databases.js +38 -0
  54. package/lib/operations/list_indexes.js +28 -52
  55. package/lib/operations/operation.js +4 -0
  56. package/lib/operations/rename.js +1 -1
  57. package/lib/operations/replace_one.js +1 -1
  58. package/lib/operations/to_array.js +3 -5
  59. package/lib/read_concern.js +7 -1
  60. package/lib/topologies/mongos.js +1 -1
  61. package/lib/topologies/replset.js +1 -1
  62. package/lib/topologies/server.js +2 -2
  63. package/lib/topologies/topology_base.js +4 -5
  64. package/lib/utils.js +4 -4
  65. package/package.json +9 -5
  66. package/lib/operations/aggregate_operation.js +0 -127
@@ -1,12 +1,9 @@
1
1
  'use strict';
2
2
 
3
- const inherits = require('util').inherits;
4
3
  const MongoError = require('./core').MongoError;
5
- const Readable = require('stream').Readable;
6
- const CoreCursor = require('./cursor');
4
+ const Cursor = require('./cursor');
5
+ const CursorState = require('./core/cursor').CursorState;
7
6
  const deprecate = require('util').deprecate;
8
- const SUPPORTS = require('./utils').SUPPORTS;
9
- const MongoDBNamespace = require('./utils').MongoDBNamespace;
10
7
 
11
8
  /**
12
9
  * @fileOverview The **AggregationCursor** class is an internal class that embodies an aggregation cursor on MongoDB
@@ -56,253 +53,211 @@ const MongoDBNamespace = require('./utils').MongoDBNamespace;
56
53
  * @fires AggregationCursor#readable
57
54
  * @return {AggregationCursor} an AggregationCursor instance.
58
55
  */
59
- var AggregationCursor = function(topology, ns, cmd, options) {
60
- CoreCursor.apply(this, Array.prototype.slice.call(arguments, 0));
61
- var state = AggregationCursor.INIT;
62
- var streamOptions = {};
63
- const bson = topology.s.bson;
64
- const topologyOptions = topology.s.options;
65
-
66
- // MaxTimeMS
67
- var maxTimeMS = null;
68
-
69
- // Get the promiseLibrary
70
- var promiseLibrary = options.promiseLibrary || Promise;
71
-
72
- // Set up
73
- Readable.call(this, { objectMode: true });
74
-
75
- // Internal state
76
- this.s = {
77
- // MaxTimeMS
78
- maxTimeMS: maxTimeMS,
79
- // State
80
- state: state,
81
- // Stream options
82
- streamOptions: streamOptions,
83
- // BSON
84
- bson: bson,
85
- // Namespace
86
- // TODO: switch to raw namespace object later
87
- namespace: MongoDBNamespace.fromString(ns),
88
- // Command
89
- cmd: cmd,
90
- // Options
91
- options: options,
92
- // Topology
93
- topology: topology,
94
- // Topology Options
95
- topologyOptions: topologyOptions,
96
- // Promise library
97
- promiseLibrary: promiseLibrary,
98
- // Optional ClientSession
99
- session: options.session
100
- };
101
- };
56
+ class AggregationCursor extends Cursor {
57
+ constructor(topology, operation, options) {
58
+ super(topology, operation, options);
59
+ }
102
60
 
103
- /**
104
- * AggregationCursor stream data event, fired for each document in the cursor.
105
- *
106
- * @event AggregationCursor#data
107
- * @type {object}
108
- */
61
+ /**
62
+ * Set the batch size for the cursor.
63
+ * @method
64
+ * @param {number} value The number of documents to return per batch. See {@link https://docs.mongodb.com/manual/reference/command/aggregate|aggregation documentation}.
65
+ * @throws {MongoError}
66
+ * @return {AggregationCursor}
67
+ */
68
+ batchSize(value) {
69
+ if (this.s.state === CursorState.CLOSED || this.isDead()) {
70
+ throw MongoError.create({ message: 'Cursor is closed', driver: true });
71
+ }
72
+
73
+ if (typeof value !== 'number') {
74
+ throw MongoError.create({ message: 'batchSize requires an integer', driver: true });
75
+ }
76
+
77
+ this.operation.options.batchSize = value;
78
+ this.setCursorBatchSize(value);
79
+ return this;
80
+ }
109
81
 
110
- /**
111
- * AggregationCursor stream end event
112
- *
113
- * @event AggregationCursor#end
114
- * @type {null}
115
- */
82
+ /**
83
+ * Add a geoNear stage to the aggregation pipeline
84
+ * @method
85
+ * @param {object} document The geoNear stage document.
86
+ * @return {AggregationCursor}
87
+ */
88
+ geoNear(document) {
89
+ this.operation.addToPipeline({ $geoNear: document });
90
+ return this;
91
+ }
116
92
 
117
- /**
118
- * AggregationCursor stream close event
119
- *
120
- * @event AggregationCursor#close
121
- * @type {null}
122
- */
93
+ /**
94
+ * Add a group stage to the aggregation pipeline
95
+ * @method
96
+ * @param {object} document The group stage document.
97
+ * @return {AggregationCursor}
98
+ */
99
+ group(document) {
100
+ this.operation.addToPipeline({ $group: document });
101
+ return this;
102
+ }
123
103
 
124
- /**
125
- * AggregationCursor stream readable event
126
- *
127
- * @event AggregationCursor#readable
128
- * @type {null}
129
- */
104
+ /**
105
+ * Add a limit stage to the aggregation pipeline
106
+ * @method
107
+ * @param {number} value The state limit value.
108
+ * @return {AggregationCursor}
109
+ */
110
+ limit(value) {
111
+ this.operation.addToPipeline({ $limit: value });
112
+ return this;
113
+ }
130
114
 
131
- // Inherit from Readable
132
- inherits(AggregationCursor, Readable);
115
+ /**
116
+ * Add a match stage to the aggregation pipeline
117
+ * @method
118
+ * @param {object} document The match stage document.
119
+ * @return {AggregationCursor}
120
+ */
121
+ match(document) {
122
+ this.operation.addToPipeline({ $match: document });
123
+ return this;
124
+ }
133
125
 
134
- // Extend the Cursor
135
- for (var name in CoreCursor.prototype) {
136
- AggregationCursor.prototype[name] = CoreCursor.prototype[name];
137
- }
138
- if (SUPPORTS.ASYNC_ITERATOR) {
139
- AggregationCursor.prototype[
140
- Symbol.asyncIterator
141
- ] = require('./async/async_iterator').asyncIterator;
142
- }
126
+ /**
127
+ * Add a maxTimeMS stage to the aggregation pipeline
128
+ * @method
129
+ * @param {number} value The state maxTimeMS value.
130
+ * @return {AggregationCursor}
131
+ */
132
+ maxTimeMS(value) {
133
+ this.operation.options.maxTimeMS = value;
134
+ return this;
135
+ }
143
136
 
144
- /**
145
- * Set the batch size for the cursor.
146
- * @method
147
- * @param {number} value The batchSize for the cursor.
148
- * @throws {MongoError}
149
- * @return {AggregationCursor}
150
- */
151
- AggregationCursor.prototype.batchSize = function(value) {
152
- if (this.s.state === AggregationCursor.CLOSED || this.isDead())
153
- throw MongoError.create({ message: 'Cursor is closed', driver: true });
154
- if (typeof value !== 'number')
155
- throw MongoError.create({ message: 'batchSize requires an integer', driver: true });
156
- if (this.s.cmd.cursor) this.s.cmd.cursor.batchSize = value;
157
- this.setCursorBatchSize(value);
158
- return this;
159
- };
137
+ /**
138
+ * Add a out stage to the aggregation pipeline
139
+ * @method
140
+ * @param {number} destination The destination name.
141
+ * @return {AggregationCursor}
142
+ */
143
+ out(destination) {
144
+ this.operation.addToPipeline({ $out: destination });
145
+ return this;
146
+ }
160
147
 
161
- /**
162
- * Add a geoNear stage to the aggregation pipeline
163
- * @method
164
- * @param {object} document The geoNear stage document.
165
- * @return {AggregationCursor}
166
- */
167
- AggregationCursor.prototype.geoNear = deprecate(function(document) {
168
- this.s.cmd.pipeline.push({ $geoNear: document });
169
- return this;
170
- }, 'The `$geoNear` stage is deprecated in MongoDB 4.0, and removed in version 4.2.');
148
+ /**
149
+ * Add a project stage to the aggregation pipeline
150
+ * @method
151
+ * @param {object} document The project stage document.
152
+ * @return {AggregationCursor}
153
+ */
154
+ project(document) {
155
+ this.operation.addToPipeline({ $project: document });
156
+ return this;
157
+ }
171
158
 
172
- /**
173
- * Add a group stage to the aggregation pipeline
174
- * @method
175
- * @param {object} document The group stage document.
176
- * @return {AggregationCursor}
177
- */
178
- AggregationCursor.prototype.group = function(document) {
179
- this.s.cmd.pipeline.push({ $group: document });
180
- return this;
181
- };
159
+ /**
160
+ * Add a lookup stage to the aggregation pipeline
161
+ * @method
162
+ * @param {object} document The lookup stage document.
163
+ * @return {AggregationCursor}
164
+ */
165
+ lookup(document) {
166
+ this.operation.addToPipeline({ $lookup: document });
167
+ return this;
168
+ }
182
169
 
183
- /**
184
- * Add a limit stage to the aggregation pipeline
185
- * @method
186
- * @param {number} value The state limit value.
187
- * @return {AggregationCursor}
188
- */
189
- AggregationCursor.prototype.limit = function(value) {
190
- this.s.cmd.pipeline.push({ $limit: value });
191
- return this;
192
- };
170
+ /**
171
+ * Add a redact stage to the aggregation pipeline
172
+ * @method
173
+ * @param {object} document The redact stage document.
174
+ * @return {AggregationCursor}
175
+ */
176
+ redact(document) {
177
+ this.operation.addToPipeline({ $redact: document });
178
+ return this;
179
+ }
193
180
 
194
- /**
195
- * Add a match stage to the aggregation pipeline
196
- * @method
197
- * @param {object} document The match stage document.
198
- * @return {AggregationCursor}
199
- */
200
- AggregationCursor.prototype.match = function(document) {
201
- this.s.cmd.pipeline.push({ $match: document });
202
- return this;
203
- };
181
+ /**
182
+ * Add a skip stage to the aggregation pipeline
183
+ * @method
184
+ * @param {number} value The state skip value.
185
+ * @return {AggregationCursor}
186
+ */
187
+ skip(value) {
188
+ this.operation.addToPipeline({ $skip: value });
189
+ return this;
190
+ }
204
191
 
205
- /**
206
- * Add a maxTimeMS stage to the aggregation pipeline
207
- * @method
208
- * @param {number} value The state maxTimeMS value.
209
- * @return {AggregationCursor}
210
- */
211
- AggregationCursor.prototype.maxTimeMS = function(value) {
212
- if (this.s.topology.lastIsMaster().minWireVersion > 2) {
213
- this.s.cmd.maxTimeMS = value;
192
+ /**
193
+ * Add a sort stage to the aggregation pipeline
194
+ * @method
195
+ * @param {object} document The sort stage document.
196
+ * @return {AggregationCursor}
197
+ */
198
+ sort(document) {
199
+ this.operation.addToPipeline({ $sort: document });
200
+ return this;
214
201
  }
215
- return this;
216
- };
217
202
 
218
- /**
219
- * Add a out stage to the aggregation pipeline
220
- * @method
221
- * @param {number} destination The destination name.
222
- * @return {AggregationCursor}
223
- */
224
- AggregationCursor.prototype.out = function(destination) {
225
- this.s.cmd.pipeline.push({ $out: destination });
226
- return this;
227
- };
203
+ /**
204
+ * Add a unwind stage to the aggregation pipeline
205
+ * @method
206
+ * @param {number} field The unwind field name.
207
+ * @return {AggregationCursor}
208
+ */
209
+ unwind(field) {
210
+ this.operation.addToPipeline({ $unwind: field });
211
+ return this;
212
+ }
228
213
 
229
- /**
230
- * Add a project stage to the aggregation pipeline
231
- * @method
232
- * @param {object} document The project stage document.
233
- * @return {AggregationCursor}
234
- */
235
- AggregationCursor.prototype.project = function(document) {
236
- this.s.cmd.pipeline.push({ $project: document });
237
- return this;
238
- };
214
+ /**
215
+ * Return the cursor logger
216
+ * @method
217
+ * @return {Logger} return the cursor logger
218
+ * @ignore
219
+ */
220
+ getLogger() {
221
+ return this.logger;
222
+ }
223
+ }
239
224
 
240
- /**
241
- * Add a lookup stage to the aggregation pipeline
242
- * @method
243
- * @param {object} document The lookup stage document.
244
- * @return {AggregationCursor}
245
- */
246
- AggregationCursor.prototype.lookup = function(document) {
247
- this.s.cmd.pipeline.push({ $lookup: document });
248
- return this;
249
- };
225
+ // aliases
226
+ AggregationCursor.prototype.get = AggregationCursor.prototype.toArray;
250
227
 
251
- /**
252
- * Add a redact stage to the aggregation pipeline
253
- * @method
254
- * @param {object} document The redact stage document.
255
- * @return {AggregationCursor}
256
- */
257
- AggregationCursor.prototype.redact = function(document) {
258
- this.s.cmd.pipeline.push({ $redact: document });
259
- return this;
260
- };
228
+ // deprecated methods
229
+ deprecate(
230
+ AggregationCursor.prototype.geoNear,
231
+ 'The `$geoNear` stage is deprecated in MongoDB 4.0, and removed in version 4.2.'
232
+ );
261
233
 
262
234
  /**
263
- * Add a skip stage to the aggregation pipeline
264
- * @method
265
- * @param {number} value The state skip value.
266
- * @return {AggregationCursor}
235
+ * AggregationCursor stream data event, fired for each document in the cursor.
236
+ *
237
+ * @event AggregationCursor#data
238
+ * @type {object}
267
239
  */
268
- AggregationCursor.prototype.skip = function(value) {
269
- this.s.cmd.pipeline.push({ $skip: value });
270
- return this;
271
- };
272
240
 
273
241
  /**
274
- * Add a sort stage to the aggregation pipeline
275
- * @method
276
- * @param {object} document The sort stage document.
277
- * @return {AggregationCursor}
242
+ * AggregationCursor stream end event
243
+ *
244
+ * @event AggregationCursor#end
245
+ * @type {null}
278
246
  */
279
- AggregationCursor.prototype.sort = function(document) {
280
- this.s.cmd.pipeline.push({ $sort: document });
281
- return this;
282
- };
283
247
 
284
248
  /**
285
- * Add a unwind stage to the aggregation pipeline
286
- * @method
287
- * @param {number} field The unwind field name.
288
- * @return {AggregationCursor}
249
+ * AggregationCursor stream close event
250
+ *
251
+ * @event AggregationCursor#close
252
+ * @type {null}
289
253
  */
290
- AggregationCursor.prototype.unwind = function(field) {
291
- this.s.cmd.pipeline.push({ $unwind: field });
292
- return this;
293
- };
294
254
 
295
255
  /**
296
- * Return the cursor logger
297
- * @method
298
- * @return {Logger} return the cursor logger
299
- * @ignore
256
+ * AggregationCursor stream readable event
257
+ *
258
+ * @event AggregationCursor#readable
259
+ * @type {null}
300
260
  */
301
- AggregationCursor.prototype.getLogger = function() {
302
- return this.logger;
303
- };
304
-
305
- AggregationCursor.prototype.get = AggregationCursor.prototype.toArray;
306
261
 
307
262
  /**
308
263
  * Get the next available document from the cursor, returns null if no more documents are available.
@@ -353,6 +308,7 @@ AggregationCursor.prototype.get = AggregationCursor.prototype.toArray;
353
308
  * at any given time if batch size is specified. Otherwise, the caller is responsible
354
309
  * for making sure that the entire result can fit the memory.
355
310
  * @method AggregationCursor.prototype.each
311
+ * @deprecated
356
312
  * @param {AggregationCursor~resultCallback} callback The result callback.
357
313
  * @throws {MongoError}
358
314
  * @return {null}
@@ -402,7 +358,7 @@ AggregationCursor.prototype.get = AggregationCursor.prototype.toArray;
402
358
  * @param {MongoError} error An error instance representing the error during the execution.
403
359
  */
404
360
 
405
- /*
361
+ /**
406
362
  * Iterates over all the documents for this cursor using the iterator, callback pattern.
407
363
  * @method AggregationCursor.prototype.forEach
408
364
  * @param {AggregationCursor~iteratorCallback} iterator The iteration callback.
@@ -411,8 +367,4 @@ AggregationCursor.prototype.get = AggregationCursor.prototype.toArray;
411
367
  * @return {null}
412
368
  */
413
369
 
414
- AggregationCursor.INIT = 0;
415
- AggregationCursor.OPEN = 1;
416
- AggregationCursor.CLOSED = 2;
417
-
418
370
  module.exports = AggregationCursor;