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/model.js
CHANGED
|
@@ -96,7 +96,7 @@ const saveToObjectOptions = Object.assign({}, internalToObjectOptions, {
|
|
|
96
96
|
* const userFromDb = await UserModel.findOne({ name: 'Foo' });
|
|
97
97
|
*
|
|
98
98
|
* @param {Object} doc values for initial set
|
|
99
|
-
* @param [fields] optional object containing the fields that were selected in the query which returned this document. You do **not** need to set this parameter to ensure Mongoose handles your [query projection](./api.html#query_Query-select).
|
|
99
|
+
* @param {Object} [fields] optional object containing the fields that were selected in the query which returned this document. You do **not** need to set this parameter to ensure Mongoose handles your [query projection](./api.html#query_Query-select).
|
|
100
100
|
* @param {Boolean} [skipId=false] optional boolean. If true, mongoose doesn't add an `_id` field to the document.
|
|
101
101
|
* @inherits Document https://mongoosejs.com/docs/api/document.html
|
|
102
102
|
* @event `error`: If listening to this event, 'error' is emitted when a document was saved without passing a callback and an `error` occurred. If not listening, the event bubbles to the connection used to create this Model.
|
|
@@ -210,6 +210,7 @@ Model.prototype.baseModelName;
|
|
|
210
210
|
* await MyModel.findOne({ _id: 'Not a valid ObjectId' }).catch(noop);
|
|
211
211
|
*
|
|
212
212
|
* @api public
|
|
213
|
+
* @property events
|
|
213
214
|
* @fires error whenever any query or model function errors
|
|
214
215
|
* @memberOf Model
|
|
215
216
|
* @static
|
|
@@ -888,26 +889,30 @@ Model.prototype.$__version = function(where, delta) {
|
|
|
888
889
|
}
|
|
889
890
|
};
|
|
890
891
|
|
|
892
|
+
/*!
|
|
893
|
+
* ignore
|
|
894
|
+
*/
|
|
895
|
+
|
|
896
|
+
function increment() {
|
|
897
|
+
this.$__.version = VERSION_ALL;
|
|
898
|
+
return this;
|
|
899
|
+
}
|
|
900
|
+
|
|
891
901
|
/**
|
|
892
902
|
* Signal that we desire an increment of this documents version.
|
|
893
903
|
*
|
|
894
904
|
* #### Example:
|
|
895
905
|
*
|
|
896
|
-
* Model.findById(id
|
|
897
|
-
*
|
|
898
|
-
*
|
|
899
|
-
* })
|
|
906
|
+
* const doc = await Model.findById(id);
|
|
907
|
+
* doc.increment();
|
|
908
|
+
* await doc.save();
|
|
900
909
|
*
|
|
901
910
|
* @see versionKeys https://mongoosejs.com/docs/guide.html#versionKey
|
|
902
911
|
* @memberOf Model
|
|
912
|
+
* @method increment
|
|
903
913
|
* @api public
|
|
904
914
|
*/
|
|
905
915
|
|
|
906
|
-
function increment() {
|
|
907
|
-
this.$__.version = VERSION_ALL;
|
|
908
|
-
return this;
|
|
909
|
-
}
|
|
910
|
-
|
|
911
916
|
Model.prototype.increment = increment;
|
|
912
917
|
|
|
913
918
|
/**
|
|
@@ -937,22 +942,12 @@ Model.prototype.$__where = function _where(where) {
|
|
|
937
942
|
* Removes this document from the db.
|
|
938
943
|
*
|
|
939
944
|
* #### Example:
|
|
940
|
-
* product.remove(function (err, product) {
|
|
941
|
-
* if (err) return handleError(err);
|
|
942
|
-
* Product.findById(product._id, function (err, product) {
|
|
943
|
-
* console.log(product) // null
|
|
944
|
-
* })
|
|
945
|
-
* })
|
|
946
|
-
*
|
|
947
|
-
*
|
|
948
|
-
* As an extra measure of flow control, remove will return a Promise (bound to `fn` if passed) so it could be chained, or hooked to receive errors
|
|
949
945
|
*
|
|
950
|
-
*
|
|
951
|
-
*
|
|
952
|
-
*
|
|
953
|
-
*
|
|
954
|
-
*
|
|
955
|
-
* })
|
|
946
|
+
* const product = await product.remove().catch(function (err) {
|
|
947
|
+
* assert.ok(err);
|
|
948
|
+
* });
|
|
949
|
+
* const foundProduct = await Product.findById(product._id);
|
|
950
|
+
* console.log(foundProduct) // null
|
|
956
951
|
*
|
|
957
952
|
* @param {Object} [options]
|
|
958
953
|
* @param {Session} [options.session=null] the [session](https://docs.mongodb.com/manual/reference/server-sessions/) associated with this operation. If not specified, defaults to the [document's associated session](api.html#document_Document-$session).
|
|
@@ -995,6 +990,7 @@ Model.prototype.delete = Model.prototype.remove;
|
|
|
995
990
|
* Removes this document from the db. Equivalent to `.remove()`.
|
|
996
991
|
*
|
|
997
992
|
* #### Example:
|
|
993
|
+
*
|
|
998
994
|
* product = await product.deleteOne();
|
|
999
995
|
* await Product.findById(product._id); // null
|
|
1000
996
|
*
|
|
@@ -1104,6 +1100,7 @@ Model.prototype.$model = function $model(name) {
|
|
|
1104
1100
|
* `MyModel.findOne({ answer: 42 }).select({ _id: 1 }).lean()`
|
|
1105
1101
|
*
|
|
1106
1102
|
* #### Example:
|
|
1103
|
+
*
|
|
1107
1104
|
* await Character.deleteMany({});
|
|
1108
1105
|
* await Character.create({ name: 'Jean-Luc Picard' });
|
|
1109
1106
|
*
|
|
@@ -1273,7 +1270,7 @@ for (const i in EventEmitter.prototype) {
|
|
|
1273
1270
|
*
|
|
1274
1271
|
* #### Example:
|
|
1275
1272
|
*
|
|
1276
|
-
* const eventSchema = new Schema({ thing: { type: 'string', unique: true }})
|
|
1273
|
+
* const eventSchema = new Schema({ thing: { type: 'string', unique: true } })
|
|
1277
1274
|
* // This calls `Event.init()` implicitly, so you don't need to call
|
|
1278
1275
|
* // `Event.init()` on your own.
|
|
1279
1276
|
* const Event = mongoose.model('Event', eventSchema);
|
|
@@ -1491,7 +1488,7 @@ Model.syncIndexes = function syncIndexes(options, callback) {
|
|
|
1491
1488
|
* Model.syncIndexes().
|
|
1492
1489
|
*
|
|
1493
1490
|
* @param {Object} [options]
|
|
1494
|
-
* @param {Function} callback optional callback
|
|
1491
|
+
* @param {Function} [callback] optional callback
|
|
1495
1492
|
* @returns {Promise} which contains an object, {toDrop, toCreate}, which
|
|
1496
1493
|
* are indexes that would be dropped in MongoDB and indexes that would be created in MongoDB.
|
|
1497
1494
|
*/
|
|
@@ -1655,7 +1652,7 @@ Model.listIndexes = function init(callback) {
|
|
|
1655
1652
|
*
|
|
1656
1653
|
* #### Example:
|
|
1657
1654
|
*
|
|
1658
|
-
* const eventSchema = new Schema({ thing: { type: 'string', unique: true }})
|
|
1655
|
+
* const eventSchema = new Schema({ thing: { type: 'string', unique: true } })
|
|
1659
1656
|
* const Event = mongoose.model('Event', eventSchema);
|
|
1660
1657
|
*
|
|
1661
1658
|
* Event.on('index', function (err) {
|
|
@@ -1890,6 +1887,7 @@ Model.discriminators;
|
|
|
1890
1887
|
* .exec(function(err, characters) {})
|
|
1891
1888
|
*
|
|
1892
1889
|
* #### Note:
|
|
1890
|
+
*
|
|
1893
1891
|
* Only translate arguments of object type anything else is returned raw
|
|
1894
1892
|
*
|
|
1895
1893
|
* @param {Object} fields fields/conditions that may contain aliased keys
|
|
@@ -2304,7 +2302,7 @@ Model.estimatedDocumentCount = function estimatedDocumentCount(options, callback
|
|
|
2304
2302
|
* });
|
|
2305
2303
|
*
|
|
2306
2304
|
* If you want to count all documents in a large collection,
|
|
2307
|
-
* use the [`estimatedDocumentCount()` function](/docs/api.html#model_Model
|
|
2305
|
+
* use the [`estimatedDocumentCount()` function](/docs/api.html#model_Model-estimatedDocumentCount)
|
|
2308
2306
|
* instead. If you call `countDocuments({})`, MongoDB will always execute
|
|
2309
2307
|
* a full collection scan and **not** use any indexes.
|
|
2310
2308
|
*
|
|
@@ -2349,8 +2347,8 @@ Model.countDocuments = function countDocuments(conditions, options, callback) {
|
|
|
2349
2347
|
* Counts number of documents that match `filter` in a database collection.
|
|
2350
2348
|
*
|
|
2351
2349
|
* This method is deprecated. If you want to count the number of documents in
|
|
2352
|
-
* a collection, e.g. `count({})`, use the [`estimatedDocumentCount()` function](/docs/api.html#model_Model
|
|
2353
|
-
* instead. Otherwise, use the [`countDocuments()`](/docs/api.html#model_Model
|
|
2350
|
+
* a collection, e.g. `count({})`, use the [`estimatedDocumentCount()` function](/docs/api.html#model_Model-estimatedDocumentCount)
|
|
2351
|
+
* instead. Otherwise, use the [`countDocuments()`](/docs/api.html#model_Model-countDocuments) function instead.
|
|
2354
2352
|
*
|
|
2355
2353
|
* #### Example:
|
|
2356
2354
|
*
|
|
@@ -2386,7 +2384,7 @@ Model.count = function count(conditions, callback) {
|
|
|
2386
2384
|
*
|
|
2387
2385
|
* #### Example:
|
|
2388
2386
|
*
|
|
2389
|
-
* Link.distinct('url', { clicks: {$gt: 100}}, function (err, result) {
|
|
2387
|
+
* Link.distinct('url', { clicks: { $gt: 100 } }, function (err, result) {
|
|
2390
2388
|
* if (err) return handleError(err);
|
|
2391
2389
|
*
|
|
2392
2390
|
* assert(Array.isArray(result));
|
|
@@ -2422,7 +2420,7 @@ Model.distinct = function distinct(field, conditions, callback) {
|
|
|
2422
2420
|
*
|
|
2423
2421
|
* For example, instead of writing:
|
|
2424
2422
|
*
|
|
2425
|
-
* User.find({age: {$gte: 21, $lte: 65}}, callback);
|
|
2423
|
+
* User.find({ age: { $gte: 21, $lte: 65 } }, callback);
|
|
2426
2424
|
*
|
|
2427
2425
|
* we can instead write:
|
|
2428
2426
|
*
|
|
@@ -2476,19 +2474,6 @@ Model.$where = function $where() {
|
|
|
2476
2474
|
*
|
|
2477
2475
|
* Finds a matching document, updates it according to the `update` arg, passing any `options`, and returns the found document (if any) to the callback. The query executes if `callback` is passed else a Query object is returned.
|
|
2478
2476
|
*
|
|
2479
|
-
* #### Options:
|
|
2480
|
-
*
|
|
2481
|
-
* - `new`: bool - if true, return the modified document rather than the original. defaults to false (changed in 4.0)
|
|
2482
|
-
* - `upsert`: bool - creates the object if it doesn't exist. defaults to false.
|
|
2483
|
-
* - `overwrite`: bool - if true, replace the entire document.
|
|
2484
|
-
* - `fields`: {Object|String} - Field selection. Equivalent to `.select(fields).findOneAndUpdate()`
|
|
2485
|
-
* - `maxTimeMS`: puts a time limit on the query - requires mongodb >= 2.6.0
|
|
2486
|
-
* - `sort`: if multiple docs are found by the conditions, sets the sort order to choose which doc to update
|
|
2487
|
-
* - `runValidators`: if true, runs [update validators](/docs/validation.html#update-validators) on this command. Update validators validate the update operation against the model's schema.
|
|
2488
|
-
* - `setDefaultsOnInsert`: `true` by default. If `setDefaultsOnInsert` and `upsert` are true, mongoose will apply the [defaults](https://mongoosejs.com/docs/defaults.html) specified in the model's schema if a new document is created.
|
|
2489
|
-
* - `rawResult`: if true, returns the [raw result from the MongoDB driver](https://mongodb.github.io/node-mongodb-native/4.3/interfaces/ModifyResult.html)
|
|
2490
|
-
* - `strict`: overwrites the schema's [strict mode option](https://mongoosejs.com/docs/guide.html#strict) for this update
|
|
2491
|
-
*
|
|
2492
2477
|
* #### Examples:
|
|
2493
2478
|
*
|
|
2494
2479
|
* A.findOneAndUpdate(conditions, update, options, callback) // executes
|
|
@@ -2531,9 +2516,16 @@ Model.$where = function $where() {
|
|
|
2531
2516
|
* @param {ClientSession} [options.session=null] The session associated with this query. See [transactions docs](/docs/transactions.html).
|
|
2532
2517
|
* @param {Boolean|String} [options.strict] overwrites the schema's [strict mode option](https://mongoosejs.com/docs/guide.html#strict)
|
|
2533
2518
|
* @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.
|
|
2534
|
-
* @param {Boolean} [options.overwrite=false] By default, if you don't include any [update operators](https://docs.mongodb.com/manual/reference/operator/update/) in `update`, Mongoose will wrap `update` in `$set` for you. This prevents you from accidentally overwriting the document. This option tells Mongoose to skip adding `$set`. An alternative to this would be using [Model.findOneAndReplace(conditions, update, options, callback)](https://mongoosejs.com/docs/api/model.html#model_Model
|
|
2519
|
+
* @param {Boolean} [options.overwrite=false] By default, if you don't include any [update operators](https://docs.mongodb.com/manual/reference/operator/update/) in `update`, Mongoose will wrap `update` in `$set` for you. This prevents you from accidentally overwriting the document. This option tells Mongoose to skip adding `$set`. An alternative to this would be using [Model.findOneAndReplace(conditions, update, options, callback)](https://mongoosejs.com/docs/api/model.html#model_Model-findOneAndReplace).
|
|
2535
2520
|
* @param {Boolean} [options.upsert=false] if true, and no documents found, insert a new document
|
|
2536
2521
|
* @param {Object|String|String[]} [options.projection=null] optional fields to return, see [`Query.prototype.select()`](#query_Query-select)
|
|
2522
|
+
* @param {Boolean} [options.new=false] if true, return the modified document rather than the original
|
|
2523
|
+
* @param {Object|String} [options.fields] Field selection. Equivalent to `.select(fields).findOneAndUpdate()`
|
|
2524
|
+
* @param {Number} [options.maxTimeMS] puts a time limit on the query - requires mongodb >= 2.6.0
|
|
2525
|
+
* @param {Object|String} [options.sort] if multiple docs are found by the conditions, sets the sort order to choose which doc to update.
|
|
2526
|
+
* @param {Boolean} [options.runValidators] if true, runs [update validators](/docs/validation.html#update-validators) on this command. Update validators validate the update operation against the model's schema
|
|
2527
|
+
* @param {Boolean} [options.setDefaultsOnInsert=true] If `setDefaultsOnInsert` and `upsert` are true, mongoose will apply the [defaults](https://mongoosejs.com/docs/defaults.html) specified in the model's schema if a new document is created
|
|
2528
|
+
* @param {Boolean} [options.rawResult] if true, returns the [raw result from the MongoDB driver](https://mongodb.github.io/node-mongodb-native/4.3/interfaces/ModifyResult.html)
|
|
2537
2529
|
* @param {Function} [callback]
|
|
2538
2530
|
* @return {Query}
|
|
2539
2531
|
* @see Tutorial /docs/tutorials/findoneandupdate.html
|
|
@@ -2614,17 +2606,6 @@ function _decorateUpdateWithVersionKey(update, options, versionKey) {
|
|
|
2614
2606
|
*
|
|
2615
2607
|
* - `findOneAndUpdate()`
|
|
2616
2608
|
*
|
|
2617
|
-
* #### Options:
|
|
2618
|
-
*
|
|
2619
|
-
* - `new`: bool - true to return the modified document rather than the original. defaults to false
|
|
2620
|
-
* - `upsert`: bool - creates the object if it doesn't exist. defaults to false.
|
|
2621
|
-
* - `runValidators`: if true, runs [update validators](/docs/validation.html#update-validators) on this command. Update validators validate the update operation against the model's schema.
|
|
2622
|
-
* - `setDefaultsOnInsert`: `true` by default. If `setDefaultsOnInsert` and `upsert` are true, mongoose will apply the [defaults](https://mongoosejs.com/docs/defaults.html) specified in the model's schema if a new document is created.
|
|
2623
|
-
* - `sort`: if multiple docs are found by the conditions, sets the sort order to choose which doc to update
|
|
2624
|
-
* - `select`: sets the document fields to return
|
|
2625
|
-
* - `rawResult`: if true, returns the [raw result from the MongoDB driver](https://mongodb.github.io/node-mongodb-native/4.3/interfaces/ModifyResult.html)
|
|
2626
|
-
* - `strict`: overwrites the schema's [strict mode option](https://mongoosejs.com/docs/guide.html#strict) for this update
|
|
2627
|
-
*
|
|
2628
2609
|
* #### Examples:
|
|
2629
2610
|
*
|
|
2630
2611
|
* A.findByIdAndUpdate(id, update, options, callback) // executes
|
|
@@ -2654,11 +2635,9 @@ function _decorateUpdateWithVersionKey(update, options, versionKey) {
|
|
|
2654
2635
|
* If you need full-fledged validation, use the traditional approach of first
|
|
2655
2636
|
* retrieving the document.
|
|
2656
2637
|
*
|
|
2657
|
-
* Model.findById(id
|
|
2658
|
-
*
|
|
2659
|
-
*
|
|
2660
|
-
* doc.save(callback);
|
|
2661
|
-
* });
|
|
2638
|
+
* const doc = await Model.findById(id)
|
|
2639
|
+
* doc.name = 'jason bourne';
|
|
2640
|
+
* await doc.save();
|
|
2662
2641
|
*
|
|
2663
2642
|
* @param {Object|Number|String} id value of `_id` to query by
|
|
2664
2643
|
* @param {Object} [update]
|
|
@@ -2668,10 +2647,17 @@ function _decorateUpdateWithVersionKey(update, options, versionKey) {
|
|
|
2668
2647
|
* @param {ClientSession} [options.session=null] The session associated with this query. See [transactions docs](/docs/transactions.html).
|
|
2669
2648
|
* @param {Boolean|String} [options.strict] overwrites the schema's [strict mode option](https://mongoosejs.com/docs/guide.html#strict)
|
|
2670
2649
|
* @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.
|
|
2671
|
-
* @param {Boolean} [options.overwrite=false] By default, if you don't include any [update operators](https://docs.mongodb.com/manual/reference/operator/update/) in `update`, Mongoose will wrap `update` in `$set` for you. This prevents you from accidentally overwriting the document. This option tells Mongoose to skip adding `$set`. An alternative to this would be using [Model.findOneAndReplace({ _id: id }, update, options, callback)](https://mongoosejs.com/docs/api/model.html#model_Model
|
|
2650
|
+
* @param {Boolean} [options.overwrite=false] By default, if you don't include any [update operators](https://docs.mongodb.com/manual/reference/operator/update/) in `update`, Mongoose will wrap `update` in `$set` for you. This prevents you from accidentally overwriting the document. This option tells Mongoose to skip adding `$set`. An alternative to this would be using [Model.findOneAndReplace({ _id: id }, update, options, callback)](https://mongoosejs.com/docs/api/model.html#model_Model-findOneAndReplace).
|
|
2651
|
+
* @param {Object|String} [options.sort] if multiple docs are found by the conditions, sets the sort order to choose which doc to update.
|
|
2652
|
+
* @param {Boolean} [options.runValidators] if true, runs [update validators](/docs/validation.html#update-validators) on this command. Update validators validate the update operation against the model's schema
|
|
2653
|
+
* @param {Boolean} [options.setDefaultsOnInsert=true] If `setDefaultsOnInsert` and `upsert` are true, mongoose will apply the [defaults](https://mongoosejs.com/docs/defaults.html) specified in the model's schema if a new document is created
|
|
2654
|
+
* @param {Boolean} [options.rawResult] if true, returns the [raw result from the MongoDB driver](https://mongodb.github.io/node-mongodb-native/4.3/interfaces/ModifyResult.html)
|
|
2655
|
+
* @param {Boolean} [options.upsert=false] if true, and no documents found, insert a new document
|
|
2656
|
+
* @param {Boolean} [options.new=false] if true, return the modified document rather than the original
|
|
2657
|
+
* @param {Object|String} [options.select] sets the document fields to return.
|
|
2672
2658
|
* @param {Function} [callback]
|
|
2673
2659
|
* @return {Query}
|
|
2674
|
-
* @see Model.findOneAndUpdate #model_Model
|
|
2660
|
+
* @see Model.findOneAndUpdate #model_Model-findOneAndUpdate
|
|
2675
2661
|
* @see mongodb https://www.mongodb.org/display/DOCS/findAndModify+Command
|
|
2676
2662
|
* @api public
|
|
2677
2663
|
*/
|
|
@@ -2717,15 +2703,6 @@ Model.findByIdAndUpdate = function(id, update, options, callback) {
|
|
|
2717
2703
|
* this distinction is purely pedantic. You should use `findOneAndDelete()`
|
|
2718
2704
|
* unless you have a good reason not to.
|
|
2719
2705
|
*
|
|
2720
|
-
* #### Options:
|
|
2721
|
-
*
|
|
2722
|
-
* - `sort`: if multiple docs are found by the conditions, sets the sort order to choose which doc to update
|
|
2723
|
-
* - `maxTimeMS`: puts a time limit on the query - requires mongodb >= 2.6.0
|
|
2724
|
-
* - `select`: sets the document fields to return, ex. `{ projection: { _id: 0 } }`
|
|
2725
|
-
* - `projection`: equivalent to `select`
|
|
2726
|
-
* - `rawResult`: if true, returns the [raw result from the MongoDB driver](https://mongodb.github.io/node-mongodb-native/4.3/interfaces/ModifyResult.html)
|
|
2727
|
-
* - `strict`: overwrites the schema's [strict mode option](https://mongoosejs.com/docs/guide.html#strict) for this update
|
|
2728
|
-
*
|
|
2729
2706
|
* #### Examples:
|
|
2730
2707
|
*
|
|
2731
2708
|
* A.findOneAndDelete(conditions, options, callback) // executes
|
|
@@ -2740,17 +2717,19 @@ Model.findByIdAndUpdate = function(id, update, options, callback) {
|
|
|
2740
2717
|
* If you need full-fledged validation, use the traditional approach of first
|
|
2741
2718
|
* retrieving the document.
|
|
2742
2719
|
*
|
|
2743
|
-
* Model.findById(id
|
|
2744
|
-
*
|
|
2745
|
-
*
|
|
2746
|
-
* doc.save(callback);
|
|
2747
|
-
* });
|
|
2720
|
+
* const doc = await Model.findById(id)
|
|
2721
|
+
* doc.name = 'jason bourne';
|
|
2722
|
+
* await doc.save();
|
|
2748
2723
|
*
|
|
2749
2724
|
* @param {Object} conditions
|
|
2750
2725
|
* @param {Object} [options] optional see [`Query.prototype.setOptions()`](https://mongoosejs.com/docs/api.html#query_Query-setOptions)
|
|
2751
2726
|
* @param {Boolean|String} [options.strict] overwrites the schema's [strict mode option](https://mongoosejs.com/docs/guide.html#strict)
|
|
2752
2727
|
* @param {Object|String|String[]} [options.projection=null] optional fields to return, see [`Query.prototype.select()`](#query_Query-select)
|
|
2753
2728
|
* @param {ClientSession} [options.session=null] The session associated with this query. See [transactions docs](/docs/transactions.html).
|
|
2729
|
+
* @param {Boolean} [options.rawResult] if true, returns the [raw result from the MongoDB driver](https://mongodb.github.io/node-mongodb-native/4.3/interfaces/ModifyResult.html)
|
|
2730
|
+
* @param {Object|String} [options.sort] if multiple docs are found by the conditions, sets the sort order to choose which doc to update.
|
|
2731
|
+
* @param {Object|String} [options.select] sets the document fields to return.
|
|
2732
|
+
* @param {Number} [options.maxTimeMS] puts a time limit on the query - requires mongodb >= 2.6.0
|
|
2754
2733
|
* @param {Function} [callback]
|
|
2755
2734
|
* @return {Query}
|
|
2756
2735
|
* @api public
|
|
@@ -2799,7 +2778,7 @@ Model.findOneAndDelete = function(conditions, options, callback) {
|
|
|
2799
2778
|
* @param {Boolean|String} [options.strict] overwrites the schema's [strict mode option](https://mongoosejs.com/docs/guide.html#strict)
|
|
2800
2779
|
* @param {Function} [callback]
|
|
2801
2780
|
* @return {Query}
|
|
2802
|
-
* @see Model.findOneAndRemove #model_Model
|
|
2781
|
+
* @see Model.findOneAndRemove #model_Model-findOneAndRemove
|
|
2803
2782
|
* @see mongodb https://www.mongodb.org/display/DOCS/findAndModify+Command
|
|
2804
2783
|
*/
|
|
2805
2784
|
|
|
@@ -2830,15 +2809,6 @@ Model.findByIdAndDelete = function(id, options, callback) {
|
|
|
2830
2809
|
*
|
|
2831
2810
|
* - `findOneAndReplace()`
|
|
2832
2811
|
*
|
|
2833
|
-
* #### Options:
|
|
2834
|
-
*
|
|
2835
|
-
* - `sort`: if multiple docs are found by the conditions, sets the sort order to choose which doc to update
|
|
2836
|
-
* - `maxTimeMS`: puts a time limit on the query - requires mongodb >= 2.6.0
|
|
2837
|
-
* - `select`: sets the document fields to return
|
|
2838
|
-
* - `projection`: like select, it determines which fields to return, ex. `{ projection: { _id: 0 } }`
|
|
2839
|
-
* - `rawResult`: if true, returns the [raw result from the MongoDB driver](https://mongodb.github.io/node-mongodb-native/4.3/interfaces/ModifyResult.html)
|
|
2840
|
-
* - `strict`: overwrites the schema's [strict mode option](https://mongoosejs.com/docs/guide.html#strict) for this update
|
|
2841
|
-
*
|
|
2842
2812
|
* #### Examples:
|
|
2843
2813
|
*
|
|
2844
2814
|
* A.findOneAndReplace(filter, replacement, options, callback) // executes
|
|
@@ -2856,6 +2826,10 @@ Model.findByIdAndDelete = function(id, options, callback) {
|
|
|
2856
2826
|
* @param {Boolean|String} [options.strict] overwrites the schema's [strict mode option](https://mongoosejs.com/docs/guide.html#strict)
|
|
2857
2827
|
* @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.
|
|
2858
2828
|
* @param {Object|String|String[]} [options.projection=null] optional fields to return, see [`Query.prototype.select()`](#query_Query-select)
|
|
2829
|
+
* @param {Object|String} [options.sort] if multiple docs are found by the conditions, sets the sort order to choose which doc to update.
|
|
2830
|
+
* @param {Boolean} [options.rawResult] if true, returns the [raw result from the MongoDB driver](https://mongodb.github.io/node-mongodb-native/4.3/interfaces/ModifyResult.html)
|
|
2831
|
+
* @param {Object|String} [options.select] sets the document fields to return.
|
|
2832
|
+
* @param {Number} [options.maxTimeMS] puts a time limit on the query - requires mongodb >= 2.6.0
|
|
2859
2833
|
* @param {Function} [callback]
|
|
2860
2834
|
* @return {Query}
|
|
2861
2835
|
* @api public
|
|
@@ -2909,15 +2883,6 @@ Model.findOneAndReplace = function(filter, replacement, options, callback) {
|
|
|
2909
2883
|
*
|
|
2910
2884
|
* - `findOneAndRemove()`
|
|
2911
2885
|
*
|
|
2912
|
-
* #### Options:
|
|
2913
|
-
*
|
|
2914
|
-
* - `sort`: if multiple docs are found by the conditions, sets the sort order to choose which doc to update
|
|
2915
|
-
* - `maxTimeMS`: puts a time limit on the query - requires mongodb >= 2.6.0
|
|
2916
|
-
* - `select`: sets the document fields to return
|
|
2917
|
-
* - `projection`: like select, it determines which fields to return, ex. `{ projection: { _id: 0 } }`
|
|
2918
|
-
* - `rawResult`: if true, returns the [raw result from the MongoDB driver](https://mongodb.github.io/node-mongodb-native/4.3/interfaces/ModifyResult.html)
|
|
2919
|
-
* - `strict`: overwrites the schema's [strict mode option](https://mongoosejs.com/docs/guide.html#strict) for this update
|
|
2920
|
-
*
|
|
2921
2886
|
* #### Examples:
|
|
2922
2887
|
*
|
|
2923
2888
|
* A.findOneAndRemove(conditions, options, callback) // executes
|
|
@@ -2932,17 +2897,19 @@ Model.findOneAndReplace = function(filter, replacement, options, callback) {
|
|
|
2932
2897
|
* If you need full-fledged validation, use the traditional approach of first
|
|
2933
2898
|
* retrieving the document.
|
|
2934
2899
|
*
|
|
2935
|
-
* Model.findById(id
|
|
2936
|
-
*
|
|
2937
|
-
*
|
|
2938
|
-
* doc.save(callback);
|
|
2939
|
-
* });
|
|
2900
|
+
* const doc = await Model.findById(id);
|
|
2901
|
+
* doc.name = 'jason bourne';
|
|
2902
|
+
* await doc.save();
|
|
2940
2903
|
*
|
|
2941
2904
|
* @param {Object} conditions
|
|
2942
2905
|
* @param {Object} [options] optional see [`Query.prototype.setOptions()`](https://mongoosejs.com/docs/api.html#query_Query-setOptions)
|
|
2943
2906
|
* @param {ClientSession} [options.session=null] The session associated with this query. See [transactions docs](/docs/transactions.html).
|
|
2944
2907
|
* @param {Boolean|String} [options.strict] overwrites the schema's [strict mode option](https://mongoosejs.com/docs/guide.html#strict)
|
|
2945
2908
|
* @param {Object|String|String[]} [options.projection=null] optional fields to return, see [`Query.prototype.select()`](#query_Query-select)
|
|
2909
|
+
* @param {Object|String} [options.sort] if multiple docs are found by the conditions, sets the sort order to choose which doc to update.
|
|
2910
|
+
* @param {Boolean} [options.rawResult] if true, returns the [raw result from the MongoDB driver](https://mongodb.github.io/node-mongodb-native/4.3/interfaces/ModifyResult.html)
|
|
2911
|
+
* @param {Object|String} [options.select] sets the document fields to return.
|
|
2912
|
+
* @param {Number} [options.maxTimeMS] puts a time limit on the query - requires mongodb >= 2.6.0
|
|
2946
2913
|
* @param {Function} [callback]
|
|
2947
2914
|
* @return {Query}
|
|
2948
2915
|
* @see mongodb https://www.mongodb.org/display/DOCS/findAndModify+Command
|
|
@@ -2989,13 +2956,6 @@ Model.findOneAndRemove = function(conditions, options, callback) {
|
|
|
2989
2956
|
*
|
|
2990
2957
|
* - `findOneAndRemove()`
|
|
2991
2958
|
*
|
|
2992
|
-
* #### Options:
|
|
2993
|
-
*
|
|
2994
|
-
* - `sort`: if multiple docs are found by the conditions, sets the sort order to choose which doc to update
|
|
2995
|
-
* - `select`: sets the document fields to return
|
|
2996
|
-
* - `rawResult`: if true, returns the [raw result from the MongoDB driver](https://mongodb.github.io/node-mongodb-native/4.3/interfaces/ModifyResult.html)
|
|
2997
|
-
* - `strict`: overwrites the schema's [strict mode option](https://mongoosejs.com/docs/guide.html#strict) for this update
|
|
2998
|
-
*
|
|
2999
2959
|
* #### Examples:
|
|
3000
2960
|
*
|
|
3001
2961
|
* A.findByIdAndRemove(id, options, callback) // executes
|
|
@@ -3009,9 +2969,12 @@ Model.findOneAndRemove = function(conditions, options, callback) {
|
|
|
3009
2969
|
* @param {Boolean|String} [options.strict] overwrites the schema's [strict mode option](https://mongoosejs.com/docs/guide.html#strict)
|
|
3010
2970
|
* @param {ClientSession} [options.session=null] The session associated with this query. See [transactions docs](/docs/transactions.html).
|
|
3011
2971
|
* @param {Object|String|String[]} [options.projection=null] optional fields to return, see [`Query.prototype.select()`](#query_Query-select)
|
|
2972
|
+
* @param {Object|String} [options.sort] if multiple docs are found by the conditions, sets the sort order to choose which doc to update.
|
|
2973
|
+
* @param {Boolean} [options.rawResult] if true, returns the [raw result from the MongoDB driver](https://mongodb.github.io/node-mongodb-native/4.3/interfaces/ModifyResult.html)
|
|
2974
|
+
* @param {Object|String} [options.select] sets the document fields to return.
|
|
3012
2975
|
* @param {Function} [callback]
|
|
3013
2976
|
* @return {Query}
|
|
3014
|
-
* @see Model.findOneAndRemove #model_Model
|
|
2977
|
+
* @see Model.findOneAndRemove #model_Model-findOneAndRemove
|
|
3015
2978
|
* @see mongodb https://www.mongodb.org/display/DOCS/findAndModify+Command
|
|
3016
2979
|
*/
|
|
3017
2980
|
|
|
@@ -3091,7 +3054,7 @@ Model.create = function create(doc, options, callback) {
|
|
|
3091
3054
|
// to use a spread to specify options, see gh-7535
|
|
3092
3055
|
utils.warn('WARNING: to pass a `session` to `Model.create()` in ' +
|
|
3093
3056
|
'Mongoose, you **must** pass an array as the first argument. See: ' +
|
|
3094
|
-
'https://mongoosejs.com/docs/api.html#model_Model
|
|
3057
|
+
'https://mongoosejs.com/docs/api.html#model_Model-create');
|
|
3095
3058
|
}
|
|
3096
3059
|
}
|
|
3097
3060
|
|
|
@@ -3396,7 +3359,7 @@ Model.$__insertMany = function(arr, options, callback) {
|
|
|
3396
3359
|
if (doc.$__schema.options.versionKey) {
|
|
3397
3360
|
doc[doc.$__schema.options.versionKey] = 0;
|
|
3398
3361
|
}
|
|
3399
|
-
if (doc.initializeTimestamps) {
|
|
3362
|
+
if ((!options || options.timestamps !== false) && doc.initializeTimestamps) {
|
|
3400
3363
|
return doc.initializeTimestamps().toObject(internalToObjectOptions);
|
|
3401
3364
|
}
|
|
3402
3365
|
return doc.toObject(internalToObjectOptions);
|
|
@@ -3500,7 +3463,7 @@ function _setIsNew(doc, val) {
|
|
|
3500
3463
|
*
|
|
3501
3464
|
* This function does **not** trigger any middleware, neither `save()`, nor `update()`.
|
|
3502
3465
|
* If you need to trigger
|
|
3503
|
-
* `save()` middleware for every document use [`create()`](https://mongoosejs.com/docs/api.html#model_Model
|
|
3466
|
+
* `save()` middleware for every document use [`create()`](https://mongoosejs.com/docs/api.html#model_Model-create) instead.
|
|
3504
3467
|
*
|
|
3505
3468
|
* #### Example:
|
|
3506
3469
|
*
|
|
@@ -3687,11 +3650,13 @@ function handleSuccessfulWrite(document, resolve, reject) {
|
|
|
3687
3650
|
}
|
|
3688
3651
|
|
|
3689
3652
|
/**
|
|
3653
|
+
* Build bulk write operations for `bulkSave()`.
|
|
3690
3654
|
*
|
|
3691
3655
|
* @param {Array<Document>} documents The array of documents to build write operations of
|
|
3692
3656
|
* @param {Object} options
|
|
3693
3657
|
* @param {Boolean} options.skipValidation defaults to `false`, when set to true, building the write operations will bypass validating the documents.
|
|
3694
3658
|
* @return {Array<Promise>} Returns a array of all Promises the function executes to be awaited.
|
|
3659
|
+
* @api private
|
|
3695
3660
|
*/
|
|
3696
3661
|
Model.buildBulkWriteOperations = function buildBulkWriteOperations(documents, options) {
|
|
3697
3662
|
if (!Array.isArray(documents)) {
|
|
@@ -3856,7 +3821,6 @@ Model.hydrate = function(obj, projection) {
|
|
|
3856
3821
|
* @param {Boolean} [options.timestamps=null] If set to `false` and [schema-level timestamps](/docs/guide.html#timestamps) are enabled, skip timestamps for this update. Does nothing if schema-level timestamps are not set.
|
|
3857
3822
|
* @param {Boolean} [options.overwrite=false] By default, if you don't include any [update operators](https://docs.mongodb.com/manual/reference/operator/update/) in `doc`, Mongoose will wrap `doc` in `$set` for you. This prevents you from accidentally overwriting the document. This option tells Mongoose to skip adding `$set`.
|
|
3858
3823
|
* @param {Function} [callback] params are (error, [updateWriteOpResult](https://mongodb.github.io/node-mongodb-native/3.6/api/Collection.html#~updateWriteOpResult))
|
|
3859
|
-
* @param {Function} [callback]
|
|
3860
3824
|
* @return {Query}
|
|
3861
3825
|
* @see MongoDB docs https://docs.mongodb.com/manual/reference/command/update/#update-command-output
|
|
3862
3826
|
* @see writeOpResult https://mongodb.github.io/node-mongodb-native/3.6/api/Collection.html#~updateWriteOpResult
|
|
@@ -4022,54 +3986,34 @@ function _update(model, op, conditions, doc, options, callback) {
|
|
|
4022
3986
|
/**
|
|
4023
3987
|
* Executes a mapReduce command.
|
|
4024
3988
|
*
|
|
4025
|
-
* `
|
|
3989
|
+
* `opts` is an object specifying all mapReduce options as well as the map and reduce functions. All options are delegated to the driver implementation. See [node-mongodb-native mapReduce() documentation](https://mongodb.github.io/node-mongodb-native/api-generated/collection.html#mapreduce) for more detail about options.
|
|
4026
3990
|
*
|
|
4027
3991
|
* This function does not trigger any middleware.
|
|
4028
3992
|
*
|
|
4029
3993
|
* #### Example:
|
|
4030
3994
|
*
|
|
4031
|
-
* const
|
|
3995
|
+
* const opts = {};
|
|
4032
3996
|
* // `map()` and `reduce()` are run on the MongoDB server, not Node.js,
|
|
4033
3997
|
* // these functions are converted to strings
|
|
4034
|
-
*
|
|
4035
|
-
*
|
|
4036
|
-
* User.mapReduce(
|
|
3998
|
+
* opts.map = function () { emit(this.name, 1) };
|
|
3999
|
+
* opts.reduce = function (k, vals) { return vals.length };
|
|
4000
|
+
* User.mapReduce(opts, function (err, results) {
|
|
4037
4001
|
* console.log(results)
|
|
4038
4002
|
* })
|
|
4039
4003
|
*
|
|
4040
|
-
*
|
|
4041
|
-
*
|
|
4042
|
-
* - `query` {Object} query filter object.
|
|
4043
|
-
* - `sort` {Object} sort input objects using this key
|
|
4044
|
-
* - `limit` {Number} max number of documents
|
|
4045
|
-
* - `keeptemp` {Boolean, default:false} keep temporary data
|
|
4046
|
-
* - `finalize` {Function} finalize function
|
|
4047
|
-
* - `scope` {Object} scope variables exposed to map/reduce/finalize during execution
|
|
4048
|
-
* - `jsMode` {Boolean, default:false} it is possible to make the execution stay in JS. Provided in MongoDB > 2.0.X
|
|
4049
|
-
* - `verbose` {Boolean, default:false} provide statistics on job execution time.
|
|
4050
|
-
* - `readPreference` {String}
|
|
4051
|
-
* - `out*` {Object, default: {inline:1}} sets the output target for the map reduce job.
|
|
4052
|
-
*
|
|
4053
|
-
* #### * out options:
|
|
4054
|
-
*
|
|
4055
|
-
* - `{inline:1}` the results are returned in an array
|
|
4056
|
-
* - `{replace: 'collectionName'}` add the results to collectionName: the results replace the collection
|
|
4057
|
-
* - `{reduce: 'collectionName'}` add the results to collectionName: if dups are detected, uses the reducer / finalize functions
|
|
4058
|
-
* - `{merge: 'collectionName'}` add the results to collectionName: if dups exist the new docs overwrite the old
|
|
4059
|
-
*
|
|
4060
|
-
* If `options.out` is set to `replace`, `merge`, or `reduce`, a Model instance is returned that can be used for further querying. Queries run against this model are all executed with the [`lean` option](/docs/tutorials/lean.html); meaning only the js object is returned and no Mongoose magic is applied (getters, setters, etc).
|
|
4004
|
+
* If `opts.out` is set to `replace`, `merge`, or `reduce`, a Model instance is returned that can be used for further querying. Queries run against this model are all executed with the [`lean` option](/docs/tutorials/lean.html); meaning only the js object is returned and no Mongoose magic is applied (getters, setters, etc).
|
|
4061
4005
|
*
|
|
4062
4006
|
* #### Example:
|
|
4063
4007
|
*
|
|
4064
|
-
* const
|
|
4008
|
+
* const opts = {};
|
|
4065
4009
|
* // You can also define `map()` and `reduce()` as strings if your
|
|
4066
4010
|
* // linter complains about `emit()` not being defined
|
|
4067
|
-
*
|
|
4068
|
-
*
|
|
4069
|
-
*
|
|
4070
|
-
*
|
|
4011
|
+
* opts.map = 'function () { emit(this.name, 1) }';
|
|
4012
|
+
* opts.reduce = 'function (k, vals) { return vals.length }';
|
|
4013
|
+
* opts.out = { replace: 'createdCollectionNameForResults' }
|
|
4014
|
+
* opts.verbose = true;
|
|
4071
4015
|
*
|
|
4072
|
-
* User.mapReduce(
|
|
4016
|
+
* User.mapReduce(opts, function (err, model, stats) {
|
|
4073
4017
|
* console.log('map reduce took %d ms', stats.processtime)
|
|
4074
4018
|
* model.find().where('value').gt(10).exec(function (err, docs) {
|
|
4075
4019
|
* console.log(docs);
|
|
@@ -4078,8 +4022,8 @@ function _update(model, op, conditions, doc, options, callback) {
|
|
|
4078
4022
|
*
|
|
4079
4023
|
* // `mapReduce()` returns a promise. However, ES6 promises can only
|
|
4080
4024
|
* // resolve to exactly one value,
|
|
4081
|
-
*
|
|
4082
|
-
* const promise = User.mapReduce(
|
|
4025
|
+
* opts.resolveToObject = true;
|
|
4026
|
+
* const promise = User.mapReduce(opts);
|
|
4083
4027
|
* promise.then(function (res) {
|
|
4084
4028
|
* const model = res.model;
|
|
4085
4029
|
* const stats = res.stats;
|
|
@@ -4089,14 +4033,28 @@ function _update(model, op, conditions, doc, options, callback) {
|
|
|
4089
4033
|
* console.log(docs);
|
|
4090
4034
|
* }).then(null, handleError).end()
|
|
4091
4035
|
*
|
|
4092
|
-
* @param {Object}
|
|
4036
|
+
* @param {Object} opts an object specifying map-reduce options
|
|
4037
|
+
* @param {Boolean} [opts.verbose=false] provide statistics on job execution time
|
|
4038
|
+
* @param {ReadPreference|String} [opts.readPreference] a read-preference string or a read-preference instance
|
|
4039
|
+
* @param {Boolean} [opts.jsMode=false] it is possible to make the execution stay in JS. Provided in MongoDB > 2.0.X
|
|
4040
|
+
* @param {Object} [opts.scope] scope variables exposed to map/reduce/finalize during execution
|
|
4041
|
+
* @param {Function} [opts.finalize] finalize function
|
|
4042
|
+
* @param {Boolean} [opts.keeptemp=false] keep temporary data
|
|
4043
|
+
* @param {Number} [opts.limit] max number of documents
|
|
4044
|
+
* @param {Object} [opts.sort] sort input objects using this key
|
|
4045
|
+
* @param {Object} [opts.query] query filter object
|
|
4046
|
+
* @param {Object} [opts.out] sets the output target for the map reduce job
|
|
4047
|
+
* @param {Number} [opts.out.inline=1] the results are returned in an array
|
|
4048
|
+
* @param {String} [opts.out.replace] add the results to collectionName: the results replace the collection
|
|
4049
|
+
* @param {String} [opts.out.reduce] add the results to collectionName: if dups are detected, uses the reducer / finalize functions
|
|
4050
|
+
* @param {String} [opts.out.merge] add the results to collectionName: if dups exist the new docs overwrite the old
|
|
4093
4051
|
* @param {Function} [callback] optional callback
|
|
4094
4052
|
* @see https://www.mongodb.org/display/DOCS/MapReduce
|
|
4095
4053
|
* @return {Promise}
|
|
4096
4054
|
* @api public
|
|
4097
4055
|
*/
|
|
4098
4056
|
|
|
4099
|
-
Model.mapReduce = function mapReduce(
|
|
4057
|
+
Model.mapReduce = function mapReduce(opts, callback) {
|
|
4100
4058
|
_checkContext(this, 'mapReduce');
|
|
4101
4059
|
|
|
4102
4060
|
callback = this.$handleCallbackError(callback);
|
|
@@ -4109,20 +4067,20 @@ Model.mapReduce = function mapReduce(o, callback) {
|
|
|
4109
4067
|
Model.mapReduce.schema = new Schema({}, opts);
|
|
4110
4068
|
}
|
|
4111
4069
|
|
|
4112
|
-
if (!
|
|
4113
|
-
if (
|
|
4070
|
+
if (!opts.out) opts.out = { inline: 1 };
|
|
4071
|
+
if (opts.verbose !== false) opts.verbose = true;
|
|
4114
4072
|
|
|
4115
|
-
|
|
4116
|
-
|
|
4073
|
+
opts.map = String(opts.map);
|
|
4074
|
+
opts.reduce = String(opts.reduce);
|
|
4117
4075
|
|
|
4118
|
-
if (
|
|
4119
|
-
let q = new this.Query(
|
|
4076
|
+
if (opts.query) {
|
|
4077
|
+
let q = new this.Query(opts.query);
|
|
4120
4078
|
q.cast(this);
|
|
4121
|
-
|
|
4079
|
+
opts.query = q._conditions;
|
|
4122
4080
|
q = undefined;
|
|
4123
4081
|
}
|
|
4124
4082
|
|
|
4125
|
-
this.$__collection.mapReduce(null, null,
|
|
4083
|
+
this.$__collection.mapReduce(null, null, opts, (err, res) => {
|
|
4126
4084
|
if (err) {
|
|
4127
4085
|
return cb(err);
|
|
4128
4086
|
}
|