-
Notifications
You must be signed in to change notification settings - Fork 31
/
collection.js
executable file
·3179 lines (2741 loc) · 119 KB
/
collection.js
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
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
"use strict";
var checkCollectionName = require('./utils').checkCollectionName
, ObjectID = require('mongodb-core').BSON.ObjectID
, Long = require('mongodb-core').BSON.Long
, Code = require('mongodb-core').BSON.Code
, f = require('util').format
, AggregationCursor = require('./aggregation_cursor')
, MongoError = require('mongodb-core').MongoError
, shallowClone = require('./utils').shallowClone
, isObject = require('./utils').isObject
, toError = require('./utils').toError
, normalizeHintField = require('./utils').normalizeHintField
, handleCallback = require('./utils').handleCallback
, decorateCommand = require('./utils').decorateCommand
, formattedOrderClause = require('./utils').formattedOrderClause
, ReadPreference = require('./read_preference')
, CoreReadPreference = require('mongodb-core').ReadPreference
, CommandCursor = require('./command_cursor')
, Define = require('./metadata')
, Cursor = require('./cursor')
, unordered = require('./bulk/unordered')
, ordered = require('./bulk/ordered');
/**
* @fileOverview The **Collection** class is an internal class that embodies a MongoDB collection
* allowing for insert/update/remove/find and other command operation on that MongoDB collection.
*
* **COLLECTION Cannot directly be instantiated**
* @example
* var MongoClient = require('mongodb').MongoClient,
* test = require('assert');
* // Connection url
* var url = 'mongodb://localhost:27017/test';
* // Connect using MongoClient
* MongoClient.connect(url, function(err, db) {
* // Create a collection we want to drop later
* var col = db.collection('createIndexExample1');
* // Show that duplicate records got dropped
* col.find({}).toArray(function(err, items) {
* test.equal(null, err);
* test.equal(4, items.length);
* db.close();
* });
* });
*/
/**
* Create a new Collection instance (INTERNAL TYPE, do not instantiate directly)
* @class
* @property {string} collectionName Get the collection name.
* @property {string} namespace Get the full collection namespace.
* @property {object} writeConcern The current write concern values.
* @property {object} readConcern The current read concern values.
* @property {object} hint Get current index hint for collection.
* @return {Collection} a Collection instance.
*/
var Collection = function(db, topology, dbName, name, pkFactory, options) {
checkCollectionName(name);
var self = this;
// Unpack variables
var internalHint = null;
var opts = options != null && ('object' === typeof options) ? options : {};
var slaveOk = options == null || options.slaveOk == null ? db.slaveOk : options.slaveOk;
var serializeFunctions = options == null || options.serializeFunctions == null ? db.serializeFunctions : options.serializeFunctions;
var raw = options == null || options.raw == null ? db.raw : options.raw;
var readPreference = null;
var collectionHint = null;
var namespace = f("%s.%s", dbName, name);
// Get the promiseLibrary
var promiseLibrary = options.promiseLibrary;
// No promise library selected fall back
if(!promiseLibrary) {
promiseLibrary = typeof global.Promise == 'function' ?
global.Promise : require('es6-promise').Promise;
}
// Assign the right collection level readPreference
if(options && options.readPreference) {
readPreference = options.readPreference;
} else if(db.options.readPreference) {
readPreference = db.options.readPreference;
}
// Set custom primary key factory if provided
pkFactory = pkFactory == null
? ObjectID
: pkFactory;
// Internal state
this.s = {
// Set custom primary key factory if provided
pkFactory: pkFactory
// Db
, db: db
// Topology
, topology: topology
// dbName
, dbName: dbName
// Options
, options: options
// Namespace
, namespace: namespace
// Read preference
, readPreference: readPreference
// Raw
, raw: raw
// SlaveOK
, slaveOk: slaveOk
// Serialize functions
, serializeFunctions: serializeFunctions
// internalHint
, internalHint: internalHint
// collectionHint
, collectionHint: collectionHint
// Name
, name: name
// Promise library
, promiseLibrary: promiseLibrary
// Read Concern
, readConcern: options.readConcern
}
}
var define = Collection.define = new Define('Collection', Collection, false);
Object.defineProperty(Collection.prototype, 'collectionName', {
enumerable: true, get: function() { return this.s.name; }
});
Object.defineProperty(Collection.prototype, 'namespace', {
enumerable: true, get: function() { return this.s.namespace; }
});
Object.defineProperty(Collection.prototype, 'readConcern', {
enumerable: true, get: function() { return this.s.readConcern || {level: 'local'}; }
});
Object.defineProperty(Collection.prototype, 'writeConcern', {
enumerable:true,
get: function() {
var ops = {};
if(this.s.options.w != null) ops.w = this.s.options.w;
if(this.s.options.j != null) ops.j = this.s.options.j;
if(this.s.options.fsync != null) ops.fsync = this.s.options.fsync;
if(this.s.options.wtimeout != null) ops.wtimeout = this.s.options.wtimeout;
return ops;
}
});
/**
* @ignore
*/
Object.defineProperty(Collection.prototype, "hint", {
enumerable: true
, get: function () { return this.s.collectionHint; }
, set: function (v) { this.s.collectionHint = normalizeHintField(v); }
});
/**
* Creates a cursor for a query that can be used to iterate over results from MongoDB
* @method
* @param {object} query The cursor query object.
* @throws {MongoError}
* @return {Cursor}
*/
Collection.prototype.find = function() {
var options
, args = Array.prototype.slice.call(arguments, 0)
, has_callback = typeof args[args.length - 1] === 'function'
, has_weird_callback = typeof args[0] === 'function'
, callback = has_callback ? args.pop() : (has_weird_callback ? args.shift() : null)
, len = args.length
, selector = len >= 1 ? args[0] : {}
, fields = len >= 2 ? args[1] : undefined;
if(len === 1 && has_weird_callback) {
// backwards compat for callback?, options case
selector = {};
options = args[0];
}
if(len === 2 && fields !== undefined && !Array.isArray(fields)) {
var fieldKeys = Object.keys(fields);
var is_option = false;
for(var i = 0; i < fieldKeys.length; i++) {
if(testForFields[fieldKeys[i]] != null) {
is_option = true;
break;
}
}
if(is_option) {
options = fields;
fields = undefined;
} else {
options = {};
}
} else if(len === 2 && Array.isArray(fields) && !Array.isArray(fields[0])) {
var newFields = {};
// Rewrite the array
for(var i = 0; i < fields.length; i++) {
newFields[fields[i]] = 1;
}
// Set the fields
fields = newFields;
}
if(3 === len) {
options = args[2];
}
// Ensure selector is not null
selector = selector == null ? {} : selector;
// Validate correctness off the selector
var object = selector;
if(Buffer.isBuffer(object)) {
var object_size = object[0] | object[1] << 8 | object[2] << 16 | object[3] << 24;
if(object_size != object.length) {
var error = new Error("query selector raw message size does not match message header size [" + object.length + "] != [" + object_size + "]");
error.name = 'MongoError';
throw error;
}
}
// Validate correctness of the field selector
var object = fields;
if(Buffer.isBuffer(object)) {
var object_size = object[0] | object[1] << 8 | object[2] << 16 | object[3] << 24;
if(object_size != object.length) {
var error = new Error("query fields raw message size does not match message header size [" + object.length + "] != [" + object_size + "]");
error.name = 'MongoError';
throw error;
}
}
// Check special case where we are using an objectId
if(selector instanceof ObjectID || (selector != null && selector._bsontype == 'ObjectID')) {
selector = {_id:selector};
}
// If it's a serialized fields field we need to just let it through
// user be warned it better be good
if(options && options.fields && !(Buffer.isBuffer(options.fields))) {
fields = {};
if(Array.isArray(options.fields)) {
if(!options.fields.length) {
fields['_id'] = 1;
} else {
for (var i = 0, l = options.fields.length; i < l; i++) {
fields[options.fields[i]] = 1;
}
}
} else {
fields = options.fields;
}
}
if (!options) options = {};
var newOptions = {};
// Make a shallow copy of options
for (var key in options) {
newOptions[key] = options[key];
}
// Unpack options
newOptions.skip = len > 3 ? args[2] : options.skip ? options.skip : 0;
newOptions.limit = len > 3 ? args[3] : options.limit ? options.limit : 0;
newOptions.raw = options.raw != null && typeof options.raw === 'boolean' ? options.raw : this.s.raw;
newOptions.hint = options.hint != null ? normalizeHintField(options.hint) : this.s.collectionHint;
newOptions.timeout = len == 5 ? args[4] : typeof options.timeout === 'undefined' ? undefined : options.timeout;
// // If we have overridden slaveOk otherwise use the default db setting
newOptions.slaveOk = options.slaveOk != null ? options.slaveOk : this.s.db.slaveOk;
// Add read preference if needed
newOptions = getReadPreference(this, newOptions, this.s.db, this);
// Set slave ok to true if read preference different from primary
if(newOptions.readPreference != null
&& (newOptions.readPreference != 'primary' || newOptions.readPreference.mode != 'primary')) {
newOptions.slaveOk = true;
}
// Ensure the query is an object
if(selector != null && typeof selector != 'object') {
throw MongoError.create({message: "query selector must be an object", driver:true });
}
// Build the find command
var findCommand = {
find: this.s.namespace
, limit: newOptions.limit
, skip: newOptions.skip
, query: selector
}
// Ensure we use the right await data option
if(typeof newOptions.awaitdata == 'boolean') {
newOptions.awaitData = newOptions.awaitdata
};
// Translate to new command option noCursorTimeout
if(typeof newOptions.timeout == 'boolean') newOptions.noCursorTimeout = newOptions.timeout;
// Merge in options to command
for(var name in newOptions) {
if(newOptions[name] != null) findCommand[name] = newOptions[name];
}
// Format the fields
var formatFields = function(fields) {
var object = {};
if(Array.isArray(fields)) {
for(var i = 0; i < fields.length; i++) {
if(Array.isArray(fields[i])) {
object[fields[i][0]] = fields[i][1];
} else {
object[fields[i][0]] = 1;
}
}
} else {
object = fields;
}
return object;
}
// Special treatment for the fields selector
if(fields) findCommand.fields = formatFields(fields);
// Add db object to the new options
newOptions.db = this.s.db;
// Add the promise library
newOptions.promiseLibrary = this.s.promiseLibrary;
// Set raw if available at collection level
if(newOptions.raw == null && this.s.raw) newOptions.raw = this.s.raw;
// Sort options
if(findCommand.sort)
findCommand.sort = formattedOrderClause(findCommand.sort);
// Set the readConcern
if(this.s.readConcern) {
findCommand.readConcern = this.s.readConcern;
}
// Create the cursor
if(typeof callback == 'function') return handleCallback(callback, null, this.s.topology.cursor(this.s.namespace, findCommand, newOptions));
return this.s.topology.cursor(this.s.namespace, findCommand, newOptions);
}
define.classMethod('find', {callback: false, promise:false, returns: [Cursor]});
/**
* 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.
*
* @method
* @param {object} doc Document to insert.
* @param {object} [options=null] Optional settings.
* @param {(number|string)} [options.w=null] The write concern.
* @param {number} [options.wtimeout=null] The write concern timeout.
* @param {boolean} [options.j=false] Specify a journal write concern.
* @param {boolean} [options.serializeFunctions=false] Serialize functions on any object.
* @param {boolean} [options.forceServerObjectId=false] Force server to assign _id values instead of driver.
* @param {boolean} [options.bypassDocumentValidation=false] Allow driver to bypass schema validation in MongoDB 3.2 or higher.
* @param {Collection~insertOneWriteOpCallback} [callback] The command result callback
* @return {Promise} returns Promise if no callback passed
*/
Collection.prototype.insertOne = function(doc, options, callback) {
var self = this;
if(typeof options == 'function') callback = options, options = {};
options = options || {};
if(Array.isArray(doc)) return callback(MongoError.create({message: 'doc parameter must be an object', driver:true }));
// Add ignoreUndfined
if(this.s.options.ignoreUndefined) {
options = shallowClone(options);
options.ignoreUndefined = this.s.options.ignoreUndefined;
}
// Execute using callback
if(typeof callback == 'function') return insertOne(self, doc, options, callback);
// Return a Promise
return new this.s.promiseLibrary(function(resolve, reject) {
insertOne(self, doc, options, function(err, r) {
if(err) return reject(err);
resolve(r);
});
});
}
var insertOne = function(self, doc, options, callback) {
insertDocuments(self, [doc], options, function(err, r) {
if(callback == null) return;
if(err && callback) return callback(err);
// Workaround for pre 2.6 servers
if(r == null) return callback(null, {result: {ok:1}});
// Add values to top level to ensure crud spec compatibility
r.insertedCount = r.result.n;
r.insertedId = doc._id;
if(callback) callback(null, r);
});
}
var mapInserManyResults = function(docs, r) {
var ids = r.getInsertedIds();
var keys = Object.keys(ids);
var finalIds = new Array(keys);
for(var i = 0; i < keys.length; i++) {
if(ids[keys[i]]._id) {
finalIds[ids[keys[i]].index] = ids[keys[i]]._id;
}
}
var finalResult = {
result: {ok: 1, n: r.insertedCount},
ops: docs,
insertedCount: r.insertedCount,
insertedIds: finalIds
};
if(r.getLastOp()) {
finalResult.result.opTime = r.getLastOp();
}
return finalResult;
}
define.classMethod('insertOne', {callback: true, promise:true});
/**
* 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.
*
* @method
* @param {object[]} docs Documents to insert.
* @param {object} [options=null] Optional settings.
* @param {(number|string)} [options.w=null] The write concern.
* @param {number} [options.wtimeout=null] The write concern timeout.
* @param {boolean} [options.j=false] Specify a journal write concern.
* @param {boolean} [options.serializeFunctions=false] Serialize functions on any object.
* @param {boolean} [options.forceServerObjectId=false] Force server to assign _id values instead of driver.
* @param {boolean} [options.bypassDocumentValidation=false] Allow driver to bypass schema validation in MongoDB 3.2 or higher.
* @param {Collection~insertWriteOpCallback} [callback] The command result callback
* @return {Promise} returns Promise if no callback passed
*/
Collection.prototype.insertMany = function(docs, options, callback) {
var self = this;
if(typeof options == 'function') callback = options, options = {};
options = options || {ordered:true};
if(!Array.isArray(docs)) return callback(MongoError.create({message: 'docs parameter must be an array of documents', driver:true }));
// Get the write concern options
if(typeof options.checkKeys != 'boolean') {
options.checkKeys = true;
}
// If keep going set unordered
options['serializeFunctions'] = options['serializeFunctions'] || self.s.serializeFunctions;
// Set up the force server object id
var forceServerObjectId = typeof options.forceServerObjectId == 'boolean'
? options.forceServerObjectId : self.s.db.options.forceServerObjectId;
// Do we want to force the server to assign the _id key
if(forceServerObjectId !== true) {
// Add _id if not specified
for(var i = 0; i < docs.length; i++) {
if(docs[i]._id == null) docs[i]._id = self.s.pkFactory.createPk();
}
}
// Generate the bulk write operations
var operations = [{
insertMany: docs
}];
// Execute using callback
if(typeof callback == 'function') return bulkWrite(self, operations, options, function(err, r) {
if(err) return callback(err, r);
callback(null, mapInserManyResults(docs, r));
});
// Return a Promise
return new this.s.promiseLibrary(function(resolve, reject) {
bulkWrite(self, operations, options, function(err, r) {
if(err) return reject(err);
resolve(mapInserManyResults(docs, r));
});
});
}
define.classMethod('insertMany', {callback: true, promise:true});
/**
* @typedef {Object} Collection~BulkWriteOpResult
* @property {number} insertedCount Number of documents inserted.
* @property {number} matchedCount Number of documents matched for update.
* @property {number} modifiedCount Number of documents modified.
* @property {number} deletedCount Number of documents deleted.
* @property {number} upsertedCount Number of documents upserted.
* @property {object} insertedIds Inserted document generated Id's, hash key is the index of the originating operation
* @property {object} upsertedIds Upserted document generated Id's, hash key is the index of the originating operation
* @property {object} result The command result object.
*/
/**
* The callback format for inserts
* @callback Collection~bulkWriteOpCallback
* @param {MongoError} error An error instance representing the error during the execution.
* @param {Collection~BulkWriteOpResult} result The result object if the command was executed successfully.
*/
/**
* Perform a bulkWrite operation without a fluent API
*
* Legal operation types are
*
* { insertOne: { document: { a: 1 } } }
*
* { updateOne: { filter: {a:2}, update: {$set: {a:2}}, upsert:true } }
*
* { updateMany: { filter: {a:2}, update: {$set: {a:2}}, upsert:true } }
*
* { deleteOne: { filter: {c:1} } }
*
* { deleteMany: { filter: {c:1} } }
*
* { replaceOne: { filter: {c:3}, replacement: {c:4}, upsert:true}}
*
* 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.
*
* @method
* @param {object[]} operations Bulk operations to perform.
* @param {object} [options=null] Optional settings.
* @param {(number|string)} [options.w=null] The write concern.
* @param {number} [options.wtimeout=null] The write concern timeout.
* @param {boolean} [options.j=false] Specify a journal write concern.
* @param {boolean} [options.serializeFunctions=false] Serialize functions on any object.
* @param {boolean} [options.ordered=true] Execute write operation in ordered or unordered fashion.
* @param {boolean} [options.bypassDocumentValidation=false] Allow driver to bypass schema validation in MongoDB 3.2 or higher.
* @param {Collection~bulkWriteOpCallback} [callback] The command result callback
* @return {Promise} returns Promise if no callback passed
*/
Collection.prototype.bulkWrite = function(operations, options, callback) {
var self = this;
if(typeof options == 'function') callback = options, options = {};
options = options || {ordered:true};
if(!Array.isArray(operations)) {
throw MongoError.create({message: "operations must be an array of documents", driver:true });
}
// Execute using callback
if(typeof callback == 'function') return bulkWrite(self, operations, options, callback);
// Return a Promise
return new this.s.promiseLibrary(function(resolve, reject) {
bulkWrite(self, operations, options, function(err, r) {
if(err) return reject(err);
resolve(r);
});
});
}
var bulkWrite = function(self, operations, options, callback) {
// Add ignoreUndfined
if(self.s.options.ignoreUndefined) {
options = shallowClone(options);
options.ignoreUndefined = self.s.options.ignoreUndefined;
}
// Create the bulk operation
var bulk = options.ordered == true || options.ordered == null ? self.initializeOrderedBulkOp(options) : self.initializeUnorderedBulkOp(options);
// for each op go through and add to the bulk
for(var i = 0; i < operations.length; i++) {
bulk.raw(operations[i]);
}
// Final options for write concern
var finalOptions = writeConcern(shallowClone(options), self.s.db, self, options);
var writeCon = finalOptions.writeConcern ? finalOptions.writeConcern : {};
// Execute the bulk
bulk.execute(writeCon, function(err, r) {
// We have connection level error
if(!r && err) return callback(err, null);
// We have single error
if(r && r.hasWriteErrors() && r.getWriteErrorCount() == 1) {
return callback(toError(r.getWriteErrorAt(0)), r);
}
// if(err) return callback(err);
r.insertedCount = r.nInserted;
r.matchedCount = r.nMatched;
r.modifiedCount = r.nModified || 0;
r.deletedCount = r.nRemoved;
r.upsertedCount = r.getUpsertedIds().length;
r.upsertedIds = {};
r.insertedIds = {};
// Update the n
r.n = r.insertedCount;
// Inserted documents
var inserted = r.getInsertedIds();
// Map inserted ids
for(var i = 0; i < inserted.length; i++) {
r.insertedIds[inserted[i].index] = inserted[i]._id;
}
// Upserted documents
var upserted = r.getUpsertedIds();
// Map upserted ids
for(var i = 0; i < upserted.length; i++) {
r.upsertedIds[upserted[i].index] = upserted[i]._id;
}
// Check if we have write errors
if(r.hasWriteErrors()) {
// Get all the errors
var errors = r.getWriteErrors();
// Return the MongoError object
return callback(toError({
message: 'write operation failed', code: errors[0].code, writeErrors: errors
}), r);
}
// Check if we have a writeConcern error
if(r.getWriteConcernError()) {
// Return the MongoError object
return callback(toError(r.getWriteConcernError()), r);
}
// Return the results
callback(null, r);
});
}
var insertDocuments = function(self, docs, options, callback) {
if(typeof options == 'function') callback = options, options = {};
options = options || {};
// Ensure we are operating on an array op docs
docs = Array.isArray(docs) ? docs : [docs];
// Get the write concern options
var finalOptions = writeConcern(shallowClone(options), self.s.db, self, options);
if(typeof finalOptions.checkKeys != 'boolean') finalOptions.checkKeys = true;
// If keep going set unordered
if(finalOptions.keepGoing == true) finalOptions.ordered = false;
finalOptions['serializeFunctions'] = options['serializeFunctions'] || self.s.serializeFunctions;
// Set up the force server object id
var forceServerObjectId = typeof options.forceServerObjectId == 'boolean'
? options.forceServerObjectId : self.s.db.options.forceServerObjectId;
// Add _id if not specified
if(forceServerObjectId !== true){
for(var i = 0; i < docs.length; i++) {
if(docs[i]._id == null) docs[i]._id = self.s.pkFactory.createPk();
}
}
// File inserts
self.s.topology.insert(self.s.namespace, docs, finalOptions, function(err, result) {
if(callback == null) return;
if(err) return handleCallback(callback, err);
if(result == null) return handleCallback(callback, null, null);
if(result.result.code) return handleCallback(callback, toError(result.result));
if(result.result.writeErrors) return handleCallback(callback, toError(result.result.writeErrors[0]));
// Add docs to the list
result.ops = docs;
// Return the results
handleCallback(callback, null, result);
});
}
define.classMethod('bulkWrite', {callback: true, promise:true});
/**
* @typedef {Object} Collection~WriteOpResult
* @property {object[]} ops All the documents inserted using insertOne/insertMany/replaceOne. Documents contain the _id field if forceServerObjectId == false for insertOne/insertMany
* @property {object} connection The connection object used for the operation.
* @property {object} result The command result object.
*/
/**
* The callback format for inserts
* @callback Collection~writeOpCallback
* @param {MongoError} error An error instance representing the error during the execution.
* @param {Collection~WriteOpResult} result The result object if the command was executed successfully.
*/
/**
* @typedef {Object} Collection~insertWriteOpResult
* @property {Number} insertedCount The total amount of documents inserted.
* @property {object[]} ops All the documents inserted using insertOne/insertMany/replaceOne. Documents contain the _id field if forceServerObjectId == false for insertOne/insertMany
* @property {ObjectId[]} insertedIds All the generated _id's for the inserted documents.
* @property {object} connection The connection object used for the operation.
* @property {object} result The raw command result object returned from MongoDB (content might vary by server version).
* @property {Number} result.ok Is 1 if the command executed correctly.
* @property {Number} result.n The total count of documents inserted.
*/
/**
* @typedef {Object} Collection~insertOneWriteOpResult
* @property {Number} insertedCount The total amount of documents inserted.
* @property {object[]} ops All the documents inserted using insertOne/insertMany/replaceOne. Documents contain the _id field if forceServerObjectId == false for insertOne/insertMany
* @property {ObjectId} insertedId The driver generated ObjectId for the insert operation.
* @property {object} connection The connection object used for the operation.
* @property {object} result The raw command result object returned from MongoDB (content might vary by server version).
* @property {Number} result.ok Is 1 if the command executed correctly.
* @property {Number} result.n The total count of documents inserted.
*/
/**
* The callback format for inserts
* @callback Collection~insertWriteOpCallback
* @param {MongoError} error An error instance representing the error during the execution.
* @param {Collection~insertWriteOpResult} result The result object if the command was executed successfully.
*/
/**
* The callback format for inserts
* @callback Collection~insertOneWriteOpCallback
* @param {MongoError} error An error instance representing the error during the execution.
* @param {Collection~insertOneWriteOpResult} result The result object if the command was executed successfully.
*/
/**
* Inserts a single document or a 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.
*
* @method
* @param {(object|object[])} docs Documents to insert.
* @param {object} [options=null] Optional settings.
* @param {(number|string)} [options.w=null] The write concern.
* @param {number} [options.wtimeout=null] The write concern timeout.
* @param {boolean} [options.j=false] Specify a journal write concern.
* @param {boolean} [options.serializeFunctions=false] Serialize functions on any object.
* @param {boolean} [options.forceServerObjectId=false] Force server to assign _id values instead of driver.
* @param {boolean} [options.bypassDocumentValidation=false] Allow driver to bypass schema validation in MongoDB 3.2 or higher.
* @param {Collection~insertWriteOpCallback} [callback] The command result callback
* @return {Promise} returns Promise if no callback passed
* @deprecated Use insertOne, insertMany or bulkWrite
*/
Collection.prototype.insert = function(docs, options, callback) {
var self = this;
if(typeof options == 'function') callback = options, options = {};
options = options || {ordered:false};
docs = !Array.isArray(docs) ? [docs] : docs;
if(options.keepGoing == true) {
options.ordered = false;
}
return this.insertMany(docs, options, callback);
}
define.classMethod('insert', {callback: true, promise:true});
/**
* @typedef {Object} Collection~updateWriteOpResult
* @property {Object} result The raw result returned from MongoDB, field will vary depending on server version.
* @property {Number} result.ok Is 1 if the command executed correctly.
* @property {Number} result.n The total count of documents scanned.
* @property {Number} result.nModified The total count of documents modified.
* @property {Object} connection The connection object used for the operation.
* @property {Number} matchedCount The number of documents that matched the filter.
* @property {Number} modifiedCount The number of documents that were modified.
* @property {Number} upsertedCount The number of documents upserted.
* @property {Object} upsertedId The upserted id.
* @property {ObjectId} upsertedId._id The upserted _id returned from the server.
*/
/**
* The callback format for inserts
* @callback Collection~updateWriteOpCallback
* @param {MongoError} error An error instance representing the error during the execution.
* @param {Collection~updateWriteOpResult} result The result object if the command was executed successfully.
*/
/**
* Update a single document on MongoDB
* @method
* @param {object} filter The Filter used to select the document to update
* @param {object} update The update operations to be applied to the document
* @param {object} [options=null] Optional settings.
* @param {boolean} [options.upsert=false] Update operation is an upsert.
* @param {(number|string)} [options.w=null] The write concern.
* @param {number} [options.wtimeout=null] The write concern timeout.
* @param {boolean} [options.j=false] Specify a journal write concern.
* @param {boolean} [options.bypassDocumentValidation=false] Allow driver to bypass schema validation in MongoDB 3.2 or higher.
* @param {Collection~updateWriteOpCallback} [callback] The command result callback
* @return {Promise} returns Promise if no callback passed
*/
Collection.prototype.updateOne = function(filter, update, options, callback) {
var self = this;
if(typeof options == 'function') callback = options, options = {};
options = shallowClone(options)
// Add ignoreUndfined
if(this.s.options.ignoreUndefined) {
options = shallowClone(options);
options.ignoreUndefined = this.s.options.ignoreUndefined;
}
// Execute using callback
if(typeof callback == 'function') return updateOne(self, filter, update, options, callback);
// Return a Promise
return new this.s.promiseLibrary(function(resolve, reject) {
updateOne(self, filter, update, options, function(err, r) {
if(err) return reject(err);
resolve(r);
});
});
}
var updateOne = function(self, filter, update, options, callback) {
// Set single document update
options.multi = false;
// Execute update
updateDocuments(self, filter, update, options, function(err, r) {
if(callback == null) return;
if(err && callback) return callback(err);
if(r == null) return callback(null, {result: {ok:1}});
r.matchedCount = r.result.n;
r.modifiedCount = r.result.nModified != null ? r.result.nModified : r.result.n;
r.upsertedId = Array.isArray(r.result.upserted) && r.result.upserted.length > 0 ? r.result.upserted[0] : null;
r.upsertedCount = Array.isArray(r.result.upserted) && r.result.upserted.length ? r.result.upserted.length : 0;
if(callback) callback(null, r);
});
}
define.classMethod('updateOne', {callback: true, promise:true});
/**
* Replace a document on MongoDB
* @method
* @param {object} filter The Filter used to select the document to update
* @param {object} doc The Document that replaces the matching document
* @param {object} [options=null] Optional settings.
* @param {boolean} [options.upsert=false] Update operation is an upsert.
* @param {(number|string)} [options.w=null] The write concern.
* @param {number} [options.wtimeout=null] The write concern timeout.
* @param {boolean} [options.j=false] Specify a journal write concern.
* @param {boolean} [options.bypassDocumentValidation=false] Allow driver to bypass schema validation in MongoDB 3.2 or higher.
* @param {Collection~updateWriteOpCallback} [callback] The command result callback
* @return {Promise} returns Promise if no callback passed
*/
Collection.prototype.replaceOne = function(filter, update, options, callback) {
var self = this;
if(typeof options == 'function') callback = options, options = {};
options = shallowClone(options)
// Add ignoreUndfined
if(this.s.options.ignoreUndefined) {
options = shallowClone(options);
options.ignoreUndefined = this.s.options.ignoreUndefined;
}
// Execute using callback
if(typeof callback == 'function') return replaceOne(self, filter, update, options, callback);
// Return a Promise
return new this.s.promiseLibrary(function(resolve, reject) {
replaceOne(self, filter, update, options, function(err, r) {
if(err) return reject(err);
resolve(r);
});
});
}
var replaceOne = function(self, filter, update, options, callback) {
// Set single document update
options.multi = false;
// Execute update
updateDocuments(self, filter, update, options, function(err, r) {
if(callback == null) return;
if(err && callback) return callback(err);
if(r == null) return callback(null, {result: {ok:1}});
r.matchedCount = r.result.n;
r.modifiedCount = r.result.nModified != null ? r.result.nModified : r.result.n;
r.upsertedId = Array.isArray(r.result.upserted) && r.result.upserted.length > 0 ? r.result.upserted[0] : null;
r.upsertedCount = Array.isArray(r.result.upserted) && r.result.upserted.length ? r.result.upserted.length : 0;
r.ops = [update];
if(callback) callback(null, r);
});
}
define.classMethod('replaceOne', {callback: true, promise:true});
/**
* Update multiple documents on MongoDB
* @method
* @param {object} filter The Filter used to select the document to update
* @param {object} update The update operations to be applied to the document
* @param {object} [options=null] Optional settings.
* @param {boolean} [options.upsert=false] Update operation is an upsert.
* @param {(number|string)} [options.w=null] The write concern.
* @param {number} [options.wtimeout=null] The write concern timeout.
* @param {boolean} [options.j=false] Specify a journal write concern.
* @param {Collection~updateWriteOpCallback} [callback] The command result callback
* @return {Promise} returns Promise if no callback passed
*/
Collection.prototype.updateMany = function(filter, update, options, callback) {
var self = this;
if(typeof options == 'function') callback = options, options = {};
options = shallowClone(options)
// Add ignoreUndfined
if(this.s.options.ignoreUndefined) {
options = shallowClone(options);
options.ignoreUndefined = this.s.options.ignoreUndefined;
}
// Execute using callback
if(typeof callback == 'function') return updateMany(self, filter, update, options, callback);
// Return a Promise
return new this.s.promiseLibrary(function(resolve, reject) {
updateMany(self, filter, update, options, function(err, r) {
if(err) return reject(err);
resolve(r);
});
});
}
var updateMany = function(self, filter, update, options, callback) {
// Set single document update
options.multi = true;
// Execute update
updateDocuments(self, filter, update, options, function(err, r) {
if(callback == null) return;
if(err && callback) return callback(err);
if(r == null) return callback(null, {result: {ok:1}});
r.matchedCount = r.result.n;
r.modifiedCount = r.result.nModified != null ? r.result.nModified : r.result.n;
r.upsertedId = Array.isArray(r.result.upserted) && r.result.upserted.length > 0 ? r.result.upserted[0] : null;
r.upsertedCount = Array.isArray(r.result.upserted) && r.result.upserted.length ? r.result.upserted.length : 0;
if(callback) callback(null, r);
});
}
define.classMethod('updateMany', {callback: true, promise:true});
var updateDocuments = function(self, selector, document, options, callback) {
if('function' === typeof options) callback = options, options = null;
if(options == null) options = {};
if(!('function' === typeof callback)) callback = null;
// If we are not providing a selector or document throw
if(selector == null || typeof selector != 'object') return callback(toError("selector must be a valid JavaScript object"));
if(document == null || typeof document != 'object') return callback(toError("document must be a valid JavaScript object"));
// Get the write concern options
var finalOptions = writeConcern(shallowClone(options), self.s.db, self, options);
// Do we return the actual result document
// Either use override on the function, or go back to default on either the collection
// level or db
finalOptions['serializeFunctions'] = options['serializeFunctions'] || self.s.serializeFunctions;
// Execute the operation
var op = {q: selector, u: document};
op.upsert = typeof options.upsert == 'boolean' ? options.upsert : false;
op.multi = typeof options.multi == 'boolean' ? options.multi : false;
// Update options
self.s.topology.update(self.s.namespace, [op], finalOptions, function(err, result) {
if(callback == null) return;
if(err) return handleCallback(callback, err, null);
if(result == null) return handleCallback(callback, null, null);
if(result.result.code) return handleCallback(callback, toError(result.result));
if(result.result.writeErrors) return handleCallback(callback, toError(result.result.writeErrors[0]));
// Return the results
handleCallback(callback, null, result);
});
}
/**
* Updates documents.
* @method