mongodb 2.2.36 → 3.0.2

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.
@@ -1,27 +1,29 @@
1
- "use strict";
1
+ 'use strict';
2
2
 
3
- var inherits = require('util').inherits
4
- , MongoError = require('mongodb-core').MongoError
5
- , Readable = require('stream').Readable || require('readable-stream').Readable
6
- , Define = require('./metadata')
7
- , CoreCursor = require('./cursor');
3
+ var inherits = require('util').inherits,
4
+ MongoError = require('mongodb-core').MongoError,
5
+ Readable = require('stream').Readable,
6
+ Define = require('./metadata'),
7
+ CoreCursor = require('./cursor');
8
8
 
9
9
  /**
10
10
  * @fileOverview The **AggregationCursor** class is an internal class that embodies an aggregation cursor on MongoDB
11
11
  * allowing for iteration over the results returned from the underlying query. It supports
12
- * one by one document iteration, conversion to an array or can be iterated as a Node 0.10.X
12
+ * one by one document iteration, conversion to an array or can be iterated as a Node 4.X
13
13
  * or higher stream
14
14
  *
15
15
  * **AGGREGATIONCURSOR Cannot directly be instantiated**
16
16
  * @example
17
- * var MongoClient = require('mongodb').MongoClient,
18
- * test = require('assert');
17
+ * const MongoClient = require('mongodb').MongoClient;
18
+ * const test = require('assert');
19
19
  * // Connection url
20
- * var url = 'mongodb://localhost:27017/test';
20
+ * const url = 'mongodb://localhost:27017';
21
+ * // Database Name
22
+ * const dbName = 'test';
21
23
  * // Connect using MongoClient
22
- * MongoClient.connect(url, function(err, db) {
24
+ * MongoClient.connect(url, function(err, client) {
23
25
  * // Create a collection we want to drop later
24
- * var col = db.collection('createIndexExample1');
26
+ * const col = client.db(dbName).collection('createIndexExample1');
25
27
  * // Insert a bunch of documents
26
28
  * col.insert([{a:1, b:1}
27
29
  * , {a:2, b:2}, {a:3, b:3}
@@ -31,7 +33,7 @@ var inherits = require('util').inherits
31
33
  * col.aggregation({}, {cursor: {}}).toArray(function(err, items) {
32
34
  * test.equal(null, err);
33
35
  * test.equal(4, items.length);
34
- * db.close();
36
+ * client.close();
35
37
  * });
36
38
  * });
37
39
  * });
@@ -61,41 +63,35 @@ var AggregationCursor = function(bson, ns, cmd, options, topology, topologyOptio
61
63
  var maxTimeMS = null;
62
64
 
63
65
  // Get the promiseLibrary
64
- var promiseLibrary = options.promiseLibrary;
65
-
66
- // No promise library selected fall back
67
- if(!promiseLibrary) {
68
- promiseLibrary = typeof global.Promise == 'function' ?
69
- global.Promise : require('es6-promise').Promise;
70
- }
66
+ var promiseLibrary = options.promiseLibrary || Promise;
71
67
 
72
68
  // Set up
73
- Readable.call(this, {objectMode: true});
69
+ Readable.call(this, { objectMode: true });
74
70
 
75
71
  // Internal state
76
72
  this.s = {
77
73
  // MaxTimeMS
78
- maxTimeMS: maxTimeMS
74
+ maxTimeMS: maxTimeMS,
79
75
  // State
80
- , state: state
76
+ state: state,
81
77
  // Stream options
82
- , streamOptions: streamOptions
78
+ streamOptions: streamOptions,
83
79
  // BSON
84
- , bson: bson
80
+ bson: bson,
85
81
  // Namespace
86
- , ns: ns
82
+ ns: ns,
87
83
  // Command
88
- , cmd: cmd
84
+ cmd: cmd,
89
85
  // Options
90
- , options: options
86
+ options: options,
91
87
  // Topology
92
- , topology: topology
88
+ topology: topology,
93
89
  // Topology Options
94
- , topologyOptions: topologyOptions
90
+ topologyOptions: topologyOptions,
95
91
  // Promise library
96
- , promiseLibrary: promiseLibrary
97
- }
98
- }
92
+ promiseLibrary: promiseLibrary
93
+ };
94
+ };
99
95
 
100
96
  /**
101
97
  * AggregationCursor stream data event, fired for each document in the cursor.
@@ -129,11 +125,11 @@ var AggregationCursor = function(bson, ns, cmd, options, topology, topologyOptio
129
125
  inherits(AggregationCursor, Readable);
130
126
 
131
127
  // Extend the Cursor
132
- for(var name in CoreCursor.prototype) {
128
+ for (var name in CoreCursor.prototype) {
133
129
  AggregationCursor.prototype[name] = CoreCursor.prototype[name];
134
130
  }
135
131
 
136
- var define = AggregationCursor.define = new Define('AggregationCursor', AggregationCursor, true);
132
+ var define = (AggregationCursor.define = new Define('AggregationCursor', AggregationCursor, true));
137
133
 
138
134
  /**
139
135
  * Set the batch size for the cursor.
@@ -143,14 +139,16 @@ var define = AggregationCursor.define = new Define('AggregationCursor', Aggregat
143
139
  * @return {AggregationCursor}
144
140
  */
145
141
  AggregationCursor.prototype.batchSize = function(value) {
146
- if(this.s.state == AggregationCursor.CLOSED || this.isDead()) throw MongoError.create({message: "Cursor is closed", driver:true });
147
- if(typeof value != 'number') throw MongoError.create({message: "batchSize requires an integer", driver:true });
148
- if(this.s.cmd.cursor) this.s.cmd.cursor.batchSize = value;
142
+ if (this.s.state === AggregationCursor.CLOSED || this.isDead())
143
+ throw MongoError.create({ message: 'Cursor is closed', driver: true });
144
+ if (typeof value !== 'number')
145
+ throw MongoError.create({ message: 'batchSize requires an integer', drvier: true });
146
+ if (this.s.cmd.cursor) this.s.cmd.cursor.batchSize = value;
149
147
  this.setCursorBatchSize(value);
150
148
  return this;
151
- }
149
+ };
152
150
 
153
- define.classMethod('batchSize', {callback: false, promise:false, returns: [AggregationCursor]});
151
+ define.classMethod('batchSize', { callback: false, promise: false, returns: [AggregationCursor] });
154
152
 
155
153
  /**
156
154
  * Add a geoNear stage to the aggregation pipeline
@@ -159,11 +157,11 @@ define.classMethod('batchSize', {callback: false, promise:false, returns: [Aggre
159
157
  * @return {AggregationCursor}
160
158
  */
161
159
  AggregationCursor.prototype.geoNear = function(document) {
162
- this.s.cmd.pipeline.push({$geoNear: document});
160
+ this.s.cmd.pipeline.push({ $geoNear: document });
163
161
  return this;
164
- }
162
+ };
165
163
 
166
- define.classMethod('geoNear', {callback: false, promise:false, returns: [AggregationCursor]});
164
+ define.classMethod('geoNear', { callback: false, promise: false, returns: [AggregationCursor] });
167
165
 
168
166
  /**
169
167
  * Add a group stage to the aggregation pipeline
@@ -172,11 +170,11 @@ define.classMethod('geoNear', {callback: false, promise:false, returns: [Aggrega
172
170
  * @return {AggregationCursor}
173
171
  */
174
172
  AggregationCursor.prototype.group = function(document) {
175
- this.s.cmd.pipeline.push({$group: document});
173
+ this.s.cmd.pipeline.push({ $group: document });
176
174
  return this;
177
- }
175
+ };
178
176
 
179
- define.classMethod('group', {callback: false, promise:false, returns: [AggregationCursor]});
177
+ define.classMethod('group', { callback: false, promise: false, returns: [AggregationCursor] });
180
178
 
181
179
  /**
182
180
  * Add a limit stage to the aggregation pipeline
@@ -185,11 +183,11 @@ define.classMethod('group', {callback: false, promise:false, returns: [Aggregati
185
183
  * @return {AggregationCursor}
186
184
  */
187
185
  AggregationCursor.prototype.limit = function(value) {
188
- this.s.cmd.pipeline.push({$limit: value});
186
+ this.s.cmd.pipeline.push({ $limit: value });
189
187
  return this;
190
- }
188
+ };
191
189
 
192
- define.classMethod('limit', {callback: false, promise:false, returns: [AggregationCursor]});
190
+ define.classMethod('limit', { callback: false, promise: false, returns: [AggregationCursor] });
193
191
 
194
192
  /**
195
193
  * Add a match stage to the aggregation pipeline
@@ -198,11 +196,11 @@ define.classMethod('limit', {callback: false, promise:false, returns: [Aggregati
198
196
  * @return {AggregationCursor}
199
197
  */
200
198
  AggregationCursor.prototype.match = function(document) {
201
- this.s.cmd.pipeline.push({$match: document});
199
+ this.s.cmd.pipeline.push({ $match: document });
202
200
  return this;
203
- }
201
+ };
204
202
 
205
- define.classMethod('match', {callback: false, promise:false, returns: [AggregationCursor]});
203
+ define.classMethod('match', { callback: false, promise: false, returns: [AggregationCursor] });
206
204
 
207
205
  /**
208
206
  * Add a maxTimeMS stage to the aggregation pipeline
@@ -211,13 +209,13 @@ define.classMethod('match', {callback: false, promise:false, returns: [Aggregati
211
209
  * @return {AggregationCursor}
212
210
  */
213
211
  AggregationCursor.prototype.maxTimeMS = function(value) {
214
- if(this.s.topology.lastIsMaster().minWireVersion > 2) {
212
+ if (this.s.topology.lastIsMaster().minWireVersion > 2) {
215
213
  this.s.cmd.maxTimeMS = value;
216
214
  }
217
215
  return this;
218
- }
216
+ };
219
217
 
220
- define.classMethod('maxTimeMS', {callback: false, promise:false, returns: [AggregationCursor]});
218
+ define.classMethod('maxTimeMS', { callback: false, promise: false, returns: [AggregationCursor] });
221
219
 
222
220
  /**
223
221
  * Add a out stage to the aggregation pipeline
@@ -226,11 +224,11 @@ define.classMethod('maxTimeMS', {callback: false, promise:false, returns: [Aggre
226
224
  * @return {AggregationCursor}
227
225
  */
228
226
  AggregationCursor.prototype.out = function(destination) {
229
- this.s.cmd.pipeline.push({$out: destination});
227
+ this.s.cmd.pipeline.push({ $out: destination });
230
228
  return this;
231
- }
229
+ };
232
230
 
233
- define.classMethod('out', {callback: false, promise:false, returns: [AggregationCursor]});
231
+ define.classMethod('out', { callback: false, promise: false, returns: [AggregationCursor] });
234
232
 
235
233
  /**
236
234
  * Add a project stage to the aggregation pipeline
@@ -239,11 +237,11 @@ define.classMethod('out', {callback: false, promise:false, returns: [Aggregation
239
237
  * @return {AggregationCursor}
240
238
  */
241
239
  AggregationCursor.prototype.project = function(document) {
242
- this.s.cmd.pipeline.push({$project: document});
240
+ this.s.cmd.pipeline.push({ $project: document });
243
241
  return this;
244
- }
242
+ };
245
243
 
246
- define.classMethod('project', {callback: false, promise:false, returns: [AggregationCursor]});
244
+ define.classMethod('project', { callback: false, promise: false, returns: [AggregationCursor] });
247
245
 
248
246
  /**
249
247
  * Add a lookup stage to the aggregation pipeline
@@ -252,11 +250,11 @@ define.classMethod('project', {callback: false, promise:false, returns: [Aggrega
252
250
  * @return {AggregationCursor}
253
251
  */
254
252
  AggregationCursor.prototype.lookup = function(document) {
255
- this.s.cmd.pipeline.push({$lookup: document});
253
+ this.s.cmd.pipeline.push({ $lookup: document });
256
254
  return this;
257
- }
255
+ };
258
256
 
259
- define.classMethod('lookup', {callback: false, promise:false, returns: [AggregationCursor]});
257
+ define.classMethod('lookup', { callback: false, promise: false, returns: [AggregationCursor] });
260
258
 
261
259
  /**
262
260
  * Add a redact stage to the aggregation pipeline
@@ -265,11 +263,11 @@ define.classMethod('lookup', {callback: false, promise:false, returns: [Aggregat
265
263
  * @return {AggregationCursor}
266
264
  */
267
265
  AggregationCursor.prototype.redact = function(document) {
268
- this.s.cmd.pipeline.push({$redact: document});
266
+ this.s.cmd.pipeline.push({ $redact: document });
269
267
  return this;
270
- }
268
+ };
271
269
 
272
- define.classMethod('redact', {callback: false, promise:false, returns: [AggregationCursor]});
270
+ define.classMethod('redact', { callback: false, promise: false, returns: [AggregationCursor] });
273
271
 
274
272
  /**
275
273
  * Add a skip stage to the aggregation pipeline
@@ -278,11 +276,11 @@ define.classMethod('redact', {callback: false, promise:false, returns: [Aggregat
278
276
  * @return {AggregationCursor}
279
277
  */
280
278
  AggregationCursor.prototype.skip = function(value) {
281
- this.s.cmd.pipeline.push({$skip: value});
279
+ this.s.cmd.pipeline.push({ $skip: value });
282
280
  return this;
283
- }
281
+ };
284
282
 
285
- define.classMethod('skip', {callback: false, promise:false, returns: [AggregationCursor]});
283
+ define.classMethod('skip', { callback: false, promise: false, returns: [AggregationCursor] });
286
284
 
287
285
  /**
288
286
  * Add a sort stage to the aggregation pipeline
@@ -291,11 +289,11 @@ define.classMethod('skip', {callback: false, promise:false, returns: [Aggregatio
291
289
  * @return {AggregationCursor}
292
290
  */
293
291
  AggregationCursor.prototype.sort = function(document) {
294
- this.s.cmd.pipeline.push({$sort: document});
292
+ this.s.cmd.pipeline.push({ $sort: document });
295
293
  return this;
296
- }
294
+ };
297
295
 
298
- define.classMethod('sort', {callback: false, promise:false, returns: [AggregationCursor]});
296
+ define.classMethod('sort', { callback: false, promise: false, returns: [AggregationCursor] });
299
297
 
300
298
  /**
301
299
  * Add a unwind stage to the aggregation pipeline
@@ -304,25 +302,25 @@ define.classMethod('sort', {callback: false, promise:false, returns: [Aggregatio
304
302
  * @return {AggregationCursor}
305
303
  */
306
304
  AggregationCursor.prototype.unwind = function(field) {
307
- this.s.cmd.pipeline.push({$unwind: field});
305
+ this.s.cmd.pipeline.push({ $unwind: field });
308
306
  return this;
309
- }
307
+ };
310
308
 
311
- define.classMethod('unwind', {callback: false, promise:false, returns: [AggregationCursor]});
309
+ define.classMethod('unwind', { callback: false, promise: false, returns: [AggregationCursor] });
312
310
 
313
311
  AggregationCursor.prototype.get = AggregationCursor.prototype.toArray;
314
312
 
315
313
  // Inherited methods
316
- define.classMethod('toArray', {callback: true, promise:true});
317
- define.classMethod('each', {callback: true, promise:false});
318
- define.classMethod('forEach', {callback: true, promise:false});
319
- define.classMethod('hasNext', {callback: true, promise:true});
320
- define.classMethod('next', {callback: true, promise:true});
321
- define.classMethod('close', {callback: true, promise:true});
322
- define.classMethod('isClosed', {callback: false, promise:false, returns: [Boolean]});
323
- define.classMethod('rewind', {callback: false, promise:false});
324
- define.classMethod('bufferedCount', {callback: false, promise:false, returns: [Number]});
325
- define.classMethod('readBufferedDocuments', {callback: false, promise:false, returns: [Array]});
314
+ define.classMethod('toArray', { callback: true, promise: true });
315
+ define.classMethod('each', { callback: true, promise: false });
316
+ define.classMethod('forEach', { callback: true, promise: false });
317
+ define.classMethod('hasNext', { callback: true, promise: true });
318
+ define.classMethod('next', { callback: true, promise: true });
319
+ define.classMethod('close', { callback: true, promise: true });
320
+ define.classMethod('isClosed', { callback: false, promise: false, returns: [Boolean] });
321
+ define.classMethod('rewind', { callback: false, promise: false });
322
+ define.classMethod('bufferedCount', { callback: false, promise: false, returns: [Number] });
323
+ define.classMethod('readBufferedDocuments', { callback: false, promise: false, returns: [Array] });
326
324
 
327
325
  /**
328
326
  * Get the next available document from the cursor, returns null if no more documents are available.
@@ -350,7 +348,7 @@ define.classMethod('readBufferedDocuments', {callback: false, promise:false, ret
350
348
  /**
351
349
  * Returns an array of documents. The caller is responsible for making sure that there
352
350
  * is enough memory to store the results. Note that the array only contain partial
353
- * results when this cursor had been previously accessed. In that case,
351
+ * results when this cursor had been previouly accessed. In that case,
354
352
  * cursor.rewind() can be used to reset the cursor.
355
353
  * @method AggregationCursor.prototype.toArray
356
354
  * @param {AggregationCursor~toArrayResultCallback} [callback] The result callback.
@@ -367,7 +365,7 @@ define.classMethod('readBufferedDocuments', {callback: false, promise:false, ret
367
365
 
368
366
  /**
369
367
  * Iterates over all the documents for this cursor. As with **{cursor.toArray}**,
370
- * not all of the elements will be iterated if this cursor had been previously accessed.
368
+ * not all of the elements will be iterated if this cursor had been previouly accessed.
371
369
  * In that case, **{cursor.rewind}** can be used to reset the cursor. However, unlike
372
370
  * **{cursor.toArray}**, the cursor will only hold a maximum of batch size elements
373
371
  * at any given time if batch size is specified. Otherwise, the caller is responsible