333
schangxiang@126.com
2025-09-19 18966e02fb573c7e2bb0c6426ed792b38b910940
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
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
'use strict';
 
const EventEmitter = require('events');
const { MongoClient } = require('mongodb');
const assert = require('assert');
 
const MAKEURL = Symbol('makeURI');
 
class MongoDB extends EventEmitter {
  constructor(config) {
    super();
    this.config = config;
    this.url = this[MAKEURL](config);
    this.clientOptions = Object.assign(
      { useNewUrlParser: true },
      config.options
    );
    this.db;
    this.client;
  }
 
  [MAKEURL](config) {
    let url = 'mongodb://';
    /* istanbul ignore if */
    if (config.user) {
      if (config.password) url += `${config.user}:${config.password}@`;
      else url += `${config.user}@`;
    }
 
    const host = config.host.toString().split(',');
    const port = config.port.toString().split(',');
    const { options } = config;
 
    if (host.length > 1 && host.length !== port.length) {
      const errMsg =
        'The host and port do not match. Please check your config.';
      return this.emit('error', errMsg);
    }
 
    const hostLen = host.length;
    const portLen = port.length;
    for (let i = 0; i < portLen; i++) {
      let h = '';
      if (hostLen === 1) h = host[0];
      else h = host[i];
 
      url += `${h}:${port[i]},`;
    }
 
    url = url.slice(0, -1);
    url += `/${config.name}`;
 
    if (options) {
      let uriOp = '?';
      Object.keys(options).forEach(key => (uriOp += `${key}=${options[key]}&`));
      url += uriOp.slice(0, -1);
    }
 
    return url;
  }
 
  /**
   * Connect to MongoDB using a url as documented at
   * docs.mongodb.org/manual/reference/connection-string/
   *
   * @see https://mongodb.github.io/node-mongodb-native/3.1/api/MongoClient.html#.connect
   *
   * @return {Promise<MongoClient>} -
   */
  connect() {
    return MongoClient.connect(this.url, this.clientOptions)
      .then(async client => {
        this.client = client;
        this.db = client.db(this.config.name);
        this.emit('connect');
        return client;
      })
      .catch(
        /* istanbul ignore next */ error => {
          this.emit('error', error);
          throw error;
        }
      );
  }
 
  /**
   * @see https://mongodb.github.io/node-mongodb-native/3.1/api/MongoClient.html#close
   *
   * @return {Promise<void>} -
   */
  close() {
    return this.client.close();
  }
 
  /**
   * Inserts a single document into MongoDB. If documents passed in do not contain the _id field,
   * one will be added to each of the documents missing it by the driver, mutating the document.
   * This behavior can be overridden by setting the forceServerObjectId flag.
   *
   * @see https://mongodb.github.io/node-mongodb-native/3.1/api/Collection.html#insertOne
   *
   * @param {string} name - The name of collection to insert.
   * @param {string} [args] - Arguments.
   * @param {object} [args.doc={}] - The doc to insert.
   * @param {object} [args.options=null] - Optional settings.
   * @param {number|string} [args.options.w=null] - The write concern.
   * @param {number} [args.options.wtimeout=null] - The write concern timeout.
   * @param {boolean} [args.options.j=false] - Specify a journal write concern.
   * @param {boolean} [args.options.serializeFunctions=false] - Serialize functions on any object.
   * @param {boolean} [args.options.forceServerObjectId=false] - Force server to assign _id values instead of driver.
   * @param {boolean} [args.options.bypassDocumentValidation=false] - Allow driver to bypass schema validation in MongoDB 3.2 or higher.
   * @param {ClientSession} [args.options.session] - optional session to use for this operation.
   *
   * @return {Promise<Object>} insertOneWriteOpResult
   * @return {number} insertOneWriteOpResult.insertedCount
   * @return {object[]} insertOneWriteOpResult.ops - All the documents inserted.
   * Documents contain the _id field if forceServerObjectId == false.
   * @return {ObjectId} insertOneWriteOpResult.insertedId
   * @return {object} insertOneWriteOpResult.connection - The connection object used for the operation.
   * @return {object} insertOneWriteOpResult.result - The raw command result object returned from MongoDB (content might vary by server version).
   * @return {number} insertOneWriteOpResult.result.ok - Is 1 if the command executed correctly.
   * @return {number} insertOneWriteOpResult.result.n - The total count of documents inserted.
   */
  insertOne(name, args = { doc: {} }) {
    const { doc, options } = args;
    return this.db.collection(name).insertOne(doc, options);
  }
 
  /**
   * Inserts an array of documents into MongoDB. If documents passed in do not contain the _id field,
   * one will be added to each of the documents missing it by the driver, mutating the document.
   * This behavior can be overridden by setting the forceServerObjectId flag.
   *
   * @see https://mongodb.github.io/node-mongodb-native/3.1/api/Collection.html#insertMany
   *
   * @param {string} name - The name of collection to insert.
   * @param {object} args - Arguments.
   * @param {object[]} args.docs - The docs to insert.
   * @param {object} [args.options] - Optional settings.
   * @param {number|string} [args.options.w=null] -
   * @param {number} [args.options.wtimeout=null] -
   * @param {boolean} [args.options.j=false] -
   * @param {boolean} [args.options.serializeFunctions=false] -
   * @param {boolean} [args.options.forceServerObjectId=false] -
   * @param {boolean} [args.options.bypassDocumentValidation=false] -
   * @param {boolean} [args.options.ordered=true] -
   * @param {ClientSession} [args.options.session] -
   *
   * @return {Promise<Object>} insertOneWriteOpResult
   * @return {number} insertOneWriteOpResult.insertedCount
   * @return {object[]} insertOneWriteOpResult.ops - All the documents inserted.
   * Documents contain the _id field if forceServerObjectId == false.
   * @return {ObjectId[]} insertOneWriteOpResult.insertedIds
   * @return {object} insertOneWriteOpResult.connection - The connection object used for the operation.
   * @return {object} insertOneWriteOpResult.result - The raw command result object returned from MongoDB (content might vary by server version).
   * @return {number} insertOneWriteOpResult.result.ok - Is 1 if the command executed correctly.
   * @return {number} insertOneWriteOpResult.result.n - The total count of documents inserted.
   */
  insertMany(name, args = {}) {
    const { docs, options } = args;
    return this.db.collection(name).insertMany(docs, options);
  }
 
  static getFindAndModifyOptions(options) {
    return Object.assign({ sort: { _id: -1 } }, options);
  }
 
  /**
   * Find a document.
   *
   * @see https://mongodb.github.io/node-mongodb-native/3.1/api/Collection.html#findOne
   * @param {string} name - The name of collection to find.
   * @param {object} args - The arguments of command.
   * @param {object} args.query - Document selection filter.
   * @param {object} [args.options={}] - Optional settings. Check node-mongodb-native api document for detail.
   *
   * @return {Promise<object>} result
   */
  findOne(name, args = {}) {
    const { query, options } = args;
    return this.db.collection(name).findOne(query, options);
  }
 
  /**
   * Find a document and update it in one atomic operation, requires a write lock for the duration of the operation.
   *
   * @see https://mongodb.github.io/node-mongodb-native/3.1/api/Collection.html#findOneAndUpdate
   * @param {string} name - The name of collection to find.
   * @param {object} args - The arguments of command.
   * @param {object} args.filter - Document selection filter.
   * @param {object} args.update - Update operations to be performed on the document.
   * @param {object} [args.options={}] - Optional settings.
   * @param {object} [args.options.projection=null] - Limits the fields to return for all matching documents.
   * @param {object} [args.options.sort={_id: -1}] - Determines which document the operation modifies if the query selects multiple documents.
   * @param {object} [args.options.maxTimeMS=null] - The maximum amount of time to allow the query to run.
   * @param {boolean} [args.options.upsert=false] - Upsert the document if it does not exist.
   * @param {object} [args.options.returnOriginal=true] - When false, returns the updated document rather than the original. The default is true.
   * @param {ClientSession} [args.options.session] -
   *
   * @see https://mongodb.github.io/node-mongodb-native/3.1/api/Collection.html#~findAndModifyWriteOpResult
   * @return {Promise<object>} findAndModifyWriteOpResult
   * @return {object} findAndModifyWriteOpResult.value - Document returned from findAndModify command.
   * @return {number} findAndModifyWriteOpResult.ok - Is 1 if the command executed correctly.
   * @return {object} findAndModifyWriteOpResult.lastErrorObject
   * @return {boolean} findAndModifyWriteOpResult.lastErrorObject.updatedExisting - Contains true if an update operation modified an existing document.
   * @return {object} findAndModifyWriteOpResult.lastErrorObject.upserted - Contains the ObjectId of the inserted document if an update operation with upsert: true resulted in a new document.
   * @return {number} findAndModifyWriteOpResult.lastErrorObject.n - The amount of update documents.
   */
  findOneAndUpdate(name, args = {}) {
    const { filter, update } = args;
    const options = this.constructor.getFindAndModifyOptions(args.options);
 
    return this.db.collection(name).findOneAndUpdate(filter, update, options);
  }
 
  /**
   * @param {string} name - The name of collection to find.
   * @param {object} args - The arguments of command.
   * @param {object} args.filter - Document selection filter.
   * @param {object} args.replacement - Document replacing the matching document.
   * @param {object} [args.options] - Optional settings.
   * @param {object} [args.options.projection=null] - Limits the fields to return for all matching documents.
   * @param {object} [args.options.sort={_id: -1}] - Determines which document the operation modifies if the query selects multiple documents.
   * @param {object} [args.options.maxTimeMS=null] - The maximum amount of time to allow the query to run.
   * @param {boolean} [args.options.upsert=false] - Upsert the document if it does not exist.
   * @param {object} [args.options.returnOriginal=true] - When false, returns the updated document rather than the original. The default is true.
   * @param {ClientSession} [args.options.session] -
   *
   * @see https://mongodb.github.io/node-mongodb-native/3.1/api/Collection.html#~findAndModifyWriteOpResult
   * @return {Promise<object>} findAndModifyWriteOpResult
   * @return {object} findAndModifyWriteOpResult.value - Document returned from findAndModify command.
   * @return {number} findAndModifyWriteOpResult.ok - Is 1 if the command executed correctly.
   * @return {object} findAndModifyWriteOpResult.lastErrorObject
   * @return {boolean} findAndModifyWriteOpResult.lastErrorObject.updatedExisting - Contains true if an update operation modified an existing document.
   * @return {object} findAndModifyWriteOpResult.lastErrorObject.upserted - Contains the ObjectId of the inserted document if an update operation with upsert: true resulted in a new document.
   * @return {number} findAndModifyWriteOpResult.lastErrorObject.n - The amount of update documents.
   */
  findOneAndReplace(name, args = {}) {
    const { filter, replacement } = args;
    const options = this.constructor.getFindAndModifyOptions(args.options);
 
    return this.db
      .collection(name)
      .findOneAndReplace(filter, replacement, options);
  }
 
  /**
   * Find a document and delete it in one atomic operation, requires a write lock for the duration of the operation.
   * @see https://mongodb.github.io/node-mongodb-native/3.1/api/Collection.html#findOneAndDelete
   *
   * @param {string} name - The name of collection to find.
   * @param {object} args - The arguments of command.
   * @param {object} args.filter - Document selection filter.
   * @param {object} [args.options] - Optional settings.
   * @param {object} [args.options.projection=null] - Limits the fields to return for all matching documents.
   * @param {object} [args.options.sort=null] - Determines which document the operation modifies if the query selects multiple documents.
   * @param {object} [args.options.maxTimeMS=null] - The maximum amount of time to allow the query to run.
   * @param {ClientSession} [args.options.session] -
   *
   * @see https://mongodb.github.io/node-mongodb-native/3.1/api/Collection.html#~findAndModifyWriteOpResult
   * @return {Promise<object>} findAndModifyWriteOpResult
   * @return {object} findAndModifyWriteOpResult.value - Document returned from findAndModify command.
   * @return {number} findAndModifyWriteOpResult.ok - Is 1 if the command executed correctly.
   * @return {object} findAndModifyWriteOpResult.lastErrorObject
   * @return {object} findAndModifyWriteOpResult.lastErrorObject.upserted - Contains the ObjectId of the inserted document if an update operation with upsert: true resulted in a new document.
   * @return {number} findAndModifyWriteOpResult.lastErrorObject.n - The amount of update documents.
   */
  findOneAndDelete(name, args = {}) {
    const { filter } = args;
    const options = this.constructor.getFindAndModifyOptions(args.options);
 
    return this.db.collection(name).findOneAndDelete(filter, options);
  }
 
  /**
   * Update multiple documents on MongoDB.
   *
   * @see https://mongodb.github.io/node-mongodb-native/3.1/api/Collection.html#updateMany
   *
   * @param {string} name - The name of collection.
   * @param {object} args - The arguments of command.
   * @param {object} args.filter - The Filter used to select the document to update.
   * @param {object} args.update - The update operations to be applied to the document.
   * @param {object} [args.options=null] - Optional settings.
   * @param {boolean} [args.options.upsert=false] - Update operation is an upsert.
   * @param {(number|string)} [args.options.w=null] - The write concern.
   * @param {number} [args.options.wtimeout=null] - The write concern timeout.
   * @param {boolean} [args.options.j=false] - Specify a journal write concern.
   * @param {ClientSession} [args.options.session] -
   *
   * @see https://mongodb.github.io/node-mongodb-native/3.1/api/Collection.html#~updateWriteOpResult
   * @return {Promise<object>} updateWriteOpResult
   * @return {object} updateWriteOpResult.connection - The connection object used for the operation.
   * @return {number} updateWriteOpResult.matchedCount - The number of documents that matched the filter.
   * @return {number} updateWriteOpResult.modifiedCount - The number of documents that were modified.
   * @return {number} updateWriteOpResult.upsertedCount - The number of documents upserted.
   * @return {object} updateWriteOpResult.upsertedId - The upserted id.
   * @return {ObjectId} updateWriteOpResult.upsertedId._id - The upserted _id returned from the server.
   * @return {object} updateWriteOpResult.result
   * @return {number} updateWriteOpResult.result.ok - Is 1 if the command executed correctly.
   * @return {number} updateWriteOpResult.result.n - The total count of documents scanned.
   * @return {objnumberect} updateWriteOpResult.result.nModified - The total count of documents modified.
   */
  updateMany(name, args = {}) {
    const { filter, update, options } = args;
 
    return this.db.collection(name).updateMany(filter, update, options);
  }
 
  /**
   * Delete multiple documents on MongoDB.
   *
   * @see https://mongodb.github.io/node-mongodb-native/3.1/api/Collection.html#deleteMany
   *
   * @param {string} name - The name of collection.
   * @param {object} args - The arguments of command.
   * @param {object} args.filter - The Filter used to select the documents to remove.
   * @param {object} [args.options=null] - Optional settings.
   * @param {(number|string)} [args.options.w=null] - The write concern.
   * @param {number} [args.options.wtimeout=null] - The write concern timeout.
   * @param {boolean} [args.options.j=false] - Specify a journal write concern.
   * @param {ClientSession} [args.options.session] -
   *
   * @see https://mongodb.github.io/node-mongodb-native/3.1/api/Collection.html#~deleteWriteOpResult
   * @return {Promise<object>} deleteWriteOpResult
   * @return {number} deleteWriteOpResult.deletedCount
   * @return {object} deleteWriteOpResult.connection
   * @return {object} deleteWriteOpResult.result
   * @return {number} deleteWriteOpResult.result.ok
   * @return {number} deleteWriteOpResult.result.n
   */
  deleteMany(name, args = {}) {
    const { filter, options } = args;
    assert(filter, 'filter parameter must be an object');
 
    return this.db.collection(name).deleteMany(filter, options);
  }
 
  /**
   * Find documents.
   * @see https://mongodb.github.io/node-mongodb-native/3.1/api/Collection.html#find
   *
   * @param {string} name - The name of collection to find.
   * @param {object} [args] - The arguments of find command.
   * @param {object} [args.query={}] - The cursor query object.
   * @param {number} [args.limit=0] - The limit for the cursor query.
   * @param {object} [args.sort={ _id: -1 }] - The sort for the cursor query.
   * @param {object} [args.projection=null] - The field projection object.
   * @param {number} [args.skip=0] - The skip for the cursor query.
   * @param {object} [args.options={}] -
   * @param {object} [args.options.hint=null] -
   * @param {boolean} [args.options.explain=false] -
   * @param {boolean} [args.options.snapshot=false] -
   * @param {boolean} [args.options.timeout=false] -
   * @param {boolean} [args.options.tailable=false] -
   * @param {number} [args.options.batchSize=0] -
   * @param {boolean} [args.options.returnKey=false] -
   * @param {number} [args.options.returnKeymaxScan=null] -
   * @param {number} [args.options.min=null] -
   * @param {number} [args.options.max=null] -
   * @param {boolean} [args.options.showDiskLoc=false] -
   * @param {string} [args.options.comment=null] -
   * @param {boolean} [args.options.raw=false] -
   * @param {boolean} [args.options.promoteLongs=true] -
   * @param {boolean} [args.options.promoteValues=true] -
   * @param {boolean} [args.options.promoteBuffers=false] -
   * @param {ReadPreference|string} [args.options.readPreference=null] -
   * @param {boolean} [args.options.partial=false] -
   * @param {number} [args.options.maxTimeMS=null] -
   * @param {object} [args.options.collation=null] -
   * @param {ClientSession} [args.options.session] -
   * @param {boolean} [returnCursor=false] -
   *
   * @return {Promise<object[]|Cursor>} result
   */
  async find(name, args = {}, returnCursor = false) {
    const query = args.query || {};
    const options = args.options || {};
    options.skip = args.skip || 0;
    options.limit = args.limit || 0;
    // version 3.0 recommand using projection of options
    // instead of project which is a method of cursor
    options.projection = args.projection || args.project || {};
    options.sort = args.sort || { _id: -1 };
 
    const cursor = await this.db.collection(name).find(query, options);
 
    return returnCursor ? cursor : cursor.toArray();
  }
 
  /**
   * Count number of matching documents in the db to a query.
   *
   * @deprecated
   *
   * @see https://mongodb.github.io/node-mongodb-native/3.1/api/Collection.html#count
   *
   * @param {string} name - The name of collection to find.
   * @param {object} args - The arguments of find command.
   * @param {object} [args.query=null] - The query for the count.
   * @param {object} [args.options=null] - Optional settings.
   * @param {number} [args.options.limit=0] - The limit of documents to count.
   * @param {number} [args.options.skip=0] - The number of documents to skip for the count.
   * @param {string} [args.options.hint=null] - An index name hint for the query.
   * @param {(ReadPreference|string)} [args.options.readPreference=null] -The preferred read preference
   * @param {number} [args.options.maxTimeMS=null] - Number of miliseconds to wait before aborting the query.
   * @param {ClientSession} [args.options.session] -
   *
   * @return {Promise<number>} result
   */
  count(name, args = {}) {
    const { query, options } = args;
 
    return this.db.collection(name).count(query, options);
  }
 
  /**
   * Gets the number of documents matching the filter.
   *
   * @see https://mongodb.github.io/node-mongodb-native/3.1/api/Collection.html#countDocuments
   *
   * @param {string} name - The name of collection to find.
   * @param {object} args - The arguments of find command.
   * @param {object} [args.query=null] - The query for the count.
   * @param {object} [args.options=null] - Optional settings.
   * @param {object} [args.options.collation] - Specifies a collation.
   * @param {string|object} [args.options.hint] - The index to use.
   * @param {number} [args.options.limit] - The maximum number of document to count.
   * @param {number} [args.options.maxTimeMS] - The maximum amount of time to allow the operation to run.
   * @param {number} [args.options.skip] - The number of documents to skip before counting.
   *
   * @return {Promise<number>} result
   */
  countDocuments(name, args = {}) {
    const { query, options } = args;
 
    return this.db.collection(name).countDocuments(query, options);
  }
 
  /**
   * Gets an estimate of the count of documents in a collection using collection metadata.
   *
   * @see https://mongodb.github.io/node-mongodb-native/3.1/api/Collection.html#estimatedDocumentCount
   *
   * @param {string} name - The name of collection to find.
   * @param {object} [args] - The arguments of find command.
   * @param {object} [options] - Optional settings.
   * @param {number} [options.maxTimeMS] - The maximum amount of time to allow the operation to run.
   *
   * @return {Promise<number>} result
   */
  estimatedDocumentCount(name, args = {}) {
    return this.db.collection(name).countDocuments(args.options);
  }
 
  /**
   * The distinct command returns a list of distinct values for the given key across a collection.
   *
   * @see https://mongodb.github.io/node-mongodb-native/3.1/api/Collection.html#distinct
   *
   * @param {string} name - The name of collection to find.
   * @param {object} args - The arguments of find command.
   * @param {string} args.key - Field of the document to find distinct values for.
   * @param {object} [args.query={}] - The query for filtering the set of documents to which we apply the distinct filter.
   * @param {object} [args.options=null] - Optional settings.
   * @param {(ReadPreference|string)} [args.options.readPreference=null] -The preferred read preference
   * @param {number} [args.options.maxTimeMS=null] - Number of miliseconds to wait before aborting the query.
   * @param {ClientSession} [args.options.session] -
   *
   * @return {Promise<object[]>} result
   */
  distinct(name, args = {}) {
    const { key, query, options } = args;
 
    return this.db.collection(name).distinct(key, query, options);
  }
 
  /**
   * Creates an index on the db and collection collection.
   *
   * @see https://mongodb.github.io/node-mongodb-native/3.1/api/Collection.html#createIndex
   *
   * @param {string} name - The name of collection.
   * @param {object} args - Arguments.
   * @param {(object|string)} args.fieldOrSpec - Defines the index.
   * @param {object} [args.options={}] -
   * @param {(number|string)} [args.options.w=null] - The write concern.
   * @param {number} [args.options.wtimeout=null] - The write concern timeout.
   * @param {boolean} [args.options.j=false] - Specify a journal write concern.
   * @param {boolean} [args.options.unique=false] - Creates an unique index.
   * @param {boolean} [args.options.sparse=false] - Creates a sparse index.
   * @param {boolean} [args.options.background=false] - Creates the index in the background, yielding whenever possible.
   * @param {boolean} [args.options.dropDups=false] - A unique index cannot be created on a key that has pre-existing duplicate values.
   * If you would like to create the index anyway,
   * keeping the first document the database indexes
   * and deleting all subsequent documents that have duplicate value.
   * @param {number} [args.options.min=null] - For geospatial indexes set the lower bound for the co-ordinates.
   * @param {number} [args.options.max=null] - For geospatial indexes set the high bound for the co-ordinates.
   * @param {number} [args.options.v=null] - Specify the format version of the indexes.
   * @param {number} [args.options.expireAfterSeconds=null] - Allows you to expire data on indexes applied to a data (MongoDB 2.2 or higher).
   * @param {string} [args.options.name=null] - Override the autogenerated index name (useful if the resulting name is larger than 128 bytes).
   * @param {object} [args.options.partialFilterExpression=null] - Override the autogenerated index name (useful if the resulting name is larger than 128 bytes).
   * @param {object} [args.options.collation=null] - Override the autogenerated index name (useful if the resulting name is larger than 128 bytes).
   * @param {ClientSession} [args.options.session] -
   *
   * @return {Promise<string>} - Promise
   */
  createIndex(name, args = {}) {
    const fieldOrSpec = args.fieldOrSpec;
    const options = args.options || {};
    return this.db.collection(name).createIndex(fieldOrSpec, options);
  }
 
  /**
   * Get the list of all collection information for the specified db.
   *
   * @see https://mongodb.github.io/node-mongodb-native/3.1/api/Db.html#listCollections
   *
   * @param {object} [args] - Arguments.
   * @param {object} [args.filter={}] - Query to filter collections by
   * @param {object} [args.options={}] - Optional settings.
   * @param {number} [args.options.batchSize=null] - The batchSize for the returned command cursor or if pre 2.8 the systems batch collection.
   * @param {ReadPreference|string} [args.options.readPreference=null] -
   * @param {ClientSession} [args.options.session] -
   *
   * @return {Promise<string[]>} result - Array of collections name.
   */
  listCollections(args = {}) {
    const filter = args.filter || {};
    const options = args.options || {};
    return this.db
      .listCollections(filter, options)
      .toArray()
      .then(result => result.map(col => col.name));
  }
 
  /**
   * Create a new collection on a server with the specified options. Use this to create capped collections.
   * More information about command options available at https://docs.mongodb.com/manual/reference/command/create/
   *
   * @see https://mongodb.github.io/node-mongodb-native/3.1/api/Db.html#createCollection
   *
   * @param {object} args - Arguments.
   * @param {string} args.name - The collection name we wish to access.
   * @param {object} [args.options={}] - Optional settings.
   * @param {(number|string)} [args.options.w=null] - The write concern.
   * @param {number} [args.options.wtimeout=null] - The write concern timeout.
   * @param {boolean} [args.options.j=false] - Specify a journal write concern.
   * @param {boolean} [args.options.raw=false] - Return document results as raw BSON buffers.
   * @param {object} [args.options.pkFactory=null] - A primary key factory object for generation of custom _id keys.
   * @param {(ReadPreference|string)} [args.options.readPreference=null] - The preferred read preference
   * @param {boolean} [args.options.serializeFunctions=false] - Serialize functions on any object.
   * @param {boolean} [args.options.strict=false] - Returns an error if the collection does not exist.
   * @param {boolean} [args.options.capped=false] - Create a capped collection.
   * @param {boolean} [args.options.autoIndexId=true] - Create an index on the _id field of the document,
   * @param {number} [args.options.size=null] - The size of the capped collection in bytes.
   * @param {number} [args.options.max=null] - The maximum number of documents in the capped collection.
   * @param {number} [args.options.flags=null] - The maximum number of documents in the capped collection.
   * @param {object} [args.options.storageEngine=null] -
   * @param {object} [args.options.validator=null] -
   * @param {string} [args.options.validationLevel=null] -
   * @param {string} [args.options.validationAction=null] -
   * @param {string} [args.options.indexOptionDefaults=null] -
   * @param {string} [args.options.viewOn=null] -
   * @param {object[]} [args.options.pipeline=null] -
   * @param {object} [args.options.collation=null] -
   * @param {ClientSession} [args.options.session] -
   *
   * @return {Promise<Collection>} - collection
   */
  createCollection(args = {}) {
    const name = args.name;
    const options = args.options || {};
    return this.db.createCollection(name, options);
  }
 
  /**
   * Execute an aggregation framework pipeline against the collection.
   *
   * @see https://mongodb.github.io/node-mongodb-native/3.1/api/Collection.html#aggregate
   *
   * @param {string} name - Collection name.
   * @param {object} args - Arguments.
   * @param {object[]} args.pipeline - Array containing all the aggregation framework commands for the execution.
   * @param {object} [args.options=null] -
   * @param {ReadPreference|string} [options.readPreference=null] -
   * @param {object} [options.cursor=null] - Return the query as cursor.
   * @param {number} [options.cursor.batchSize=null] -
   * @param {boolean} [options.explain=false] -
   * @param {boolean} [options.allowDiskUse=false] -
   * @param {number} [options.maxTimeMS=null] -
   * @param {boolean} [options.bypassDocumentValidation=false] -
   * @param {boolean} [options.raw=false] -
   * @param {boolean} [options.promoteLongs=true] -
   * @param {boolean} [options.promoteValues=true] -
   * @param {boolean} [options.promoteBuffers=false] -
   * @param {object} [options.collation=null] -
   * @param {string} [options.comment] -
   * @param {ClientSession} [args.options.session] -
   *
   * @return {Promise<object[]>} result
   */
  aggregate(name, args = {}) {
    const { pipeline, options } = args;
    return this.db
      .collection(name)
      .aggregate(pipeline, options)
      .toArray();
  }
 
  /**
   * Starts a new session on the server
   *
   * @see http://mongodb.github.io/node-mongodb-native/3.1/api/MongoClient.html#startSession
   *
   * @param {object} options - optional settings for a driver session
   * @param {boolean} causalConsistency - Whether causal consistency should be enabled on this session
   * @param {object} defaultTransactionOptions - The default TransactionOptions to use for transactions started on this session
   *
   * @return {object} clientSession
   */
  /* istanbul ignore next */
  startSession(args = {}) {
    const options = Object.assign({}, args.options);
    return this.client.startSession(options);
  }
 
  /**
   * Starts a new transaction with the given options
   *
   * @see http://mongodb.github.io/node-mongodb-native/3.1/api/ClientSession.html#startTransaction
   *
   * @param {object} args
   * @param {object} [args.options] - Options for the transaction
   *
   * @return {object} clientSeesion
   */
  /* istanbul ignore next */
  startTransaction(args = {}) {
    const options = Object.assign({}, args.options);
    const session = this.startSession();
    session.startTransaction(options);
    return session;
  }
}
 
module.exports = { default: MongoDB };