mongoose 6.4.4 → 6.4.7
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.
- package/lib/aggregate.js +32 -30
- package/lib/browser.js +1 -1
- package/lib/cast.js +2 -2
- package/lib/collection.js +1 -1
- package/lib/connection.js +34 -16
- package/lib/document.js +175 -102
- package/lib/helpers/timestamps/setDocumentTimestamps.js +26 -0
- package/lib/helpers/timestamps/setupTimestamps.js +14 -18
- package/lib/helpers/topology/isAtlas.js +14 -9
- package/lib/index.js +3 -2
- package/lib/model.js +117 -159
- package/lib/query.js +18 -12
- package/lib/schema.js +8 -1
- package/lib/schematype.js +95 -81
- package/lib/virtualtype.js +14 -16
- package/package.json +4 -4
- package/types/connection.d.ts +1 -1
- package/types/expressions.d.ts +5 -4
- package/types/index.d.ts +9 -7
- package/types/indexes.d.ts +2 -2
- package/types/inferschematype.d.ts +26 -10
- package/types/models.d.ts +5 -5
package/lib/document.js
CHANGED
|
@@ -193,6 +193,46 @@ function Document(obj, fields, skipId, options) {
|
|
|
193
193
|
applyQueue(this);
|
|
194
194
|
}
|
|
195
195
|
|
|
196
|
+
/**
|
|
197
|
+
* Boolean flag specifying if the document is new. If you create a document
|
|
198
|
+
* using `new`, this document will be considered "new". `$isNew` is how
|
|
199
|
+
* Mongoose determines whether `save()` should use `insertOne()` to create
|
|
200
|
+
* a new document or `updateOne()` to update an existing document.
|
|
201
|
+
*
|
|
202
|
+
* #### Example:
|
|
203
|
+
*
|
|
204
|
+
* const user = new User({ name: 'John Smith' });
|
|
205
|
+
* user.$isNew; // true
|
|
206
|
+
*
|
|
207
|
+
* await user.save(); // Sends an `insertOne` to MongoDB
|
|
208
|
+
*
|
|
209
|
+
* On the other hand, if you load an existing document from the database
|
|
210
|
+
* using `findOne()` or another [query operation](/docs/queries.html),
|
|
211
|
+
* `$isNew` will be false.
|
|
212
|
+
*
|
|
213
|
+
* #### Example:
|
|
214
|
+
*
|
|
215
|
+
* const user = await User.findOne({ name: 'John Smith' });
|
|
216
|
+
* user.$isNew; // false
|
|
217
|
+
*
|
|
218
|
+
* For subdocuments, `$isNew` is true if either the parent has `$isNew` set,
|
|
219
|
+
* or if you create a new subdocument.
|
|
220
|
+
*
|
|
221
|
+
* #### Example:
|
|
222
|
+
*
|
|
223
|
+
* // Assume `Group` has a document array `users`
|
|
224
|
+
* const group = await Group.findOne();
|
|
225
|
+
* group.users[0].$isNew; // false
|
|
226
|
+
*
|
|
227
|
+
* group.users.push({ name: 'John Smith' });
|
|
228
|
+
* group.users[1].$isNew; // true
|
|
229
|
+
*
|
|
230
|
+
* @api public
|
|
231
|
+
* @property $isNew
|
|
232
|
+
* @memberOf Document
|
|
233
|
+
* @instance
|
|
234
|
+
*/
|
|
235
|
+
|
|
196
236
|
Object.defineProperty(Document.prototype, 'isNew', {
|
|
197
237
|
get: function() {
|
|
198
238
|
return this.$isNew;
|
|
@@ -202,6 +242,15 @@ Object.defineProperty(Document.prototype, 'isNew', {
|
|
|
202
242
|
}
|
|
203
243
|
});
|
|
204
244
|
|
|
245
|
+
/**
|
|
246
|
+
* Hash containing current validation errors.
|
|
247
|
+
*
|
|
248
|
+
* @api public
|
|
249
|
+
* @property errors
|
|
250
|
+
* @memberOf Document
|
|
251
|
+
* @instance
|
|
252
|
+
*/
|
|
253
|
+
|
|
205
254
|
Object.defineProperty(Document.prototype, 'errors', {
|
|
206
255
|
get: function() {
|
|
207
256
|
return this.$errors;
|
|
@@ -210,6 +259,7 @@ Object.defineProperty(Document.prototype, 'errors', {
|
|
|
210
259
|
this.$errors = value;
|
|
211
260
|
}
|
|
212
261
|
});
|
|
262
|
+
|
|
213
263
|
/*!
|
|
214
264
|
* Document exposes the NodeJS event emitter API, so you can use
|
|
215
265
|
* `on`, `once`, etc.
|
|
@@ -298,52 +348,13 @@ Object.defineProperty(Document.prototype, '$locals', {
|
|
|
298
348
|
}
|
|
299
349
|
});
|
|
300
350
|
|
|
301
|
-
|
|
302
|
-
/**
|
|
303
|
-
* Boolean flag specifying if the document is new. If you create a document
|
|
304
|
-
* using `new`, this document will be considered "new". `$isNew` is how
|
|
305
|
-
* Mongoose determines whether `save()` should use `insertOne()` to create
|
|
306
|
-
* a new document or `updateOne()` to update an existing document.
|
|
307
|
-
*
|
|
308
|
-
* #### Example:
|
|
309
|
-
* const user = new User({ name: 'John Smith' });
|
|
310
|
-
* user.$isNew; // true
|
|
311
|
-
*
|
|
312
|
-
* await user.save(); // Sends an `insertOne` to MongoDB
|
|
313
|
-
*
|
|
314
|
-
* On the other hand, if you load an existing document from the database
|
|
315
|
-
* using `findOne()` or another [query operation](/docs/queries.html),
|
|
316
|
-
* `$isNew` will be false.
|
|
317
|
-
*
|
|
318
|
-
* #### Example:
|
|
319
|
-
* const user = await User.findOne({ name: 'John Smith' });
|
|
320
|
-
* user.$isNew; // false
|
|
321
|
-
*
|
|
322
|
-
* For subdocuments, `$isNew` is true if either the parent has `$isNew` set,
|
|
323
|
-
* or if you create a new subdocument.
|
|
324
|
-
*
|
|
325
|
-
* #### Example:
|
|
326
|
-
* // Assume `Group` has a document array `users`
|
|
327
|
-
* const group = await Group.findOne();
|
|
328
|
-
* group.users[0].$isNew; // false
|
|
329
|
-
*
|
|
330
|
-
* group.users.push({ name: 'John Smith' });
|
|
331
|
-
* group.users[1].$isNew; // true
|
|
332
|
-
*
|
|
333
|
-
* @api public
|
|
334
|
-
* @property $isNew
|
|
335
|
-
* @memberOf Document
|
|
336
|
-
* @instance
|
|
337
|
-
*/
|
|
338
|
-
|
|
339
|
-
Document.prototype.$isNew;
|
|
340
|
-
|
|
341
351
|
/**
|
|
342
352
|
* Legacy alias for `$isNew`.
|
|
343
353
|
*
|
|
344
354
|
* @api public
|
|
345
355
|
* @property isNew
|
|
346
356
|
* @memberOf Document
|
|
357
|
+
* @see $isNew #document_Document-$isNew
|
|
347
358
|
* @instance
|
|
348
359
|
*/
|
|
349
360
|
|
|
@@ -400,17 +411,6 @@ Document.prototype.id;
|
|
|
400
411
|
|
|
401
412
|
Document.prototype.$errors;
|
|
402
413
|
|
|
403
|
-
/**
|
|
404
|
-
* Hash containing current validation errors.
|
|
405
|
-
*
|
|
406
|
-
* @api public
|
|
407
|
-
* @property errors
|
|
408
|
-
* @memberOf Document
|
|
409
|
-
* @instance
|
|
410
|
-
*/
|
|
411
|
-
|
|
412
|
-
Document.prototype.errors;
|
|
413
|
-
|
|
414
414
|
/**
|
|
415
415
|
* A string containing the current operation that Mongoose is executing
|
|
416
416
|
* on this document. May be `null`, `'save'`, `'validate'`, or `'remove'`.
|
|
@@ -630,6 +630,8 @@ function $applyDefaultsToNested(val, path, doc) {
|
|
|
630
630
|
* @param {Object} obj
|
|
631
631
|
* @param {Object} [fields]
|
|
632
632
|
* @param {Boolean} [skipId]
|
|
633
|
+
* @param {Boolean} [exclude]
|
|
634
|
+
* @param {Object} [hasIncludedChildren]
|
|
633
635
|
* @api private
|
|
634
636
|
* @method $__buildDoc
|
|
635
637
|
* @memberOf Document
|
|
@@ -714,6 +716,8 @@ Document.prototype.toBSON = function() {
|
|
|
714
716
|
* Note that `init` hooks are [synchronous](/docs/middleware.html#synchronous).
|
|
715
717
|
*
|
|
716
718
|
* @param {Object} doc document returned by mongo
|
|
719
|
+
* @param {Object} [opts]
|
|
720
|
+
* @param {Function} [fn]
|
|
717
721
|
* @api public
|
|
718
722
|
* @memberOf Document
|
|
719
723
|
* @instance
|
|
@@ -734,10 +738,25 @@ Document.prototype.init = function(doc, opts, fn) {
|
|
|
734
738
|
return this;
|
|
735
739
|
};
|
|
736
740
|
|
|
741
|
+
/**
|
|
742
|
+
* Alias for [`.init`](#document_Document-init)
|
|
743
|
+
*
|
|
744
|
+
* @api public
|
|
745
|
+
*/
|
|
746
|
+
|
|
737
747
|
Document.prototype.$init = function() {
|
|
738
748
|
return this.constructor.prototype.init.apply(this, arguments);
|
|
739
749
|
};
|
|
740
750
|
|
|
751
|
+
/**
|
|
752
|
+
* Internal "init" function
|
|
753
|
+
*
|
|
754
|
+
* @param {Document} doc
|
|
755
|
+
* @param {Object} [opts]
|
|
756
|
+
* @returns {Document} this
|
|
757
|
+
* @api private
|
|
758
|
+
*/
|
|
759
|
+
|
|
741
760
|
Document.prototype.$__init = function(doc, opts) {
|
|
742
761
|
this.$isNew = false;
|
|
743
762
|
opts = opts || {};
|
|
@@ -871,17 +890,17 @@ function init(self, obj, doc, opts, prefix) {
|
|
|
871
890
|
*
|
|
872
891
|
* #### Example:
|
|
873
892
|
*
|
|
874
|
-
* weirdCar.update({$inc: {wheels:1}}, { w: 1 }, callback);
|
|
893
|
+
* weirdCar.update({ $inc: { wheels:1 } }, { w: 1 }, callback);
|
|
875
894
|
*
|
|
876
895
|
* #### Valid options:
|
|
877
896
|
*
|
|
878
|
-
* - same as in [Model.update](#model_Model
|
|
897
|
+
* - same as in [Model.update](#model_Model-update)
|
|
879
898
|
*
|
|
880
|
-
* @see Model.update #model_Model
|
|
881
|
-
* @param {Object}
|
|
882
|
-
* @param {Object} options
|
|
883
|
-
* @param {Function} callback
|
|
884
|
-
* @return {Query}
|
|
899
|
+
* @see Model.update #model_Model-update
|
|
900
|
+
* @param {...Object} ops
|
|
901
|
+
* @param {Object} [options]
|
|
902
|
+
* @param {Function} [callback]
|
|
903
|
+
* @return {Query} this
|
|
885
904
|
* @api public
|
|
886
905
|
* @memberOf Document
|
|
887
906
|
* @instance
|
|
@@ -910,15 +929,15 @@ Document.prototype.update = function update() {
|
|
|
910
929
|
*
|
|
911
930
|
* #### Valid options:
|
|
912
931
|
*
|
|
913
|
-
* - same as in [Model.updateOne](#model_Model
|
|
932
|
+
* - same as in [Model.updateOne](#model_Model-updateOne)
|
|
914
933
|
*
|
|
915
|
-
* @see Model.updateOne #model_Model
|
|
934
|
+
* @see Model.updateOne #model_Model-updateOne
|
|
916
935
|
* @param {Object} doc
|
|
917
936
|
* @param {Object} [options] optional see [`Query.prototype.setOptions()`](https://mongoosejs.com/docs/api.html#query_Query-setOptions)
|
|
918
937
|
* @param {Object} [options.lean] if truthy, mongoose will return the document as a plain JavaScript object rather than a mongoose document. See [`Query.lean()`](/docs/api.html#query_Query-lean) and the [Mongoose lean tutorial](/docs/tutorials/lean.html).
|
|
919
938
|
* @param {Boolean|String} [options.strict] overwrites the schema's [strict mode option](https://mongoosejs.com/docs/guide.html#strict)
|
|
920
939
|
* @param {Boolean} [options.timestamps=null] If set to `false` and [schema-level timestamps](/docs/guide.html#timestamps) are enabled, skip timestamps for this update. Note that this allows you to overwrite timestamps. Does nothing if schema-level timestamps are not set.
|
|
921
|
-
* @param {Function} callback
|
|
940
|
+
* @param {Function} [callback]
|
|
922
941
|
* @return {Query}
|
|
923
942
|
* @api public
|
|
924
943
|
* @memberOf Document
|
|
@@ -952,12 +971,12 @@ Document.prototype.updateOne = function updateOne(doc, options, callback) {
|
|
|
952
971
|
*
|
|
953
972
|
* #### Valid options:
|
|
954
973
|
*
|
|
955
|
-
* - same as in [Model.replaceOne](https://mongoosejs.com/docs/api/model.html#model_Model
|
|
974
|
+
* - same as in [Model.replaceOne](https://mongoosejs.com/docs/api/model.html#model_Model-replaceOne)
|
|
956
975
|
*
|
|
957
|
-
* @see Model.replaceOne #model_Model
|
|
976
|
+
* @see Model.replaceOne #model_Model-replaceOne
|
|
958
977
|
* @param {Object} doc
|
|
959
|
-
* @param {Object} options
|
|
960
|
-
* @param {Function} callback
|
|
978
|
+
* @param {Object} [options]
|
|
979
|
+
* @param {Function} [callback]
|
|
961
980
|
* @return {Query}
|
|
962
981
|
* @api public
|
|
963
982
|
* @memberOf Document
|
|
@@ -1030,10 +1049,10 @@ Document.prototype.$session = function $session(session) {
|
|
|
1030
1049
|
*
|
|
1031
1050
|
* @param {Object} obj the object to overwrite this document with
|
|
1032
1051
|
* @method overwrite
|
|
1033
|
-
* @name overwrite
|
|
1034
1052
|
* @memberOf Document
|
|
1035
1053
|
* @instance
|
|
1036
1054
|
* @api public
|
|
1055
|
+
* @return {Document} this
|
|
1037
1056
|
*/
|
|
1038
1057
|
|
|
1039
1058
|
Document.prototype.overwrite = function overwrite(obj) {
|
|
@@ -1064,8 +1083,8 @@ Document.prototype.overwrite = function overwrite(obj) {
|
|
|
1064
1083
|
* @param {Schema|String|Number|Buffer|*} [type] optionally specify a type for "on-the-fly" attributes
|
|
1065
1084
|
* @param {Object} [options] optionally specify options that modify the behavior of the set
|
|
1066
1085
|
* @param {Boolean} [options.merge=false] if true, setting a [nested path](/docs/subdocs.html#subdocuments-versus-nested-paths) will merge existing values rather than overwrite the whole object. So `doc.set('nested', { a: 1, b: 2 })` becomes `doc.set('nested.a', 1); doc.set('nested.b', 2);`
|
|
1086
|
+
* @return {Document} this
|
|
1067
1087
|
* @method $set
|
|
1068
|
-
* @name $set
|
|
1069
1088
|
* @memberOf Document
|
|
1070
1089
|
* @instance
|
|
1071
1090
|
* @api public
|
|
@@ -1149,8 +1168,8 @@ Document.prototype.$set = function $set(path, val, type, options) {
|
|
|
1149
1168
|
}
|
|
1150
1169
|
|
|
1151
1170
|
if (utils.isNonBuiltinObject(valForKey) && pathtype === 'nested') {
|
|
1152
|
-
$applyDefaultsToNested(path[key], prefix + key, this);
|
|
1153
1171
|
this.$set(prefix + key, path[key], constructing, Object.assign({}, options, { _skipMarkModified: true }));
|
|
1172
|
+
$applyDefaultsToNested(this.$get(prefix + key), prefix + key, this);
|
|
1154
1173
|
continue;
|
|
1155
1174
|
} else if (strict) {
|
|
1156
1175
|
// Don't overwrite defaults with undefined keys (gh-3981) (gh-9039)
|
|
@@ -1554,6 +1573,7 @@ function _isManuallyPopulatedArray(val, ref) {
|
|
|
1554
1573
|
|
|
1555
1574
|
/**
|
|
1556
1575
|
* Sets the value of a path, or many paths.
|
|
1576
|
+
* Alias for [`.$set`](#document_Document-$set).
|
|
1557
1577
|
*
|
|
1558
1578
|
* #### Example:
|
|
1559
1579
|
*
|
|
@@ -1581,6 +1601,7 @@ function _isManuallyPopulatedArray(val, ref) {
|
|
|
1581
1601
|
* @param {Any} val the value to set
|
|
1582
1602
|
* @param {Schema|String|Number|Buffer|*} [type] optionally specify a type for "on-the-fly" attributes
|
|
1583
1603
|
* @param {Object} [options] optionally specify options that modify the behavior of the set
|
|
1604
|
+
* @return {Document} this
|
|
1584
1605
|
* @api public
|
|
1585
1606
|
* @method set
|
|
1586
1607
|
* @memberOf Document
|
|
@@ -1592,6 +1613,14 @@ Document.prototype.set = Document.prototype.$set;
|
|
|
1592
1613
|
/**
|
|
1593
1614
|
* Determine if we should mark this change as modified.
|
|
1594
1615
|
*
|
|
1616
|
+
* @param {never} pathToMark UNUSED
|
|
1617
|
+
* @param {String|Symbol} path
|
|
1618
|
+
* @param {Object} options
|
|
1619
|
+
* @param {Any} constructing
|
|
1620
|
+
* @param {never} parts UNUSED
|
|
1621
|
+
* @param {Schema} schema
|
|
1622
|
+
* @param {Any} val
|
|
1623
|
+
* @param {Any} priorVal
|
|
1595
1624
|
* @return {Boolean}
|
|
1596
1625
|
* @api private
|
|
1597
1626
|
* @method $__shouldModify
|
|
@@ -1652,6 +1681,14 @@ Document.prototype.$__shouldModify = function(pathToMark, path, options, constru
|
|
|
1652
1681
|
/**
|
|
1653
1682
|
* Handles the actual setting of the value and marking the path modified if appropriate.
|
|
1654
1683
|
*
|
|
1684
|
+
* @param {String} pathToMark
|
|
1685
|
+
* @param {String|Symbol} path
|
|
1686
|
+
* @param {Object} options
|
|
1687
|
+
* @param {Any} constructing
|
|
1688
|
+
* @param {Array} parts
|
|
1689
|
+
* @param {Schema} schema
|
|
1690
|
+
* @param {Any} val
|
|
1691
|
+
* @param {Any} priorVal
|
|
1655
1692
|
* @api private
|
|
1656
1693
|
* @method $__set
|
|
1657
1694
|
* @memberOf Document
|
|
@@ -1727,6 +1764,7 @@ Document.prototype.$__set = function(pathToMark, path, options, constructing, pa
|
|
|
1727
1764
|
* Gets a raw value from a path (no getters)
|
|
1728
1765
|
*
|
|
1729
1766
|
* @param {String} path
|
|
1767
|
+
* @return {Any} Returns the value from the given `path`.
|
|
1730
1768
|
* @api private
|
|
1731
1769
|
*/
|
|
1732
1770
|
|
|
@@ -1739,6 +1777,7 @@ Document.prototype.$__getValue = function(path) {
|
|
|
1739
1777
|
*
|
|
1740
1778
|
* @param {String} path
|
|
1741
1779
|
* @param {Object} value
|
|
1780
|
+
* @return {Document} this
|
|
1742
1781
|
* @api private
|
|
1743
1782
|
*/
|
|
1744
1783
|
|
|
@@ -1763,6 +1802,7 @@ Document.prototype.$__setValue = function(path, val) {
|
|
|
1763
1802
|
* @param {Object} [options]
|
|
1764
1803
|
* @param {Boolean} [options.virtuals=false] Apply virtuals before getting this path
|
|
1765
1804
|
* @param {Boolean} [options.getters=true] If false, skip applying getters and just get the raw value
|
|
1805
|
+
* @return {Any}
|
|
1766
1806
|
* @api public
|
|
1767
1807
|
*/
|
|
1768
1808
|
|
|
@@ -1831,10 +1871,12 @@ Document.prototype.get = function(path, type, options) {
|
|
|
1831
1871
|
|
|
1832
1872
|
Document.prototype[getSymbol] = Document.prototype.get;
|
|
1833
1873
|
Document.prototype.$get = Document.prototype.get;
|
|
1874
|
+
|
|
1834
1875
|
/**
|
|
1835
1876
|
* Returns the schematype for the given `path`.
|
|
1836
1877
|
*
|
|
1837
1878
|
* @param {String} path
|
|
1879
|
+
* @return {SchemaPath}
|
|
1838
1880
|
* @api private
|
|
1839
1881
|
* @method $__path
|
|
1840
1882
|
* @memberOf Document
|
|
@@ -1924,6 +1966,7 @@ Document.prototype.$ignore = function(path) {
|
|
|
1924
1966
|
* because a child of `a` was directly modified.
|
|
1925
1967
|
*
|
|
1926
1968
|
* #### Example:
|
|
1969
|
+
*
|
|
1927
1970
|
* const schema = new Schema({ foo: String, nested: { bar: String } });
|
|
1928
1971
|
* const Model = mongoose.model('Test', schema);
|
|
1929
1972
|
* await Model.create({ foo: 'original', nested: { bar: 'original' } });
|
|
@@ -1933,7 +1976,7 @@ Document.prototype.$ignore = function(path) {
|
|
|
1933
1976
|
* doc.directModifiedPaths(); // ['nested.bar']
|
|
1934
1977
|
* doc.modifiedPaths(); // ['nested', 'nested.bar']
|
|
1935
1978
|
*
|
|
1936
|
-
* @return {
|
|
1979
|
+
* @return {String[]}
|
|
1937
1980
|
* @api public
|
|
1938
1981
|
*/
|
|
1939
1982
|
|
|
@@ -1947,6 +1990,7 @@ Document.prototype.directModifiedPaths = function() {
|
|
|
1947
1990
|
* [minimize option](/docs/guide.html#minimize).
|
|
1948
1991
|
*
|
|
1949
1992
|
* #### Example:
|
|
1993
|
+
*
|
|
1950
1994
|
* const schema = new Schema({ nested: { foo: String } });
|
|
1951
1995
|
* const Model = mongoose.model('Test', schema);
|
|
1952
1996
|
* const doc = new Model({});
|
|
@@ -1957,6 +2001,7 @@ Document.prototype.directModifiedPaths = function() {
|
|
|
1957
2001
|
* doc.$isEmpty('nested'); // false
|
|
1958
2002
|
* doc.nested.$isEmpty(); // false
|
|
1959
2003
|
*
|
|
2004
|
+
* @param {String} [path]
|
|
1960
2005
|
* @memberOf Document
|
|
1961
2006
|
* @instance
|
|
1962
2007
|
* @api public
|
|
@@ -1989,6 +2034,10 @@ Document.prototype.$isEmpty = function(path) {
|
|
|
1989
2034
|
return Object.keys(this.toObject(isEmptyOptions)).length === 0;
|
|
1990
2035
|
};
|
|
1991
2036
|
|
|
2037
|
+
/*!
|
|
2038
|
+
* ignore
|
|
2039
|
+
*/
|
|
2040
|
+
|
|
1992
2041
|
function _isEmpty(v) {
|
|
1993
2042
|
if (v == null) {
|
|
1994
2043
|
return true;
|
|
@@ -2009,7 +2058,7 @@ function _isEmpty(v) {
|
|
|
2009
2058
|
*
|
|
2010
2059
|
* @param {Object} [options]
|
|
2011
2060
|
* @param {Boolean} [options.includeChildren=false] if true, returns children of modified paths as well. For example, if false, the list of modified paths for `doc.colors = { primary: 'blue' };` will **not** contain `colors.primary`. If true, `modifiedPaths()` will return an array that contains `colors.primary`.
|
|
2012
|
-
* @return {
|
|
2061
|
+
* @return {String[]}
|
|
2013
2062
|
* @api public
|
|
2014
2063
|
*/
|
|
2015
2064
|
|
|
@@ -2118,6 +2167,14 @@ Document.prototype.isModified = function(paths, modifiedPaths) {
|
|
|
2118
2167
|
return this.$__.activePaths.some('modify');
|
|
2119
2168
|
};
|
|
2120
2169
|
|
|
2170
|
+
/**
|
|
2171
|
+
* Alias of [`.isModified`](#document_Document-isModified)
|
|
2172
|
+
*
|
|
2173
|
+
* @method $isModified
|
|
2174
|
+
* @memberOf Document
|
|
2175
|
+
* @api public
|
|
2176
|
+
*/
|
|
2177
|
+
|
|
2121
2178
|
Document.prototype.$isModified = Document.prototype.isModified;
|
|
2122
2179
|
|
|
2123
2180
|
Document.prototype[documentIsModified] = Document.prototype.isModified;
|
|
@@ -2160,6 +2217,7 @@ Document.prototype.$isDefault = function(path) {
|
|
|
2160
2217
|
* Getter/setter, determines whether the document was removed or not.
|
|
2161
2218
|
*
|
|
2162
2219
|
* #### Example:
|
|
2220
|
+
*
|
|
2163
2221
|
* const product = await product.remove();
|
|
2164
2222
|
* product.$isDeleted(); // true
|
|
2165
2223
|
* product.remove(); // no-op, doesn't send anything to the db
|
|
@@ -2170,7 +2228,7 @@ Document.prototype.$isDefault = function(path) {
|
|
|
2170
2228
|
*
|
|
2171
2229
|
*
|
|
2172
2230
|
* @param {Boolean} [val] optional, overrides whether mongoose thinks the doc is deleted
|
|
2173
|
-
* @return {Boolean} whether mongoose thinks this doc is deleted.
|
|
2231
|
+
* @return {Boolean|Document} whether mongoose thinks this doc is deleted.
|
|
2174
2232
|
* @method $isDeleted
|
|
2175
2233
|
* @memberOf Document
|
|
2176
2234
|
* @instance
|
|
@@ -2195,7 +2253,7 @@ Document.prototype.$isDeleted = function(val) {
|
|
|
2195
2253
|
* doc.isDirectModified('documents.0.title') // true
|
|
2196
2254
|
* doc.isDirectModified('documents') // false
|
|
2197
2255
|
*
|
|
2198
|
-
* @param {String|String[]} path
|
|
2256
|
+
* @param {String|String[]} [path]
|
|
2199
2257
|
* @return {Boolean}
|
|
2200
2258
|
* @api public
|
|
2201
2259
|
*/
|
|
@@ -2220,7 +2278,7 @@ Document.prototype.isDirectModified = function(path) {
|
|
|
2220
2278
|
/**
|
|
2221
2279
|
* Checks if `path` is in the `init` state, that is, it was set by `Document#init()` and not modified since.
|
|
2222
2280
|
*
|
|
2223
|
-
* @param {String} path
|
|
2281
|
+
* @param {String} [path]
|
|
2224
2282
|
* @return {Boolean}
|
|
2225
2283
|
* @api public
|
|
2226
2284
|
*/
|
|
@@ -2404,7 +2462,7 @@ Document.prototype.isDirectSelected = function isDirectSelected(path) {
|
|
|
2404
2462
|
* @param {Boolean} [options.validateModifiedOnly=false] if `true` mongoose validates only modified paths.
|
|
2405
2463
|
* @param {Array|string} [options.pathsToSkip] list of paths to skip. If set, Mongoose will validate every modified path that is not in this list.
|
|
2406
2464
|
* @param {Function} [callback] optional callback called after validation completes, passing an error if one occurred
|
|
2407
|
-
* @return {Promise} Promise
|
|
2465
|
+
* @return {Promise} Returns a Promise if no `callback` is given.
|
|
2408
2466
|
* @api public
|
|
2409
2467
|
*/
|
|
2410
2468
|
|
|
@@ -2460,6 +2518,14 @@ Document.prototype.validate = function(pathsToValidate, options, callback) {
|
|
|
2460
2518
|
}, this.constructor.events);
|
|
2461
2519
|
};
|
|
2462
2520
|
|
|
2521
|
+
/**
|
|
2522
|
+
* Alias of [`.validate`](#document_Document-validate)
|
|
2523
|
+
*
|
|
2524
|
+
* @method $validate
|
|
2525
|
+
* @memberOf Document
|
|
2526
|
+
* @api public
|
|
2527
|
+
*/
|
|
2528
|
+
|
|
2463
2529
|
Document.prototype.$validate = Document.prototype.validate;
|
|
2464
2530
|
|
|
2465
2531
|
/*!
|
|
@@ -2854,6 +2920,7 @@ function _handlePathsToValidate(paths, pathsToValidate) {
|
|
|
2854
2920
|
/*!
|
|
2855
2921
|
* ignore
|
|
2856
2922
|
*/
|
|
2923
|
+
|
|
2857
2924
|
function _handlePathsToSkip(paths, pathsToSkip) {
|
|
2858
2925
|
pathsToSkip = new Set(pathsToSkip);
|
|
2859
2926
|
paths = paths.filter(p => !pathsToSkip.has(p));
|
|
@@ -2876,7 +2943,7 @@ function _handlePathsToSkip(paths, pathsToSkip) {
|
|
|
2876
2943
|
* // validation passed
|
|
2877
2944
|
* }
|
|
2878
2945
|
*
|
|
2879
|
-
* @param {Array|string} pathsToValidate only validate the given paths
|
|
2946
|
+
* @param {Array|string} [pathsToValidate] only validate the given paths
|
|
2880
2947
|
* @param {Object} [options] options for validation
|
|
2881
2948
|
* @param {Boolean} [options.validateModifiedOnly=false] If `true`, Mongoose will only validate modified paths, as opposed to modified paths and `required` paths.
|
|
2882
2949
|
* @param {Array|string} [options.pathsToSkip] list of paths to skip. If set, Mongoose will validate every modified path that is not in this list.
|
|
@@ -2985,7 +3052,7 @@ Document.prototype.validateSync = function(pathsToValidate, options) {
|
|
|
2985
3052
|
* The `value` argument (if passed) will be available through the `ValidationError.value` property.
|
|
2986
3053
|
*
|
|
2987
3054
|
* doc.invalidate('size', 'must be less than 20', 14);
|
|
2988
|
-
|
|
3055
|
+
*
|
|
2989
3056
|
* doc.validate(function (err) {
|
|
2990
3057
|
* console.log(err)
|
|
2991
3058
|
* // prints
|
|
@@ -3001,8 +3068,8 @@ Document.prototype.validateSync = function(pathsToValidate, options) {
|
|
|
3001
3068
|
* })
|
|
3002
3069
|
*
|
|
3003
3070
|
* @param {String} path the field to invalidate. For array elements, use the `array.i.field` syntax, where `i` is the 0-based index in the array.
|
|
3004
|
-
* @param {String|Error}
|
|
3005
|
-
* @param {Object|String|Number|any}
|
|
3071
|
+
* @param {String|Error} err the error which states the reason `path` was invalid
|
|
3072
|
+
* @param {Object|String|Number|any} val optional invalid value
|
|
3006
3073
|
* @param {String} [kind] optional `kind` property for the error
|
|
3007
3074
|
* @return {ValidationError} the current ValidationError, with all currently invalidated paths
|
|
3008
3075
|
* @api public
|
|
@@ -3138,7 +3205,7 @@ function _checkImmutableSubpaths(subdoc, schematype, priorVal) {
|
|
|
3138
3205
|
/**
|
|
3139
3206
|
* Checks if a path is invalid
|
|
3140
3207
|
*
|
|
3141
|
-
* @param {String|String[]} path the field to check
|
|
3208
|
+
* @param {String|String[]} [path] the field to check. If unset will always return "false"
|
|
3142
3209
|
* @method $isValid
|
|
3143
3210
|
* @memberOf Document
|
|
3144
3211
|
* @instance
|
|
@@ -3167,7 +3234,7 @@ Document.prototype.$isValid = function(path) {
|
|
|
3167
3234
|
* Resets the internal modified state of this document.
|
|
3168
3235
|
*
|
|
3169
3236
|
* @api private
|
|
3170
|
-
* @return {Document}
|
|
3237
|
+
* @return {Document} this
|
|
3171
3238
|
* @method $__reset
|
|
3172
3239
|
* @memberOf Document
|
|
3173
3240
|
* @instance
|
|
@@ -3279,6 +3346,7 @@ Document.prototype.$__undoReset = function $__undoReset() {
|
|
|
3279
3346
|
/**
|
|
3280
3347
|
* Returns this documents dirty paths / vals.
|
|
3281
3348
|
*
|
|
3349
|
+
* @return {Array}
|
|
3282
3350
|
* @api private
|
|
3283
3351
|
* @method $__dirty
|
|
3284
3352
|
* @memberOf Document
|
|
@@ -3369,6 +3437,7 @@ Document.prototype.$__setSchema = function(schema) {
|
|
|
3369
3437
|
/**
|
|
3370
3438
|
* Get active path that were changed and are arrays
|
|
3371
3439
|
*
|
|
3440
|
+
* @return {Array}
|
|
3372
3441
|
* @api private
|
|
3373
3442
|
* @method $__getArrayPathsToValidate
|
|
3374
3443
|
* @memberOf Document
|
|
@@ -3397,6 +3466,7 @@ Document.prototype.$__getArrayPathsToValidate = function() {
|
|
|
3397
3466
|
/**
|
|
3398
3467
|
* Get all subdocs (by bfs)
|
|
3399
3468
|
*
|
|
3469
|
+
* @return {Array}
|
|
3400
3470
|
* @api public
|
|
3401
3471
|
* @method $getAllSubdocs
|
|
3402
3472
|
* @memberOf Document
|
|
@@ -3492,6 +3562,7 @@ Document.prototype.$__handleReject = function handleReject(err) {
|
|
|
3492
3562
|
/**
|
|
3493
3563
|
* Internal helper for toObject() and toJSON() that doesn't manipulate options
|
|
3494
3564
|
*
|
|
3565
|
+
* @return {Object}
|
|
3495
3566
|
* @api private
|
|
3496
3567
|
* @method $toObject
|
|
3497
3568
|
* @memberOf Document
|
|
@@ -3571,10 +3642,10 @@ Document.prototype.$toObject = function(options, json) {
|
|
|
3571
3642
|
options.minimize = _minimize;
|
|
3572
3643
|
|
|
3573
3644
|
cloneOptions._parentOptions = options;
|
|
3574
|
-
cloneOptions._skipSingleNestedGetters =
|
|
3645
|
+
cloneOptions._skipSingleNestedGetters = false;
|
|
3575
3646
|
|
|
3576
3647
|
const gettersOptions = Object.assign({}, cloneOptions);
|
|
3577
|
-
gettersOptions._skipSingleNestedGetters =
|
|
3648
|
+
gettersOptions._skipSingleNestedGetters = true;
|
|
3578
3649
|
|
|
3579
3650
|
// remember the root transform function
|
|
3580
3651
|
// to save it from being overwritten by sub-transform functions
|
|
@@ -3638,18 +3709,6 @@ Document.prototype.$toObject = function(options, json) {
|
|
|
3638
3709
|
*
|
|
3639
3710
|
* Buffers are converted to instances of [mongodb.Binary](https://mongodb.github.com/node-mongodb-native/api-bson-generated/binary.html) for proper storage.
|
|
3640
3711
|
*
|
|
3641
|
-
* #### Options:
|
|
3642
|
-
*
|
|
3643
|
-
* - `getters` apply all getters (path and virtual getters), defaults to false
|
|
3644
|
-
* - `aliases` apply all aliases if `virtuals=true`, defaults to true
|
|
3645
|
-
* - `virtuals` apply virtual getters (can override `getters` option), defaults to false
|
|
3646
|
-
* - `minimize` remove empty objects, defaults to true
|
|
3647
|
-
* - `transform` a transform function to apply to the resulting document before returning
|
|
3648
|
-
* - `depopulate` depopulate any populated paths, replacing them with their original refs, defaults to false
|
|
3649
|
-
* - `versionKey` whether to include the version key, defaults to true
|
|
3650
|
-
* - `flattenMaps` convert Maps to POJOs. Useful if you want to JSON.stringify() the result of toObject(), defaults to false
|
|
3651
|
-
* - `useProjection` set to `true` to omit fields that are excluded in this document's projection. Unless you specified a projection, this will omit any field that has `select: false` in the schema.
|
|
3652
|
-
*
|
|
3653
3712
|
* #### Getters/Virtuals
|
|
3654
3713
|
*
|
|
3655
3714
|
* Example of only applying path getters
|
|
@@ -3774,7 +3833,7 @@ Document.prototype.$toObject = function(options, json) {
|
|
|
3774
3833
|
* @param {Boolean} [options.versionKey=true] if false, exclude the version key (`__v` by default) from the output
|
|
3775
3834
|
* @param {Boolean} [options.flattenMaps=false] if true, convert Maps to POJOs. Useful if you want to `JSON.stringify()` the result of `toObject()`.
|
|
3776
3835
|
* @param {Boolean} [options.useProjection=false] - If true, omits fields that are excluded in this document's projection. Unless you specified a projection, this will omit any field that has `select: false` in the schema.
|
|
3777
|
-
* @return {Object} js object
|
|
3836
|
+
* @return {Object} js object (not a POJO)
|
|
3778
3837
|
* @see mongodb.Binary https://mongodb.github.com/node-mongodb-native/api-bson-generated/binary.html
|
|
3779
3838
|
* @api public
|
|
3780
3839
|
* @memberOf Document
|
|
@@ -4072,6 +4131,7 @@ Document.prototype.ownerDocument = function() {
|
|
|
4072
4131
|
* If this document is a subdocument or populated document, returns the document's
|
|
4073
4132
|
* parent. Returns the original document if there is no parent.
|
|
4074
4133
|
*
|
|
4134
|
+
* @return {Document}
|
|
4075
4135
|
* @api public
|
|
4076
4136
|
* @method parent
|
|
4077
4137
|
* @memberOf Document
|
|
@@ -4086,9 +4146,10 @@ Document.prototype.parent = function() {
|
|
|
4086
4146
|
};
|
|
4087
4147
|
|
|
4088
4148
|
/**
|
|
4089
|
-
* Alias for `parent()
|
|
4149
|
+
* Alias for [`parent()`](#document_Document-parent). If this document is a subdocument or populated
|
|
4090
4150
|
* document, returns the document's parent. Returns `undefined` otherwise.
|
|
4091
4151
|
*
|
|
4152
|
+
* @return {Document}
|
|
4092
4153
|
* @api public
|
|
4093
4154
|
* @method $parent
|
|
4094
4155
|
* @memberOf Document
|
|
@@ -4100,6 +4161,7 @@ Document.prototype.$parent = Document.prototype.parent;
|
|
|
4100
4161
|
/**
|
|
4101
4162
|
* Helper for console.log
|
|
4102
4163
|
*
|
|
4164
|
+
* @return {String}
|
|
4103
4165
|
* @api public
|
|
4104
4166
|
* @method inspect
|
|
4105
4167
|
* @memberOf Document
|
|
@@ -4135,6 +4197,7 @@ if (inspect.custom) {
|
|
|
4135
4197
|
/**
|
|
4136
4198
|
* Helper for console.log
|
|
4137
4199
|
*
|
|
4200
|
+
* @return {String}
|
|
4138
4201
|
* @api public
|
|
4139
4202
|
* @method toString
|
|
4140
4203
|
* @memberOf Document
|
|
@@ -4156,7 +4219,7 @@ Document.prototype.toString = function() {
|
|
|
4156
4219
|
* document has an `_id`, in which case this function falls back to using
|
|
4157
4220
|
* `deepEqual()`.
|
|
4158
4221
|
*
|
|
4159
|
-
* @param {Document} doc a document to compare
|
|
4222
|
+
* @param {Document} [doc] a document to compare. If falsy, will always return "false".
|
|
4160
4223
|
* @return {Boolean}
|
|
4161
4224
|
* @api public
|
|
4162
4225
|
* @memberOf Document
|
|
@@ -4214,10 +4277,10 @@ Document.prototype.equals = function(doc) {
|
|
|
4214
4277
|
* @param {Function} [callback] Callback
|
|
4215
4278
|
* @see population ./populate.html
|
|
4216
4279
|
* @see Query#select #query_Query-select
|
|
4217
|
-
* @see Model.populate #model_Model
|
|
4280
|
+
* @see Model.populate #model_Model-populate
|
|
4218
4281
|
* @memberOf Document
|
|
4219
4282
|
* @instance
|
|
4220
|
-
* @return {Promise|null}
|
|
4283
|
+
* @return {Promise|null} Returns a Promise if no `callback` is given.
|
|
4221
4284
|
* @api public
|
|
4222
4285
|
*/
|
|
4223
4286
|
|
|
@@ -4274,7 +4337,7 @@ Document.prototype.populate = function populate() {
|
|
|
4274
4337
|
* Gets all populated documents associated with this document.
|
|
4275
4338
|
*
|
|
4276
4339
|
* @api public
|
|
4277
|
-
* @return {
|
|
4340
|
+
* @return {Document[]} array of populated documents. Empty array if there are no populated documents associated with this document.
|
|
4278
4341
|
* @memberOf Document
|
|
4279
4342
|
* @method $getPopulatedDocs
|
|
4280
4343
|
* @instance
|
|
@@ -4310,6 +4373,8 @@ Document.prototype.$getPopulatedDocs = function $getPopulatedDocs() {
|
|
|
4310
4373
|
* If the path was not populated, returns `undefined`.
|
|
4311
4374
|
*
|
|
4312
4375
|
* @param {String} path
|
|
4376
|
+
* @param {Any} [val]
|
|
4377
|
+
* @param {Object} [options]
|
|
4313
4378
|
* @return {Array|ObjectId|Number|Buffer|String|undefined}
|
|
4314
4379
|
* @memberOf Document
|
|
4315
4380
|
* @instance
|
|
@@ -4357,6 +4422,14 @@ Document.prototype.populated = function(path, val, options) {
|
|
|
4357
4422
|
return val;
|
|
4358
4423
|
};
|
|
4359
4424
|
|
|
4425
|
+
/**
|
|
4426
|
+
* Alias of [`.populated`](#document_Document-populated).
|
|
4427
|
+
*
|
|
4428
|
+
* @method $populated
|
|
4429
|
+
* @memberOf Document
|
|
4430
|
+
* @api public
|
|
4431
|
+
*/
|
|
4432
|
+
|
|
4360
4433
|
Document.prototype.$populated = Document.prototype.populated;
|
|
4361
4434
|
|
|
4362
4435
|
/**
|
|
@@ -4370,7 +4443,7 @@ Document.prototype.$populated = Document.prototype.populated;
|
|
|
4370
4443
|
* doc.$assertPopulated('other path'); // throws an error
|
|
4371
4444
|
*
|
|
4372
4445
|
*
|
|
4373
|
-
* @param {String|String[]}
|
|
4446
|
+
* @param {String|String[]} paths
|
|
4374
4447
|
* @return {Document} this
|
|
4375
4448
|
* @memberOf Document
|
|
4376
4449
|
* @method $assertPopulated
|
|
@@ -4404,7 +4477,7 @@ Document.prototype.$assertPopulated = function $assertPopulated(paths) {
|
|
|
4404
4477
|
*
|
|
4405
4478
|
* If the path was not provided, then all populated fields are returned to their unpopulated state.
|
|
4406
4479
|
*
|
|
4407
|
-
* @param {String} path
|
|
4480
|
+
* @param {String} [path] Specific Path to depopulate. If unset, will depopulate all paths on the Document.
|
|
4408
4481
|
* @return {Document} this
|
|
4409
4482
|
* @see Document.populate #document_Document-populate
|
|
4410
4483
|
* @api public
|