mongoose 6.4.3 → 6.4.6
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 +3 -2
- package/lib/connection.js +9 -9
- package/lib/cursor/AggregationCursor.js +3 -2
- package/lib/cursor/QueryCursor.js +5 -4
- package/lib/document.js +44 -16
- package/lib/error/index.js +12 -12
- package/lib/error/messages.js +2 -2
- package/lib/helpers/query/castUpdate.js +4 -4
- 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 +6 -2
- package/lib/model.js +129 -170
- package/lib/options/SchemaArrayOptions.js +2 -2
- package/lib/options/SchemaBufferOptions.js +1 -1
- package/lib/options/SchemaDateOptions.js +3 -3
- package/lib/options/SchemaDocumentArrayOptions.js +2 -2
- package/lib/options/SchemaMapOptions.js +1 -1
- package/lib/options/SchemaNumberOptions.js +4 -4
- package/lib/options/SchemaObjectIdOptions.js +2 -2
- package/lib/options/SchemaStringOptions.js +8 -8
- package/lib/options/SchemaSubdocumentOptions.js +1 -1
- package/lib/options/SchemaTypeOptions.js +14 -14
- package/lib/options/VirtualOptions.js +11 -11
- package/lib/query.js +82 -73
- package/lib/schema/SubdocumentPath.js +10 -6
- package/lib/schema/array.js +3 -3
- package/lib/schema/boolean.js +5 -5
- package/lib/schema/buffer.js +2 -2
- package/lib/schema/date.js +2 -2
- package/lib/schema/decimal128.js +2 -2
- package/lib/schema/documentarray.js +12 -8
- package/lib/schema/mixed.js +2 -2
- package/lib/schema/number.js +2 -2
- package/lib/schema/objectid.js +2 -2
- package/lib/schema/string.js +2 -2
- package/lib/schema.js +76 -34
- package/lib/schematype.js +7 -9
- package/lib/types/DocumentArray/methods/index.js +1 -1
- package/lib/types/buffer.js +8 -4
- package/lib/types/decimal128.js +1 -1
- package/lib/types/map.js +1 -1
- package/lib/types/objectid.js +1 -1
- package/lib/types/subdocument.js +1 -1
- package/lib/virtualtype.js +5 -5
- package/package.json +7 -7
- 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 +2 -1
- package/types/query.d.ts +6 -1
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,9 +210,10 @@ 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
|
-
* @static
|
|
216
|
+
* @static
|
|
216
217
|
*/
|
|
217
218
|
|
|
218
219
|
Model.events;
|
|
@@ -888,25 +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
|
|
911
|
+
* @memberOf Model
|
|
912
|
+
* @method increment
|
|
902
913
|
* @api public
|
|
903
914
|
*/
|
|
904
915
|
|
|
905
|
-
function increment() {
|
|
906
|
-
this.$__.version = VERSION_ALL;
|
|
907
|
-
return this;
|
|
908
|
-
}
|
|
909
|
-
|
|
910
916
|
Model.prototype.increment = increment;
|
|
911
917
|
|
|
912
918
|
/**
|
|
@@ -936,22 +942,12 @@ Model.prototype.$__where = function _where(where) {
|
|
|
936
942
|
* Removes this document from the db.
|
|
937
943
|
*
|
|
938
944
|
* #### Example:
|
|
939
|
-
* product.remove(function (err, product) {
|
|
940
|
-
* if (err) return handleError(err);
|
|
941
|
-
* Product.findById(product._id, function (err, product) {
|
|
942
|
-
* console.log(product) // null
|
|
943
|
-
* })
|
|
944
|
-
* })
|
|
945
|
-
*
|
|
946
|
-
*
|
|
947
|
-
* 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
|
|
948
945
|
*
|
|
949
|
-
*
|
|
950
|
-
*
|
|
951
|
-
*
|
|
952
|
-
*
|
|
953
|
-
*
|
|
954
|
-
* })
|
|
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
|
|
955
951
|
*
|
|
956
952
|
* @param {Object} [options]
|
|
957
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).
|
|
@@ -994,6 +990,7 @@ Model.prototype.delete = Model.prototype.remove;
|
|
|
994
990
|
* Removes this document from the db. Equivalent to `.remove()`.
|
|
995
991
|
*
|
|
996
992
|
* #### Example:
|
|
993
|
+
*
|
|
997
994
|
* product = await product.deleteOne();
|
|
998
995
|
* await Product.findById(product._id); // null
|
|
999
996
|
*
|
|
@@ -1103,6 +1100,7 @@ Model.prototype.$model = function $model(name) {
|
|
|
1103
1100
|
* `MyModel.findOne({ answer: 42 }).select({ _id: 1 }).lean()`
|
|
1104
1101
|
*
|
|
1105
1102
|
* #### Example:
|
|
1103
|
+
*
|
|
1106
1104
|
* await Character.deleteMany({});
|
|
1107
1105
|
* await Character.create({ name: 'Jean-Luc Picard' });
|
|
1108
1106
|
*
|
|
@@ -1272,7 +1270,7 @@ for (const i in EventEmitter.prototype) {
|
|
|
1272
1270
|
*
|
|
1273
1271
|
* #### Example:
|
|
1274
1272
|
*
|
|
1275
|
-
* const eventSchema = new Schema({ thing: { type: 'string', unique: true }})
|
|
1273
|
+
* const eventSchema = new Schema({ thing: { type: 'string', unique: true } })
|
|
1276
1274
|
* // This calls `Event.init()` implicitly, so you don't need to call
|
|
1277
1275
|
* // `Event.init()` on your own.
|
|
1278
1276
|
* const Event = mongoose.model('Event', eventSchema);
|
|
@@ -1453,7 +1451,7 @@ Model.createCollection = function createCollection(options, callback) {
|
|
|
1453
1451
|
* @param {Object} [options] options to pass to `ensureIndexes()`
|
|
1454
1452
|
* @param {Boolean} [options.background=null] if specified, overrides each index's `background` property
|
|
1455
1453
|
* @param {Function} [callback] optional callback
|
|
1456
|
-
* @return {Promise|undefined} Returns `undefined` if callback is specified, returns a promise if no callback.
|
|
1454
|
+
* @return {Promise|undefined} Returns `undefined` if callback is specified, returns a promise if no callback, when the Promise resolves the value is a list of the dropped indexes.
|
|
1457
1455
|
* @api public
|
|
1458
1456
|
*/
|
|
1459
1457
|
|
|
@@ -1490,7 +1488,7 @@ Model.syncIndexes = function syncIndexes(options, callback) {
|
|
|
1490
1488
|
* Model.syncIndexes().
|
|
1491
1489
|
*
|
|
1492
1490
|
* @param {Object} [options]
|
|
1493
|
-
* @param {Function} callback optional callback
|
|
1491
|
+
* @param {Function} [callback] optional callback
|
|
1494
1492
|
* @returns {Promise} which contains an object, {toDrop, toCreate}, which
|
|
1495
1493
|
* are indexes that would be dropped in MongoDB and indexes that would be created in MongoDB.
|
|
1496
1494
|
*/
|
|
@@ -1654,7 +1652,7 @@ Model.listIndexes = function init(callback) {
|
|
|
1654
1652
|
*
|
|
1655
1653
|
* #### Example:
|
|
1656
1654
|
*
|
|
1657
|
-
* const eventSchema = new Schema({ thing: { type: 'string', unique: true }})
|
|
1655
|
+
* const eventSchema = new Schema({ thing: { type: 'string', unique: true } })
|
|
1658
1656
|
* const Event = mongoose.model('Event', eventSchema);
|
|
1659
1657
|
*
|
|
1660
1658
|
* Event.on('index', function (err) {
|
|
@@ -1821,7 +1819,7 @@ function _ensureIndexes(model, options, callback) {
|
|
|
1821
1819
|
* Schema the model uses.
|
|
1822
1820
|
*
|
|
1823
1821
|
* @property schema
|
|
1824
|
-
* @
|
|
1822
|
+
* @static
|
|
1825
1823
|
* @api public
|
|
1826
1824
|
* @memberOf Model
|
|
1827
1825
|
*/
|
|
@@ -1889,6 +1887,7 @@ Model.discriminators;
|
|
|
1889
1887
|
* .exec(function(err, characters) {})
|
|
1890
1888
|
*
|
|
1891
1889
|
* #### Note:
|
|
1890
|
+
*
|
|
1892
1891
|
* Only translate arguments of object type anything else is returned raw
|
|
1893
1892
|
*
|
|
1894
1893
|
* @param {Object} fields fields/conditions that may contain aliased keys
|
|
@@ -2128,7 +2127,7 @@ Model.deleteMany = function deleteMany(conditions, options, callback) {
|
|
|
2128
2127
|
* await MyModel.find({ name: /john/i }, null, { skip: 10 }).exec();
|
|
2129
2128
|
*
|
|
2130
2129
|
* @param {Object|ObjectId} filter
|
|
2131
|
-
* @param {Object|String|
|
|
2130
|
+
* @param {Object|String|String[]} [projection] optional fields to return, see [`Query.prototype.select()`](https://mongoosejs.com/docs/api.html#query_Query-select)
|
|
2132
2131
|
* @param {Object} [options] optional see [`Query.prototype.setOptions()`](https://mongoosejs.com/docs/api.html#query_Query-setOptions)
|
|
2133
2132
|
* @param {Function} [callback]
|
|
2134
2133
|
* @return {Query}
|
|
@@ -2191,7 +2190,7 @@ Model.find = function find(conditions, projection, options, callback) {
|
|
|
2191
2190
|
* await Adventure.findById(id, 'name length').exec();
|
|
2192
2191
|
*
|
|
2193
2192
|
* @param {Any} id value of `_id` to query by
|
|
2194
|
-
* @param {Object|String|
|
|
2193
|
+
* @param {Object|String|String[]} [projection] optional fields to return, see [`Query.prototype.select()`](#query_Query-select)
|
|
2195
2194
|
* @param {Object} [options] optional see [`Query.prototype.setOptions()`](https://mongoosejs.com/docs/api.html#query_Query-setOptions)
|
|
2196
2195
|
* @param {Function} [callback]
|
|
2197
2196
|
* @return {Query}
|
|
@@ -2234,7 +2233,7 @@ Model.findById = function findById(id, projection, options, callback) {
|
|
|
2234
2233
|
* await Adventure.findOne({ country: 'Croatia' }, 'name length').exec();
|
|
2235
2234
|
*
|
|
2236
2235
|
* @param {Object} [conditions]
|
|
2237
|
-
* @param {Object|String|
|
|
2236
|
+
* @param {Object|String|String[]} [projection] optional fields to return, see [`Query.prototype.select()`](#query_Query-select)
|
|
2238
2237
|
* @param {Object} [options] optional see [`Query.prototype.setOptions()`](https://mongoosejs.com/docs/api.html#query_Query-setOptions)
|
|
2239
2238
|
* @param {Function} [callback]
|
|
2240
2239
|
* @return {Query}
|
|
@@ -2383,9 +2382,9 @@ Model.count = function count(conditions, callback) {
|
|
|
2383
2382
|
*
|
|
2384
2383
|
* Passing a `callback` executes the query.
|
|
2385
2384
|
*
|
|
2386
|
-
* #### Example
|
|
2385
|
+
* #### Example:
|
|
2387
2386
|
*
|
|
2388
|
-
* Link.distinct('url', { clicks: {$gt: 100}}, function (err, result) {
|
|
2387
|
+
* Link.distinct('url', { clicks: { $gt: 100 } }, function (err, result) {
|
|
2389
2388
|
* if (err) return handleError(err);
|
|
2390
2389
|
*
|
|
2391
2390
|
* assert(Array.isArray(result));
|
|
@@ -2421,7 +2420,7 @@ Model.distinct = function distinct(field, conditions, callback) {
|
|
|
2421
2420
|
*
|
|
2422
2421
|
* For example, instead of writing:
|
|
2423
2422
|
*
|
|
2424
|
-
* User.find({age: {$gte: 21, $lte: 65}}, callback);
|
|
2423
|
+
* User.find({ age: { $gte: 21, $lte: 65 } }, callback);
|
|
2425
2424
|
*
|
|
2426
2425
|
* we can instead write:
|
|
2427
2426
|
*
|
|
@@ -2475,19 +2474,6 @@ Model.$where = function $where() {
|
|
|
2475
2474
|
*
|
|
2476
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.
|
|
2477
2476
|
*
|
|
2478
|
-
* #### Options:
|
|
2479
|
-
*
|
|
2480
|
-
* - `new`: bool - if true, return the modified document rather than the original. defaults to false (changed in 4.0)
|
|
2481
|
-
* - `upsert`: bool - creates the object if it doesn't exist. defaults to false.
|
|
2482
|
-
* - `overwrite`: bool - if true, replace the entire document.
|
|
2483
|
-
* - `fields`: {Object|String} - Field selection. Equivalent to `.select(fields).findOneAndUpdate()`
|
|
2484
|
-
* - `maxTimeMS`: puts a time limit on the query - requires mongodb >= 2.6.0
|
|
2485
|
-
* - `sort`: if multiple docs are found by the conditions, sets the sort order to choose which doc to update
|
|
2486
|
-
* - `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.
|
|
2487
|
-
* - `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.
|
|
2488
|
-
* - `rawResult`: if true, returns the [raw result from the MongoDB driver](https://mongodb.github.io/node-mongodb-native/4.3/interfaces/ModifyResult.html)
|
|
2489
|
-
* - `strict`: overwrites the schema's [strict mode option](https://mongoosejs.com/docs/guide.html#strict) for this update
|
|
2490
|
-
*
|
|
2491
2477
|
* #### Examples:
|
|
2492
2478
|
*
|
|
2493
2479
|
* A.findOneAndUpdate(conditions, update, options, callback) // executes
|
|
@@ -2532,7 +2518,14 @@ Model.$where = function $where() {
|
|
|
2532
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.
|
|
2533
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).
|
|
2534
2520
|
* @param {Boolean} [options.upsert=false] if true, and no documents found, insert a new document
|
|
2535
|
-
* @param {Object|String|
|
|
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)
|
|
2536
2529
|
* @param {Function} [callback]
|
|
2537
2530
|
* @return {Query}
|
|
2538
2531
|
* @see Tutorial /docs/tutorials/findoneandupdate.html
|
|
@@ -2613,17 +2606,6 @@ function _decorateUpdateWithVersionKey(update, options, versionKey) {
|
|
|
2613
2606
|
*
|
|
2614
2607
|
* - `findOneAndUpdate()`
|
|
2615
2608
|
*
|
|
2616
|
-
* #### Options:
|
|
2617
|
-
*
|
|
2618
|
-
* - `new`: bool - true to return the modified document rather than the original. defaults to false
|
|
2619
|
-
* - `upsert`: bool - creates the object if it doesn't exist. defaults to false.
|
|
2620
|
-
* - `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.
|
|
2621
|
-
* - `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.
|
|
2622
|
-
* - `sort`: if multiple docs are found by the conditions, sets the sort order to choose which doc to update
|
|
2623
|
-
* - `select`: sets the document fields to return
|
|
2624
|
-
* - `rawResult`: if true, returns the [raw result from the MongoDB driver](https://mongodb.github.io/node-mongodb-native/4.3/interfaces/ModifyResult.html)
|
|
2625
|
-
* - `strict`: overwrites the schema's [strict mode option](https://mongoosejs.com/docs/guide.html#strict) for this update
|
|
2626
|
-
*
|
|
2627
2609
|
* #### Examples:
|
|
2628
2610
|
*
|
|
2629
2611
|
* A.findByIdAndUpdate(id, update, options, callback) // executes
|
|
@@ -2653,11 +2635,9 @@ function _decorateUpdateWithVersionKey(update, options, versionKey) {
|
|
|
2653
2635
|
* If you need full-fledged validation, use the traditional approach of first
|
|
2654
2636
|
* retrieving the document.
|
|
2655
2637
|
*
|
|
2656
|
-
* Model.findById(id
|
|
2657
|
-
*
|
|
2658
|
-
*
|
|
2659
|
-
* doc.save(callback);
|
|
2660
|
-
* });
|
|
2638
|
+
* const doc = await Model.findById(id)
|
|
2639
|
+
* doc.name = 'jason bourne';
|
|
2640
|
+
* await doc.save();
|
|
2661
2641
|
*
|
|
2662
2642
|
* @param {Object|Number|String} id value of `_id` to query by
|
|
2663
2643
|
* @param {Object} [update]
|
|
@@ -2668,6 +2648,13 @@ function _decorateUpdateWithVersionKey(update, options, versionKey) {
|
|
|
2668
2648
|
* @param {Boolean|String} [options.strict] overwrites the schema's [strict mode option](https://mongoosejs.com/docs/guide.html#strict)
|
|
2669
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.
|
|
2670
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.
|
|
2671
2658
|
* @param {Function} [callback]
|
|
2672
2659
|
* @return {Query}
|
|
2673
2660
|
* @see Model.findOneAndUpdate #model_Model.findOneAndUpdate
|
|
@@ -2716,15 +2703,6 @@ Model.findByIdAndUpdate = function(id, update, options, callback) {
|
|
|
2716
2703
|
* this distinction is purely pedantic. You should use `findOneAndDelete()`
|
|
2717
2704
|
* unless you have a good reason not to.
|
|
2718
2705
|
*
|
|
2719
|
-
* #### Options:
|
|
2720
|
-
*
|
|
2721
|
-
* - `sort`: if multiple docs are found by the conditions, sets the sort order to choose which doc to update
|
|
2722
|
-
* - `maxTimeMS`: puts a time limit on the query - requires mongodb >= 2.6.0
|
|
2723
|
-
* - `select`: sets the document fields to return, ex. `{ projection: { _id: 0 } }`
|
|
2724
|
-
* - `projection`: equivalent to `select`
|
|
2725
|
-
* - `rawResult`: if true, returns the [raw result from the MongoDB driver](https://mongodb.github.io/node-mongodb-native/4.3/interfaces/ModifyResult.html)
|
|
2726
|
-
* - `strict`: overwrites the schema's [strict mode option](https://mongoosejs.com/docs/guide.html#strict) for this update
|
|
2727
|
-
*
|
|
2728
2706
|
* #### Examples:
|
|
2729
2707
|
*
|
|
2730
2708
|
* A.findOneAndDelete(conditions, options, callback) // executes
|
|
@@ -2739,17 +2717,19 @@ Model.findByIdAndUpdate = function(id, update, options, callback) {
|
|
|
2739
2717
|
* If you need full-fledged validation, use the traditional approach of first
|
|
2740
2718
|
* retrieving the document.
|
|
2741
2719
|
*
|
|
2742
|
-
* Model.findById(id
|
|
2743
|
-
*
|
|
2744
|
-
*
|
|
2745
|
-
* doc.save(callback);
|
|
2746
|
-
* });
|
|
2720
|
+
* const doc = await Model.findById(id)
|
|
2721
|
+
* doc.name = 'jason bourne';
|
|
2722
|
+
* await doc.save();
|
|
2747
2723
|
*
|
|
2748
2724
|
* @param {Object} conditions
|
|
2749
2725
|
* @param {Object} [options] optional see [`Query.prototype.setOptions()`](https://mongoosejs.com/docs/api.html#query_Query-setOptions)
|
|
2750
2726
|
* @param {Boolean|String} [options.strict] overwrites the schema's [strict mode option](https://mongoosejs.com/docs/guide.html#strict)
|
|
2751
|
-
* @param {Object|String|
|
|
2727
|
+
* @param {Object|String|String[]} [options.projection=null] optional fields to return, see [`Query.prototype.select()`](#query_Query-select)
|
|
2752
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
|
|
2753
2733
|
* @param {Function} [callback]
|
|
2754
2734
|
* @return {Query}
|
|
2755
2735
|
* @api public
|
|
@@ -2829,15 +2809,6 @@ Model.findByIdAndDelete = function(id, options, callback) {
|
|
|
2829
2809
|
*
|
|
2830
2810
|
* - `findOneAndReplace()`
|
|
2831
2811
|
*
|
|
2832
|
-
* #### Options:
|
|
2833
|
-
*
|
|
2834
|
-
* - `sort`: if multiple docs are found by the conditions, sets the sort order to choose which doc to update
|
|
2835
|
-
* - `maxTimeMS`: puts a time limit on the query - requires mongodb >= 2.6.0
|
|
2836
|
-
* - `select`: sets the document fields to return
|
|
2837
|
-
* - `projection`: like select, it determines which fields to return, ex. `{ projection: { _id: 0 } }`
|
|
2838
|
-
* - `rawResult`: if true, returns the [raw result from the MongoDB driver](https://mongodb.github.io/node-mongodb-native/4.3/interfaces/ModifyResult.html)
|
|
2839
|
-
* - `strict`: overwrites the schema's [strict mode option](https://mongoosejs.com/docs/guide.html#strict) for this update
|
|
2840
|
-
*
|
|
2841
2812
|
* #### Examples:
|
|
2842
2813
|
*
|
|
2843
2814
|
* A.findOneAndReplace(filter, replacement, options, callback) // executes
|
|
@@ -2854,7 +2825,11 @@ Model.findByIdAndDelete = function(id, options, callback) {
|
|
|
2854
2825
|
* @param {ClientSession} [options.session=null] The session associated with this query. See [transactions docs](/docs/transactions.html).
|
|
2855
2826
|
* @param {Boolean|String} [options.strict] overwrites the schema's [strict mode option](https://mongoosejs.com/docs/guide.html#strict)
|
|
2856
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.
|
|
2857
|
-
* @param {Object|String|
|
|
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
|
|
2858
2833
|
* @param {Function} [callback]
|
|
2859
2834
|
* @return {Query}
|
|
2860
2835
|
* @api public
|
|
@@ -2908,15 +2883,6 @@ Model.findOneAndReplace = function(filter, replacement, options, callback) {
|
|
|
2908
2883
|
*
|
|
2909
2884
|
* - `findOneAndRemove()`
|
|
2910
2885
|
*
|
|
2911
|
-
* #### Options:
|
|
2912
|
-
*
|
|
2913
|
-
* - `sort`: if multiple docs are found by the conditions, sets the sort order to choose which doc to update
|
|
2914
|
-
* - `maxTimeMS`: puts a time limit on the query - requires mongodb >= 2.6.0
|
|
2915
|
-
* - `select`: sets the document fields to return
|
|
2916
|
-
* - `projection`: like select, it determines which fields to return, ex. `{ projection: { _id: 0 } }`
|
|
2917
|
-
* - `rawResult`: if true, returns the [raw result from the MongoDB driver](https://mongodb.github.io/node-mongodb-native/4.3/interfaces/ModifyResult.html)
|
|
2918
|
-
* - `strict`: overwrites the schema's [strict mode option](https://mongoosejs.com/docs/guide.html#strict) for this update
|
|
2919
|
-
*
|
|
2920
2886
|
* #### Examples:
|
|
2921
2887
|
*
|
|
2922
2888
|
* A.findOneAndRemove(conditions, options, callback) // executes
|
|
@@ -2931,17 +2897,19 @@ Model.findOneAndReplace = function(filter, replacement, options, callback) {
|
|
|
2931
2897
|
* If you need full-fledged validation, use the traditional approach of first
|
|
2932
2898
|
* retrieving the document.
|
|
2933
2899
|
*
|
|
2934
|
-
* Model.findById(id
|
|
2935
|
-
*
|
|
2936
|
-
*
|
|
2937
|
-
* doc.save(callback);
|
|
2938
|
-
* });
|
|
2900
|
+
* const doc = await Model.findById(id);
|
|
2901
|
+
* doc.name = 'jason bourne';
|
|
2902
|
+
* await doc.save();
|
|
2939
2903
|
*
|
|
2940
2904
|
* @param {Object} conditions
|
|
2941
2905
|
* @param {Object} [options] optional see [`Query.prototype.setOptions()`](https://mongoosejs.com/docs/api.html#query_Query-setOptions)
|
|
2942
2906
|
* @param {ClientSession} [options.session=null] The session associated with this query. See [transactions docs](/docs/transactions.html).
|
|
2943
2907
|
* @param {Boolean|String} [options.strict] overwrites the schema's [strict mode option](https://mongoosejs.com/docs/guide.html#strict)
|
|
2944
|
-
* @param {Object|String|
|
|
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
|
|
2945
2913
|
* @param {Function} [callback]
|
|
2946
2914
|
* @return {Query}
|
|
2947
2915
|
* @see mongodb https://www.mongodb.org/display/DOCS/findAndModify+Command
|
|
@@ -2988,13 +2956,6 @@ Model.findOneAndRemove = function(conditions, options, callback) {
|
|
|
2988
2956
|
*
|
|
2989
2957
|
* - `findOneAndRemove()`
|
|
2990
2958
|
*
|
|
2991
|
-
* #### Options:
|
|
2992
|
-
*
|
|
2993
|
-
* - `sort`: if multiple docs are found by the conditions, sets the sort order to choose which doc to update
|
|
2994
|
-
* - `select`: sets the document fields to return
|
|
2995
|
-
* - `rawResult`: if true, returns the [raw result from the MongoDB driver](https://mongodb.github.io/node-mongodb-native/4.3/interfaces/ModifyResult.html)
|
|
2996
|
-
* - `strict`: overwrites the schema's [strict mode option](https://mongoosejs.com/docs/guide.html#strict) for this update
|
|
2997
|
-
*
|
|
2998
2959
|
* #### Examples:
|
|
2999
2960
|
*
|
|
3000
2961
|
* A.findByIdAndRemove(id, options, callback) // executes
|
|
@@ -3007,7 +2968,10 @@ Model.findOneAndRemove = function(conditions, options, callback) {
|
|
|
3007
2968
|
* @param {Object} [options] optional see [`Query.prototype.setOptions()`](https://mongoosejs.com/docs/api.html#query_Query-setOptions)
|
|
3008
2969
|
* @param {Boolean|String} [options.strict] overwrites the schema's [strict mode option](https://mongoosejs.com/docs/guide.html#strict)
|
|
3009
2970
|
* @param {ClientSession} [options.session=null] The session associated with this query. See [transactions docs](/docs/transactions.html).
|
|
3010
|
-
* @param {Object|String|
|
|
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.
|
|
3011
2975
|
* @param {Function} [callback]
|
|
3012
2976
|
* @return {Query}
|
|
3013
2977
|
* @see Model.findOneAndRemove #model_Model.findOneAndRemove
|
|
@@ -3287,11 +3251,11 @@ Model.startSession = function() {
|
|
|
3287
3251
|
*
|
|
3288
3252
|
* @param {Array|Object|*} doc(s)
|
|
3289
3253
|
* @param {Object} [options] see the [mongodb driver options](https://mongodb.github.io/node-mongodb-native/2.2/api/Collection.html#insertMany)
|
|
3290
|
-
* @param {Boolean} [options.ordered
|
|
3291
|
-
* @param {Boolean} [options.rawResult
|
|
3292
|
-
* @param {Boolean} [options.lean
|
|
3293
|
-
* @param {Number} [options.limit
|
|
3294
|
-
* @param {String|Object|Array} [options.populate
|
|
3254
|
+
* @param {Boolean} [options.ordered=true] if true, will fail fast on the first error encountered. If false, will insert all the documents it can and report errors later. An `insertMany()` with `ordered = false` is called an "unordered" `insertMany()`.
|
|
3255
|
+
* @param {Boolean} [options.rawResult=false] if false, the returned promise resolves to the documents that passed mongoose document validation. If `true`, will return the [raw result from the MongoDB driver](https://mongodb.github.io/node-mongodb-native/2.2/api/Collection.html#~insertWriteOpCallback) with a `mongoose` property that contains `validationErrors` if this is an unordered `insertMany`.
|
|
3256
|
+
* @param {Boolean} [options.lean=false] if `true`, skips hydrating and validating the documents. This option is useful if you need the extra performance, but Mongoose won't validate the documents before inserting.
|
|
3257
|
+
* @param {Number} [options.limit=null] this limits the number of documents being processed (validation/casting) by mongoose in parallel, this does **NOT** send the documents in batches to MongoDB. Use this option if you're processing a large number of documents and your app is running out of memory.
|
|
3258
|
+
* @param {String|Object|Array} [options.populate=null] populates the result documents. This option is a no-op if `rawResult` is set.
|
|
3295
3259
|
* @param {Function} [callback] callback
|
|
3296
3260
|
* @return {Promise} resolving to the raw result from the MongoDB driver if `options.rawResult` was `true`, or the documents that passed validation, otherwise
|
|
3297
3261
|
* @api public
|
|
@@ -3395,7 +3359,7 @@ Model.$__insertMany = function(arr, options, callback) {
|
|
|
3395
3359
|
if (doc.$__schema.options.versionKey) {
|
|
3396
3360
|
doc[doc.$__schema.options.versionKey] = 0;
|
|
3397
3361
|
}
|
|
3398
|
-
if (doc.initializeTimestamps) {
|
|
3362
|
+
if ((!options || options.timestamps !== false) && doc.initializeTimestamps) {
|
|
3399
3363
|
return doc.initializeTimestamps().toObject(internalToObjectOptions);
|
|
3400
3364
|
}
|
|
3401
3365
|
return doc.toObject(internalToObjectOptions);
|
|
@@ -3613,7 +3577,7 @@ Model.bulkWrite = function(ops, options, callback) {
|
|
|
3613
3577
|
*
|
|
3614
3578
|
* `bulkSave` uses `bulkWrite` under the hood, so it's mostly useful when dealing with many documents (10K+)
|
|
3615
3579
|
*
|
|
3616
|
-
* @param {
|
|
3580
|
+
* @param {Array<Document>} documents
|
|
3617
3581
|
* @param {Object} [options] options passed to the underlying `bulkWrite()`
|
|
3618
3582
|
* @param {ClientSession} [options.session=null] The session associated with this bulk write. See [transactions docs](/docs/transactions.html).
|
|
3619
3583
|
* @param {String|number} [options.w=1] The [write concern](https://docs.mongodb.com/manual/reference/write-concern/). See [`Query#w()`](/docs/api.html#query_Query-w) for more information.
|
|
@@ -3686,11 +3650,13 @@ function handleSuccessfulWrite(document, resolve, reject) {
|
|
|
3686
3650
|
}
|
|
3687
3651
|
|
|
3688
3652
|
/**
|
|
3653
|
+
* Build bulk write operations for `bulkSave()`.
|
|
3689
3654
|
*
|
|
3690
|
-
* @param {
|
|
3655
|
+
* @param {Array<Document>} documents The array of documents to build write operations of
|
|
3691
3656
|
* @param {Object} options
|
|
3692
3657
|
* @param {Boolean} options.skipValidation defaults to `false`, when set to true, building the write operations will bypass validating the documents.
|
|
3693
|
-
* @
|
|
3658
|
+
* @return {Array<Promise>} Returns a array of all Promises the function executes to be awaited.
|
|
3659
|
+
* @api private
|
|
3694
3660
|
*/
|
|
3695
3661
|
Model.buildBulkWriteOperations = function buildBulkWriteOperations(documents, options) {
|
|
3696
3662
|
if (!Array.isArray(documents)) {
|
|
@@ -3764,7 +3730,7 @@ Model.buildBulkWriteOperations = function buildBulkWriteOperations(documents, op
|
|
|
3764
3730
|
* const mongooseCandy = Candy.hydrate({ _id: '54108337212ffb6d459f854c', type: 'jelly bean' });
|
|
3765
3731
|
*
|
|
3766
3732
|
* @param {Object} obj
|
|
3767
|
-
* @param {Object|String|
|
|
3733
|
+
* @param {Object|String|String[]} [projection] optional projection containing which fields should be selected for this document
|
|
3768
3734
|
* @return {Document} document instance
|
|
3769
3735
|
* @api public
|
|
3770
3736
|
*/
|
|
@@ -3855,7 +3821,6 @@ Model.hydrate = function(obj, projection) {
|
|
|
3855
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.
|
|
3856
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`.
|
|
3857
3823
|
* @param {Function} [callback] params are (error, [updateWriteOpResult](https://mongodb.github.io/node-mongodb-native/3.6/api/Collection.html#~updateWriteOpResult))
|
|
3858
|
-
* @param {Function} [callback]
|
|
3859
3824
|
* @return {Query}
|
|
3860
3825
|
* @see MongoDB docs https://docs.mongodb.com/manual/reference/command/update/#update-command-output
|
|
3861
3826
|
* @see writeOpResult https://mongodb.github.io/node-mongodb-native/3.6/api/Collection.html#~updateWriteOpResult
|
|
@@ -4021,54 +3986,34 @@ function _update(model, op, conditions, doc, options, callback) {
|
|
|
4021
3986
|
/**
|
|
4022
3987
|
* Executes a mapReduce command.
|
|
4023
3988
|
*
|
|
4024
|
-
* `
|
|
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.
|
|
4025
3990
|
*
|
|
4026
3991
|
* This function does not trigger any middleware.
|
|
4027
3992
|
*
|
|
4028
3993
|
* #### Example:
|
|
4029
3994
|
*
|
|
4030
|
-
* const
|
|
3995
|
+
* const opts = {};
|
|
4031
3996
|
* // `map()` and `reduce()` are run on the MongoDB server, not Node.js,
|
|
4032
3997
|
* // these functions are converted to strings
|
|
4033
|
-
*
|
|
4034
|
-
*
|
|
4035
|
-
* 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) {
|
|
4036
4001
|
* console.log(results)
|
|
4037
4002
|
* })
|
|
4038
4003
|
*
|
|
4039
|
-
*
|
|
4040
|
-
*
|
|
4041
|
-
* - `query` {Object} query filter object.
|
|
4042
|
-
* - `sort` {Object} sort input objects using this key
|
|
4043
|
-
* - `limit` {Number} max number of documents
|
|
4044
|
-
* - `keeptemp` {Boolean, default:false} keep temporary data
|
|
4045
|
-
* - `finalize` {Function} finalize function
|
|
4046
|
-
* - `scope` {Object} scope variables exposed to map/reduce/finalize during execution
|
|
4047
|
-
* - `jsMode` {Boolean, default:false} it is possible to make the execution stay in JS. Provided in MongoDB > 2.0.X
|
|
4048
|
-
* - `verbose` {Boolean, default:false} provide statistics on job execution time.
|
|
4049
|
-
* - `readPreference` {String}
|
|
4050
|
-
* - `out*` {Object, default: {inline:1}} sets the output target for the map reduce job.
|
|
4051
|
-
*
|
|
4052
|
-
* #### * out options:
|
|
4053
|
-
*
|
|
4054
|
-
* - `{inline:1}` the results are returned in an array
|
|
4055
|
-
* - `{replace: 'collectionName'}` add the results to collectionName: the results replace the collection
|
|
4056
|
-
* - `{reduce: 'collectionName'}` add the results to collectionName: if dups are detected, uses the reducer / finalize functions
|
|
4057
|
-
* - `{merge: 'collectionName'}` add the results to collectionName: if dups exist the new docs overwrite the old
|
|
4058
|
-
*
|
|
4059
|
-
* 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).
|
|
4060
4005
|
*
|
|
4061
4006
|
* #### Example:
|
|
4062
4007
|
*
|
|
4063
|
-
* const
|
|
4008
|
+
* const opts = {};
|
|
4064
4009
|
* // You can also define `map()` and `reduce()` as strings if your
|
|
4065
4010
|
* // linter complains about `emit()` not being defined
|
|
4066
|
-
*
|
|
4067
|
-
*
|
|
4068
|
-
*
|
|
4069
|
-
*
|
|
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;
|
|
4070
4015
|
*
|
|
4071
|
-
* User.mapReduce(
|
|
4016
|
+
* User.mapReduce(opts, function (err, model, stats) {
|
|
4072
4017
|
* console.log('map reduce took %d ms', stats.processtime)
|
|
4073
4018
|
* model.find().where('value').gt(10).exec(function (err, docs) {
|
|
4074
4019
|
* console.log(docs);
|
|
@@ -4077,8 +4022,8 @@ function _update(model, op, conditions, doc, options, callback) {
|
|
|
4077
4022
|
*
|
|
4078
4023
|
* // `mapReduce()` returns a promise. However, ES6 promises can only
|
|
4079
4024
|
* // resolve to exactly one value,
|
|
4080
|
-
*
|
|
4081
|
-
* const promise = User.mapReduce(
|
|
4025
|
+
* opts.resolveToObject = true;
|
|
4026
|
+
* const promise = User.mapReduce(opts);
|
|
4082
4027
|
* promise.then(function (res) {
|
|
4083
4028
|
* const model = res.model;
|
|
4084
4029
|
* const stats = res.stats;
|
|
@@ -4088,14 +4033,28 @@ function _update(model, op, conditions, doc, options, callback) {
|
|
|
4088
4033
|
* console.log(docs);
|
|
4089
4034
|
* }).then(null, handleError).end()
|
|
4090
4035
|
*
|
|
4091
|
-
* @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
|
|
4092
4051
|
* @param {Function} [callback] optional callback
|
|
4093
4052
|
* @see https://www.mongodb.org/display/DOCS/MapReduce
|
|
4094
4053
|
* @return {Promise}
|
|
4095
4054
|
* @api public
|
|
4096
4055
|
*/
|
|
4097
4056
|
|
|
4098
|
-
Model.mapReduce = function mapReduce(
|
|
4057
|
+
Model.mapReduce = function mapReduce(opts, callback) {
|
|
4099
4058
|
_checkContext(this, 'mapReduce');
|
|
4100
4059
|
|
|
4101
4060
|
callback = this.$handleCallbackError(callback);
|
|
@@ -4108,20 +4067,20 @@ Model.mapReduce = function mapReduce(o, callback) {
|
|
|
4108
4067
|
Model.mapReduce.schema = new Schema({}, opts);
|
|
4109
4068
|
}
|
|
4110
4069
|
|
|
4111
|
-
if (!
|
|
4112
|
-
if (
|
|
4070
|
+
if (!opts.out) opts.out = { inline: 1 };
|
|
4071
|
+
if (opts.verbose !== false) opts.verbose = true;
|
|
4113
4072
|
|
|
4114
|
-
|
|
4115
|
-
|
|
4073
|
+
opts.map = String(opts.map);
|
|
4074
|
+
opts.reduce = String(opts.reduce);
|
|
4116
4075
|
|
|
4117
|
-
if (
|
|
4118
|
-
let q = new this.Query(
|
|
4076
|
+
if (opts.query) {
|
|
4077
|
+
let q = new this.Query(opts.query);
|
|
4119
4078
|
q.cast(this);
|
|
4120
|
-
|
|
4079
|
+
opts.query = q._conditions;
|
|
4121
4080
|
q = undefined;
|
|
4122
4081
|
}
|
|
4123
4082
|
|
|
4124
|
-
this.$__collection.mapReduce(null, null,
|
|
4083
|
+
this.$__collection.mapReduce(null, null, opts, (err, res) => {
|
|
4125
4084
|
if (err) {
|
|
4126
4085
|
return cb(err);
|
|
4127
4086
|
}
|
|
@@ -26,7 +26,7 @@ const opts = require('./propertyOptions');
|
|
|
26
26
|
* @api public
|
|
27
27
|
* @property enum
|
|
28
28
|
* @memberOf SchemaArrayOptions
|
|
29
|
-
* @type Array
|
|
29
|
+
* @type {Array}
|
|
30
30
|
* @instance
|
|
31
31
|
*/
|
|
32
32
|
|
|
@@ -46,7 +46,7 @@ Object.defineProperty(SchemaArrayOptions.prototype, 'enum', opts);
|
|
|
46
46
|
* @api public
|
|
47
47
|
* @property of
|
|
48
48
|
* @memberOf SchemaArrayOptions
|
|
49
|
-
* @type Function|String
|
|
49
|
+
* @type {Function|String}
|
|
50
50
|
* @instance
|
|
51
51
|
*/
|
|
52
52
|
|