mongoose 6.2.8 → 6.2.11
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/.eslintrc.json +35 -26
- package/CHANGELOG.md +28 -0
- package/dist/browser.umd.js +38 -27
- package/lib/aggregate.js +57 -66
- package/lib/browser.js +4 -4
- package/lib/connection.js +21 -21
- package/lib/cursor/AggregationCursor.js +2 -2
- package/lib/cursor/QueryCursor.js +3 -3
- package/lib/document.js +65 -49
- package/lib/error/index.js +2 -2
- package/lib/helpers/indexes/applySchemaCollation.js +13 -0
- package/lib/helpers/indexes/isTextIndex.js +16 -0
- package/lib/helpers/populate/markArraySubdocsPopulated.js +1 -1
- package/lib/helpers/projection/hasIncludedChildren.js +1 -1
- package/lib/helpers/query/castUpdate.js +3 -1
- package/lib/helpers/update/applyTimestampsToChildren.js +2 -2
- package/lib/helpers/update/applyTimestampsToUpdate.js +0 -1
- package/lib/index.js +24 -26
- package/lib/model.js +144 -146
- package/lib/options/SchemaArrayOptions.js +2 -2
- package/lib/options/SchemaBufferOptions.js +1 -1
- package/lib/options/SchemaDateOptions.js +9 -2
- package/lib/options/SchemaDocumentArrayOptions.js +3 -3
- package/lib/options/SchemaMapOptions.js +2 -2
- package/lib/options/SchemaNumberOptions.js +3 -3
- package/lib/options/SchemaObjectIdOptions.js +2 -2
- package/lib/options/SchemaStringOptions.js +1 -1
- package/lib/options/SchemaSubdocumentOptions.js +2 -2
- package/lib/options/SchemaTypeOptions.js +3 -3
- package/lib/query.js +291 -248
- package/lib/schema/SubdocumentPath.js +4 -3
- package/lib/schema/array.js +2 -2
- package/lib/schema/boolean.js +4 -4
- package/lib/schema/buffer.js +3 -3
- package/lib/schema/date.js +7 -7
- package/lib/schema/decimal128.js +2 -2
- package/lib/schema/documentarray.js +3 -3
- package/lib/schema/mixed.js +2 -2
- package/lib/schema/number.js +6 -6
- package/lib/schema/objectid.js +4 -7
- package/lib/schema/string.js +38 -16
- package/lib/schema.js +30 -29
- package/lib/schematype.js +78 -69
- package/lib/types/ArraySubdocument.js +1 -1
- package/lib/types/DocumentArray/methods/index.js +2 -2
- package/lib/types/array/index.js +1 -1
- package/lib/types/array/methods/index.js +13 -13
- package/lib/types/buffer.js +1 -1
- package/lib/types/decimal128.js +1 -1
- package/lib/types/objectid.js +1 -1
- package/lib/types/subdocument.js +31 -2
- package/lib/virtualtype.js +3 -3
- package/package.json +18 -16
- package/tools/repl.js +8 -8
- package/tools/sharded.js +3 -3
- package/types/aggregate.d.ts +223 -0
- package/types/connection.d.ts +116 -116
- package/types/error.d.ts +2 -2
- package/types/index.d.ts +76 -213
- package/types/pipelinestage.d.ts +194 -194
- package/types/schemaoptions.d.ts +2 -2
package/lib/query.js
CHANGED
|
@@ -11,6 +11,7 @@ const MongooseError = require('./error/mongooseError');
|
|
|
11
11
|
const ObjectParameterError = require('./error/objectParameter');
|
|
12
12
|
const QueryCursor = require('./cursor/QueryCursor');
|
|
13
13
|
const ReadPreference = require('./driver').get().ReadPreference;
|
|
14
|
+
const ValidationError = require('./error/validation');
|
|
14
15
|
const applyGlobalMaxTimeMS = require('./helpers/query/applyGlobalMaxTimeMS');
|
|
15
16
|
const applyWriteConcern = require('./helpers/schema/applyWriteConcern');
|
|
16
17
|
const cast = require('./cast');
|
|
@@ -39,12 +40,37 @@ const utils = require('./utils');
|
|
|
39
40
|
const validOps = require('./helpers/query/validOps');
|
|
40
41
|
const wrapThunk = require('./helpers/query/wrapThunk');
|
|
41
42
|
|
|
43
|
+
const queryOptionMethods = new Set([
|
|
44
|
+
'allowDiskUse',
|
|
45
|
+
'batchSize',
|
|
46
|
+
'collation',
|
|
47
|
+
'comment',
|
|
48
|
+
'explain',
|
|
49
|
+
'hint',
|
|
50
|
+
'j',
|
|
51
|
+
'lean',
|
|
52
|
+
'limit',
|
|
53
|
+
'maxScan',
|
|
54
|
+
'maxTimeMS',
|
|
55
|
+
'maxscan',
|
|
56
|
+
'projection',
|
|
57
|
+
'read',
|
|
58
|
+
'select',
|
|
59
|
+
'skip',
|
|
60
|
+
'slice',
|
|
61
|
+
'sort',
|
|
62
|
+
'tailable',
|
|
63
|
+
'w',
|
|
64
|
+
'writeConcern',
|
|
65
|
+
'wtimeout'
|
|
66
|
+
]);
|
|
67
|
+
|
|
42
68
|
/**
|
|
43
69
|
* Query constructor used for building queries. You do not need
|
|
44
70
|
* to instantiate a `Query` directly. Instead use Model functions like
|
|
45
71
|
* [`Model.find()`](/docs/api.html#find_find).
|
|
46
72
|
*
|
|
47
|
-
* ####Example:
|
|
73
|
+
* #### Example:
|
|
48
74
|
*
|
|
49
75
|
* const query = MyModel.find(); // `query` is an instance of `Query`
|
|
50
76
|
* query.setOptions({ lean : true });
|
|
@@ -129,7 +155,9 @@ Query.base = mquery.prototype;
|
|
|
129
155
|
/**
|
|
130
156
|
* Flag to opt out of using `$geoWithin`.
|
|
131
157
|
*
|
|
132
|
-
*
|
|
158
|
+
* ```javascript
|
|
159
|
+
* mongoose.Query.use$geoWithin = false;
|
|
160
|
+
* ```
|
|
133
161
|
*
|
|
134
162
|
* MongoDB 2.4 deprecated the use of `$within`, replacing it with `$geoWithin`. Mongoose uses `$geoWithin` by default (which is 100% backward compatible with `$within`). If you are running an older version of MongoDB, set this flag to `false` so your `within()` queries continue to work.
|
|
135
163
|
*
|
|
@@ -146,7 +174,7 @@ Query.use$geoWithin = mquery.use$geoWithin;
|
|
|
146
174
|
/**
|
|
147
175
|
* Converts this query to a customized, reusable query constructor with all arguments and options retained.
|
|
148
176
|
*
|
|
149
|
-
* ####Example
|
|
177
|
+
* #### Example
|
|
150
178
|
*
|
|
151
179
|
* // Create a query for adventure movies and read from the primary
|
|
152
180
|
* // node in the replica-set unless it is down, in which case we'll
|
|
@@ -228,7 +256,7 @@ Query.prototype.toConstructor = function toConstructor() {
|
|
|
228
256
|
/**
|
|
229
257
|
* Make a copy of this query so you can re-execute it.
|
|
230
258
|
*
|
|
231
|
-
* ####Example:
|
|
259
|
+
* #### Example:
|
|
232
260
|
* const q = Book.findOne({ title: 'Casino Royale' });
|
|
233
261
|
* await q.exec();
|
|
234
262
|
* await q.exec(); // Throws an error because you can't execute a query twice
|
|
@@ -276,7 +304,7 @@ Query.prototype.clone = function clone() {
|
|
|
276
304
|
/**
|
|
277
305
|
* Specifies a javascript function or expression to pass to MongoDBs query system.
|
|
278
306
|
*
|
|
279
|
-
* ####Example
|
|
307
|
+
* #### Example
|
|
280
308
|
*
|
|
281
309
|
* query.$where('this.comments.length === 10 || this.name.length === 5')
|
|
282
310
|
*
|
|
@@ -286,7 +314,7 @@ Query.prototype.clone = function clone() {
|
|
|
286
314
|
* return this.comments.length === 10 || this.name.length === 5;
|
|
287
315
|
* })
|
|
288
316
|
*
|
|
289
|
-
* ####
|
|
317
|
+
* #### Note:
|
|
290
318
|
*
|
|
291
319
|
* Only use `$where` when you have a condition that cannot be met using other MongoDB operators like `$lt`.
|
|
292
320
|
* **Be sure to read about all of [its caveats](https://docs.mongodb.org/manual/reference/operator/where/) before using.**
|
|
@@ -304,7 +332,7 @@ Query.prototype.clone = function clone() {
|
|
|
304
332
|
/**
|
|
305
333
|
* Specifies a `path` for use with chaining.
|
|
306
334
|
*
|
|
307
|
-
* ####Example
|
|
335
|
+
* #### Example
|
|
308
336
|
*
|
|
309
337
|
* // instead of writing:
|
|
310
338
|
* User.find({age: {$gte: 21, $lte: 65}}, callback);
|
|
@@ -334,13 +362,13 @@ Query.prototype.clone = function clone() {
|
|
|
334
362
|
/**
|
|
335
363
|
* Specifies a `$slice` projection for an array.
|
|
336
364
|
*
|
|
337
|
-
* ####Example
|
|
365
|
+
* #### Example
|
|
338
366
|
*
|
|
339
|
-
* query.slice('comments', 5)
|
|
340
|
-
* query.slice('comments', -5)
|
|
341
|
-
* query.slice('comments', [10, 5])
|
|
342
|
-
* query.where('comments').slice(5)
|
|
343
|
-
* query.where('comments').slice([-10, 5])
|
|
367
|
+
* query.slice('comments', 5);
|
|
368
|
+
* query.slice('comments', -5);
|
|
369
|
+
* query.slice('comments', [10, 5]);
|
|
370
|
+
* query.where('comments').slice(5);
|
|
371
|
+
* query.where('comments').slice([-10, 5]);
|
|
344
372
|
*
|
|
345
373
|
* @method slice
|
|
346
374
|
* @memberOf Query
|
|
@@ -412,7 +440,7 @@ Query.prototype._validateOp = function() {
|
|
|
412
440
|
/**
|
|
413
441
|
* Specifies the complementary comparison value for paths specified with `where()`
|
|
414
442
|
*
|
|
415
|
-
* ####Example
|
|
443
|
+
* #### Example
|
|
416
444
|
*
|
|
417
445
|
* User.where('age').equals(49);
|
|
418
446
|
*
|
|
@@ -431,9 +459,9 @@ Query.prototype._validateOp = function() {
|
|
|
431
459
|
/**
|
|
432
460
|
* Specifies arguments for an `$or` condition.
|
|
433
461
|
*
|
|
434
|
-
* ####Example
|
|
462
|
+
* #### Example
|
|
435
463
|
*
|
|
436
|
-
* query.or([{ color: 'red' }, { status: 'emergency' }])
|
|
464
|
+
* query.or([{ color: 'red' }, { status: 'emergency' }]);
|
|
437
465
|
*
|
|
438
466
|
* @see $or https://docs.mongodb.org/manual/reference/operator/or/
|
|
439
467
|
* @method or
|
|
@@ -447,9 +475,9 @@ Query.prototype._validateOp = function() {
|
|
|
447
475
|
/**
|
|
448
476
|
* Specifies arguments for a `$nor` condition.
|
|
449
477
|
*
|
|
450
|
-
* ####Example
|
|
478
|
+
* #### Example
|
|
451
479
|
*
|
|
452
|
-
* query.nor([{ color: 'green' }, { status: 'ok' }])
|
|
480
|
+
* query.nor([{ color: 'green' }, { status: 'ok' }]);
|
|
453
481
|
*
|
|
454
482
|
* @see $nor https://docs.mongodb.org/manual/reference/operator/nor/
|
|
455
483
|
* @method nor
|
|
@@ -463,7 +491,7 @@ Query.prototype._validateOp = function() {
|
|
|
463
491
|
/**
|
|
464
492
|
* Specifies arguments for a `$and` condition.
|
|
465
493
|
*
|
|
466
|
-
* ####Example
|
|
494
|
+
* #### Example
|
|
467
495
|
*
|
|
468
496
|
* query.and([{ color: 'green' }, { status: 'ok' }])
|
|
469
497
|
*
|
|
@@ -481,12 +509,12 @@ Query.prototype._validateOp = function() {
|
|
|
481
509
|
*
|
|
482
510
|
* When called with one argument, the most recent path passed to `where()` is used.
|
|
483
511
|
*
|
|
484
|
-
* ####Example
|
|
512
|
+
* #### Example
|
|
485
513
|
*
|
|
486
|
-
* Thing.find().where('age').gt(21)
|
|
514
|
+
* Thing.find().where('age').gt(21);
|
|
487
515
|
*
|
|
488
516
|
* // or
|
|
489
|
-
* Thing.find().gt('age', 21)
|
|
517
|
+
* Thing.find().gt('age', 21);
|
|
490
518
|
*
|
|
491
519
|
* @method gt
|
|
492
520
|
* @memberOf Query
|
|
@@ -586,7 +614,7 @@ Query.prototype._validateOp = function() {
|
|
|
586
614
|
*
|
|
587
615
|
* When called with one argument, the most recent path passed to `where()` is used.
|
|
588
616
|
*
|
|
589
|
-
* ####Example:
|
|
617
|
+
* #### Example:
|
|
590
618
|
*
|
|
591
619
|
* MyModel.find().where('pets').all(['dog', 'cat', 'ferret']);
|
|
592
620
|
* // Equivalent:
|
|
@@ -606,7 +634,7 @@ Query.prototype._validateOp = function() {
|
|
|
606
634
|
*
|
|
607
635
|
* When called with one argument, the most recent path passed to `where()` is used.
|
|
608
636
|
*
|
|
609
|
-
* ####Example
|
|
637
|
+
* #### Example
|
|
610
638
|
*
|
|
611
639
|
* const docs = await MyModel.where('tags').size(0).exec();
|
|
612
640
|
* assert(Array.isArray(docs));
|
|
@@ -653,7 +681,7 @@ Query.prototype._validateOp = function() {
|
|
|
653
681
|
* Specifies a `$mod` condition, filters documents for documents whose
|
|
654
682
|
* `path` property is a number that is equal to `remainder` modulo `divisor`.
|
|
655
683
|
*
|
|
656
|
-
* ####Example
|
|
684
|
+
* #### Example
|
|
657
685
|
*
|
|
658
686
|
* // All find products whose inventory is odd
|
|
659
687
|
* Product.find().mod('inventory', [2, 1]);
|
|
@@ -699,7 +727,7 @@ Query.prototype.mod = function() {
|
|
|
699
727
|
/**
|
|
700
728
|
* Specifies an `$exists` condition
|
|
701
729
|
*
|
|
702
|
-
* ####Example
|
|
730
|
+
* #### Example
|
|
703
731
|
*
|
|
704
732
|
* // { name: { $exists: true }}
|
|
705
733
|
* Thing.where('name').exists()
|
|
@@ -723,7 +751,7 @@ Query.prototype.mod = function() {
|
|
|
723
751
|
/**
|
|
724
752
|
* Specifies an `$elemMatch` condition
|
|
725
753
|
*
|
|
726
|
-
* ####Example
|
|
754
|
+
* #### Example
|
|
727
755
|
*
|
|
728
756
|
* query.elemMatch('comment', { author: 'autobot', votes: {$gte: 5}})
|
|
729
757
|
*
|
|
@@ -752,7 +780,7 @@ Query.prototype.mod = function() {
|
|
|
752
780
|
/**
|
|
753
781
|
* Defines a `$within` or `$geoWithin` argument for geo-spatial queries.
|
|
754
782
|
*
|
|
755
|
-
* ####Example
|
|
783
|
+
* #### Example
|
|
756
784
|
*
|
|
757
785
|
* query.where(path).within().box()
|
|
758
786
|
* query.where(path).within().circle()
|
|
@@ -768,11 +796,11 @@ Query.prototype.mod = function() {
|
|
|
768
796
|
*
|
|
769
797
|
* **MUST** be used after `where()`.
|
|
770
798
|
*
|
|
771
|
-
* ####
|
|
799
|
+
* #### Note:
|
|
772
800
|
*
|
|
773
801
|
* As of Mongoose 3.7, `$geoWithin` is always used for queries. To change this behavior, see [Query.use$geoWithin](#query_Query-use%2524geoWithin).
|
|
774
802
|
*
|
|
775
|
-
* ####
|
|
803
|
+
* #### Note:
|
|
776
804
|
*
|
|
777
805
|
* In Mongoose 3.7, `within` changed from a getter to a function. If you need the old syntax, use [this](https://github.com/ebensing/mongoose-within).
|
|
778
806
|
*
|
|
@@ -791,11 +819,11 @@ Query.prototype.mod = function() {
|
|
|
791
819
|
/**
|
|
792
820
|
* Specifies the maximum number of documents the query will return.
|
|
793
821
|
*
|
|
794
|
-
* ####Example
|
|
822
|
+
* #### Example
|
|
795
823
|
*
|
|
796
|
-
* query.limit(20)
|
|
824
|
+
* query.limit(20);
|
|
797
825
|
*
|
|
798
|
-
* ####Note
|
|
826
|
+
* #### Note
|
|
799
827
|
*
|
|
800
828
|
* Cannot be used with `distinct()`
|
|
801
829
|
*
|
|
@@ -824,11 +852,11 @@ Query.prototype.limit = function limit(v) {
|
|
|
824
852
|
/**
|
|
825
853
|
* Specifies the number of documents to skip.
|
|
826
854
|
*
|
|
827
|
-
* ####Example
|
|
855
|
+
* #### Example
|
|
828
856
|
*
|
|
829
|
-
* query.skip(100).limit(20)
|
|
857
|
+
* query.skip(100).limit(20);
|
|
830
858
|
*
|
|
831
|
-
* ####Note
|
|
859
|
+
* #### Note
|
|
832
860
|
*
|
|
833
861
|
* Cannot be used with `distinct()`
|
|
834
862
|
*
|
|
@@ -858,11 +886,11 @@ Query.prototype.skip = function skip(v) {
|
|
|
858
886
|
/**
|
|
859
887
|
* Specifies the maxScan option.
|
|
860
888
|
*
|
|
861
|
-
* ####Example
|
|
889
|
+
* #### Example
|
|
862
890
|
*
|
|
863
|
-
* query.maxScan(100)
|
|
891
|
+
* query.maxScan(100);
|
|
864
892
|
*
|
|
865
|
-
* ####Note
|
|
893
|
+
* #### Note
|
|
866
894
|
*
|
|
867
895
|
* Cannot be used with `distinct()`
|
|
868
896
|
*
|
|
@@ -877,11 +905,11 @@ Query.prototype.skip = function skip(v) {
|
|
|
877
905
|
/**
|
|
878
906
|
* Specifies the batchSize option.
|
|
879
907
|
*
|
|
880
|
-
* ####Example
|
|
908
|
+
* #### Example
|
|
881
909
|
*
|
|
882
910
|
* query.batchSize(100)
|
|
883
911
|
*
|
|
884
|
-
* ####Note
|
|
912
|
+
* #### Note
|
|
885
913
|
*
|
|
886
914
|
* Cannot be used with `distinct()`
|
|
887
915
|
*
|
|
@@ -896,11 +924,11 @@ Query.prototype.skip = function skip(v) {
|
|
|
896
924
|
/**
|
|
897
925
|
* Specifies the `comment` option.
|
|
898
926
|
*
|
|
899
|
-
* ####Example
|
|
927
|
+
* #### Example
|
|
900
928
|
*
|
|
901
929
|
* query.comment('login query')
|
|
902
930
|
*
|
|
903
|
-
* ####Note
|
|
931
|
+
* #### Note
|
|
904
932
|
*
|
|
905
933
|
* Cannot be used with `distinct()`
|
|
906
934
|
*
|
|
@@ -915,13 +943,13 @@ Query.prototype.skip = function skip(v) {
|
|
|
915
943
|
/**
|
|
916
944
|
* Specifies this query as a `snapshot` query.
|
|
917
945
|
*
|
|
918
|
-
* ####Example
|
|
946
|
+
* #### Example
|
|
919
947
|
*
|
|
920
|
-
* query.snapshot() // true
|
|
921
|
-
* query.snapshot(true)
|
|
922
|
-
* query.snapshot(false)
|
|
948
|
+
* query.snapshot(); // true
|
|
949
|
+
* query.snapshot(true);
|
|
950
|
+
* query.snapshot(false);
|
|
923
951
|
*
|
|
924
|
-
* ####Note
|
|
952
|
+
* #### Note
|
|
925
953
|
*
|
|
926
954
|
* Cannot be used with `distinct()`
|
|
927
955
|
*
|
|
@@ -936,11 +964,11 @@ Query.prototype.skip = function skip(v) {
|
|
|
936
964
|
/**
|
|
937
965
|
* Sets query hints.
|
|
938
966
|
*
|
|
939
|
-
* ####Example
|
|
967
|
+
* #### Example
|
|
940
968
|
*
|
|
941
|
-
* query.hint({ indexA: 1, indexB: -1})
|
|
969
|
+
* query.hint({ indexA: 1, indexB: -1 });
|
|
942
970
|
*
|
|
943
|
-
* ####Note
|
|
971
|
+
* #### Note
|
|
944
972
|
*
|
|
945
973
|
* Cannot be used with `distinct()`
|
|
946
974
|
*
|
|
@@ -960,7 +988,7 @@ Query.prototype.skip = function skip(v) {
|
|
|
960
988
|
* Unlike `projection()`, the `select()` function modifies the current
|
|
961
989
|
* projection in place. This function overwrites the existing projection.
|
|
962
990
|
*
|
|
963
|
-
* ####Example:
|
|
991
|
+
* #### Example:
|
|
964
992
|
*
|
|
965
993
|
* const q = Model.find();
|
|
966
994
|
* q.projection(); // null
|
|
@@ -1003,7 +1031,7 @@ Query.prototype.projection = function(arg) {
|
|
|
1003
1031
|
* either list the fields to include (which excludes all others), or list the fields
|
|
1004
1032
|
* to exclude (which implies all other fields are included). The [`_id` field is the only exception because MongoDB includes it by default](https://docs.mongodb.com/manual/tutorial/project-fields-from-query-results/#suppress-id-field).
|
|
1005
1033
|
*
|
|
1006
|
-
* ####Example
|
|
1034
|
+
* #### Example
|
|
1007
1035
|
*
|
|
1008
1036
|
* // include a and b, exclude other fields
|
|
1009
1037
|
* query.select('a b');
|
|
@@ -1114,49 +1142,30 @@ Query.prototype.select = function select() {
|
|
|
1114
1142
|
throw new TypeError('Invalid select() argument. Must be string or object.');
|
|
1115
1143
|
};
|
|
1116
1144
|
|
|
1117
|
-
/**
|
|
1118
|
-
* _DEPRECATED_ Sets the slaveOk option.
|
|
1119
|
-
*
|
|
1120
|
-
* **Deprecated** in MongoDB 2.2 in favor of [read preferences](#query_Query-read).
|
|
1121
|
-
*
|
|
1122
|
-
* ####Example:
|
|
1123
|
-
*
|
|
1124
|
-
* query.slaveOk() // true
|
|
1125
|
-
* query.slaveOk(true)
|
|
1126
|
-
* query.slaveOk(false)
|
|
1127
|
-
*
|
|
1128
|
-
* @method slaveOk
|
|
1129
|
-
* @memberOf Query
|
|
1130
|
-
* @instance
|
|
1131
|
-
* @deprecated use read() preferences instead if on mongodb >= 2.2
|
|
1132
|
-
* @param {Boolean} v defaults to true
|
|
1133
|
-
* @see mongodb https://docs.mongodb.org/manual/applications/replication/#read-preference
|
|
1134
|
-
* @see slaveOk https://docs.mongodb.org/manual/reference/method/rs.slaveOk/
|
|
1135
|
-
* @see read() #query_Query-read
|
|
1136
|
-
* @return {Query} this
|
|
1137
|
-
* @api public
|
|
1138
|
-
*/
|
|
1139
|
-
|
|
1140
1145
|
/**
|
|
1141
1146
|
* Determines the MongoDB nodes from which to read.
|
|
1142
1147
|
*
|
|
1143
|
-
* ####Preferences:
|
|
1148
|
+
* #### Preferences:
|
|
1144
1149
|
*
|
|
1145
|
-
*
|
|
1146
|
-
*
|
|
1147
|
-
*
|
|
1148
|
-
*
|
|
1149
|
-
*
|
|
1150
|
+
* ```
|
|
1151
|
+
* primary - (default) Read from primary only. Operations will produce an error if primary is unavailable. Cannot be combined with tags.
|
|
1152
|
+
* secondary Read from secondary if available, otherwise error.
|
|
1153
|
+
* primaryPreferred Read from primary if available, otherwise a secondary.
|
|
1154
|
+
* secondaryPreferred Read from a secondary if available, otherwise read from the primary.
|
|
1155
|
+
* nearest All operations read from among the nearest candidates, but unlike other modes, this option will include both the primary and all secondaries in the random selection.
|
|
1156
|
+
* ```
|
|
1150
1157
|
*
|
|
1151
1158
|
* Aliases
|
|
1152
1159
|
*
|
|
1153
|
-
*
|
|
1154
|
-
*
|
|
1155
|
-
*
|
|
1156
|
-
*
|
|
1157
|
-
*
|
|
1160
|
+
* ```
|
|
1161
|
+
* p primary
|
|
1162
|
+
* pp primaryPreferred
|
|
1163
|
+
* s secondary
|
|
1164
|
+
* sp secondaryPreferred
|
|
1165
|
+
* n nearest
|
|
1166
|
+
* ```
|
|
1158
1167
|
*
|
|
1159
|
-
* ####Example:
|
|
1168
|
+
* #### Example:
|
|
1160
1169
|
*
|
|
1161
1170
|
* new Query().read('primary')
|
|
1162
1171
|
* new Query().read('p') // same as primary
|
|
@@ -1235,7 +1244,7 @@ Query.prototype.toString = function toString() {
|
|
|
1235
1244
|
*
|
|
1236
1245
|
* Calling `session(null)` removes the session from this query.
|
|
1237
1246
|
*
|
|
1238
|
-
* ####Example:
|
|
1247
|
+
* #### Example:
|
|
1239
1248
|
*
|
|
1240
1249
|
* const s = await mongoose.startSession();
|
|
1241
1250
|
* await mongoose.model('Person').findOne({ name: 'Axl Rose' }).session(s);
|
|
@@ -1279,7 +1288,7 @@ Query.prototype.session = function session(v) {
|
|
|
1279
1288
|
*
|
|
1280
1289
|
* Defaults to the schema's [`writeConcern` option](/docs/guide.html#writeConcern)
|
|
1281
1290
|
*
|
|
1282
|
-
* ####Example:
|
|
1291
|
+
* #### Example:
|
|
1283
1292
|
*
|
|
1284
1293
|
* // The 'majority' option means the `deleteOne()` promise won't resolve
|
|
1285
1294
|
* // until the `deleteOne()` has propagated to the majority of the replica set
|
|
@@ -1322,7 +1331,7 @@ Query.prototype.writeConcern = function writeConcern(val) {
|
|
|
1322
1331
|
*
|
|
1323
1332
|
* Defaults to the schema's [`writeConcern.w` option](/docs/guide.html#writeConcern)
|
|
1324
1333
|
*
|
|
1325
|
-
* ####Example:
|
|
1334
|
+
* #### Example:
|
|
1326
1335
|
*
|
|
1327
1336
|
* // The 'majority' option means the `deleteOne()` promise won't resolve
|
|
1328
1337
|
* // until the `deleteOne()` has propagated to the majority of the replica set
|
|
@@ -1368,7 +1377,7 @@ Query.prototype.w = function w(val) {
|
|
|
1368
1377
|
*
|
|
1369
1378
|
* Defaults to the schema's [`writeConcern.j` option](/docs/guide.html#writeConcern)
|
|
1370
1379
|
*
|
|
1371
|
-
* ####Example:
|
|
1380
|
+
* #### Example:
|
|
1372
1381
|
*
|
|
1373
1382
|
* await mongoose.model('Person').deleteOne({ name: 'Ned Stark' }).j(true);
|
|
1374
1383
|
*
|
|
@@ -1412,7 +1421,7 @@ Query.prototype.j = function j(val) {
|
|
|
1412
1421
|
*
|
|
1413
1422
|
* Defaults to the schema's [`writeConcern.wtimeout` option](/docs/guide.html#writeConcern)
|
|
1414
1423
|
*
|
|
1415
|
-
* ####Example:
|
|
1424
|
+
* #### Example:
|
|
1416
1425
|
*
|
|
1417
1426
|
* // The `deleteOne()` promise won't resolve until this `deleteOne()` has
|
|
1418
1427
|
* // propagated to at least `w = 2` members of the replica set. If it takes
|
|
@@ -1446,7 +1455,7 @@ Query.prototype.wtimeout = function wtimeout(ms) {
|
|
|
1446
1455
|
/**
|
|
1447
1456
|
* Sets the readConcern option for the query.
|
|
1448
1457
|
*
|
|
1449
|
-
* ####Example:
|
|
1458
|
+
* #### Example:
|
|
1450
1459
|
*
|
|
1451
1460
|
* new Query().readConcern('local')
|
|
1452
1461
|
* new Query().readConcern('l') // same as local
|
|
@@ -1464,21 +1473,25 @@ Query.prototype.wtimeout = function wtimeout(ms) {
|
|
|
1464
1473
|
* new Query().readConcern('s') // same as snapshot
|
|
1465
1474
|
*
|
|
1466
1475
|
*
|
|
1467
|
-
* ####Read Concern Level:
|
|
1476
|
+
* #### Read Concern Level:
|
|
1468
1477
|
*
|
|
1469
|
-
*
|
|
1470
|
-
*
|
|
1471
|
-
*
|
|
1472
|
-
*
|
|
1473
|
-
*
|
|
1478
|
+
* ```
|
|
1479
|
+
* local MongoDB 3.2+ The query returns from the instance with no guarantee guarantee that the data has been written to a majority of the replica set members (i.e. may be rolled back).
|
|
1480
|
+
* available MongoDB 3.6+ The query returns from the instance with no guarantee guarantee that the data has been written to a majority of the replica set members (i.e. may be rolled back).
|
|
1481
|
+
* majority MongoDB 3.2+ The query returns the data that has been acknowledged by a majority of the replica set members. The documents returned by the read operation are durable, even in the event of failure.
|
|
1482
|
+
* linearizable MongoDB 3.4+ The query returns data that reflects all successful majority-acknowledged writes that completed prior to the start of the read operation. The query may wait for concurrently executing writes to propagate to a majority of replica set members before returning results.
|
|
1483
|
+
* snapshot MongoDB 4.0+ Only available for operations within multi-document transactions. Upon transaction commit with write concern "majority", the transaction operations are guaranteed to have read from a snapshot of majority-committed data.
|
|
1484
|
+
* ```
|
|
1474
1485
|
*
|
|
1475
1486
|
* Aliases
|
|
1476
1487
|
*
|
|
1477
|
-
*
|
|
1478
|
-
*
|
|
1479
|
-
*
|
|
1480
|
-
*
|
|
1481
|
-
*
|
|
1488
|
+
* ```
|
|
1489
|
+
* l local
|
|
1490
|
+
* a available
|
|
1491
|
+
* m majority
|
|
1492
|
+
* lz linearizable
|
|
1493
|
+
* s snapshot
|
|
1494
|
+
* ```
|
|
1482
1495
|
*
|
|
1483
1496
|
* Read more about how to use read concern [here](https://docs.mongodb.com/manual/reference/read-concern/).
|
|
1484
1497
|
*
|
|
@@ -1493,11 +1506,11 @@ Query.prototype.wtimeout = function wtimeout(ms) {
|
|
|
1493
1506
|
/**
|
|
1494
1507
|
* Gets query options.
|
|
1495
1508
|
*
|
|
1496
|
-
* ####Example:
|
|
1509
|
+
* #### Example:
|
|
1497
1510
|
*
|
|
1498
1511
|
* const query = new Query();
|
|
1499
1512
|
* query.limit(10);
|
|
1500
|
-
* query.setOptions({ maxTimeMS: 1000 })
|
|
1513
|
+
* query.setOptions({ maxTimeMS: 1000 });
|
|
1501
1514
|
* query.getOptions(); // { limit: 10, maxTimeMS: 1000 }
|
|
1502
1515
|
*
|
|
1503
1516
|
* @return {Object} the options
|
|
@@ -1511,7 +1524,7 @@ Query.prototype.getOptions = function() {
|
|
|
1511
1524
|
/**
|
|
1512
1525
|
* Sets query options. Some options only make sense for certain operations.
|
|
1513
1526
|
*
|
|
1514
|
-
* ####Options:
|
|
1527
|
+
* #### Options:
|
|
1515
1528
|
*
|
|
1516
1529
|
* The following options are only for `find()`:
|
|
1517
1530
|
*
|
|
@@ -1630,7 +1643,19 @@ Query.prototype.setOptions = function(options, overwrite) {
|
|
|
1630
1643
|
}
|
|
1631
1644
|
}
|
|
1632
1645
|
|
|
1633
|
-
|
|
1646
|
+
// set arbitrary options
|
|
1647
|
+
for (const key of Object.keys(options)) {
|
|
1648
|
+
if (queryOptionMethods.has(key)) {
|
|
1649
|
+
const args = Array.isArray(options[key]) ?
|
|
1650
|
+
options[key] :
|
|
1651
|
+
[options[key]];
|
|
1652
|
+
this[key].apply(this, args);
|
|
1653
|
+
} else {
|
|
1654
|
+
this.options[key] = options[key];
|
|
1655
|
+
}
|
|
1656
|
+
}
|
|
1657
|
+
|
|
1658
|
+
return this;
|
|
1634
1659
|
};
|
|
1635
1660
|
|
|
1636
1661
|
/**
|
|
@@ -1641,7 +1666,7 @@ Query.prototype.setOptions = function(options, overwrite) {
|
|
|
1641
1666
|
*
|
|
1642
1667
|
* Calling `query.explain(v)` is equivalent to `query.setOptions({ explain: v })`
|
|
1643
1668
|
*
|
|
1644
|
-
* ####Example:
|
|
1669
|
+
* #### Example:
|
|
1645
1670
|
*
|
|
1646
1671
|
* const query = new Query();
|
|
1647
1672
|
* const res = await query.find({ a: 1 }).explain('queryPlanner');
|
|
@@ -1673,7 +1698,7 @@ Query.prototype.explain = function(verbose) {
|
|
|
1673
1698
|
*
|
|
1674
1699
|
* Calling `query.allowDiskUse(v)` is equivalent to `query.setOptions({ allowDiskUse: v })`
|
|
1675
1700
|
*
|
|
1676
|
-
* ####Example:
|
|
1701
|
+
* #### Example:
|
|
1677
1702
|
*
|
|
1678
1703
|
* await query.find().sort({ name: 1 }).allowDiskUse(true);
|
|
1679
1704
|
* // Equivalent:
|
|
@@ -1702,7 +1727,7 @@ Query.prototype.allowDiskUse = function(v) {
|
|
|
1702
1727
|
*
|
|
1703
1728
|
* Calling `query.maxTimeMS(v)` is equivalent to `query.setOptions({ maxTimeMS: v })`
|
|
1704
1729
|
*
|
|
1705
|
-
* ####Example:
|
|
1730
|
+
* #### Example:
|
|
1706
1731
|
*
|
|
1707
1732
|
* const query = new Query();
|
|
1708
1733
|
* // Throws an error 'operation exceeded time limit' as long as there's
|
|
@@ -1722,7 +1747,7 @@ Query.prototype.maxTimeMS = function(ms) {
|
|
|
1722
1747
|
/**
|
|
1723
1748
|
* Returns the current query filter (also known as conditions) as a [POJO](https://masteringjs.io/tutorials/fundamentals/pojo).
|
|
1724
1749
|
*
|
|
1725
|
-
* ####Example:
|
|
1750
|
+
* #### Example:
|
|
1726
1751
|
*
|
|
1727
1752
|
* const query = new Query();
|
|
1728
1753
|
* query.find({ a: 1 }).where('b').gt(2);
|
|
@@ -1742,7 +1767,7 @@ Query.prototype.getFilter = function() {
|
|
|
1742
1767
|
* You should use `getFilter()` instead of `getQuery()` where possible. `getQuery()`
|
|
1743
1768
|
* will likely be deprecated in a future release.
|
|
1744
1769
|
*
|
|
1745
|
-
* ####Example:
|
|
1770
|
+
* #### Example:
|
|
1746
1771
|
*
|
|
1747
1772
|
* const query = new Query();
|
|
1748
1773
|
* query.find({ a: 1 }).where('b').gt(2);
|
|
@@ -1759,7 +1784,7 @@ Query.prototype.getQuery = function() {
|
|
|
1759
1784
|
/**
|
|
1760
1785
|
* Sets the query conditions to the provided JSON object.
|
|
1761
1786
|
*
|
|
1762
|
-
* ####Example:
|
|
1787
|
+
* #### Example:
|
|
1763
1788
|
*
|
|
1764
1789
|
* const query = new Query();
|
|
1765
1790
|
* query.find({ a: 1 })
|
|
@@ -1778,7 +1803,7 @@ Query.prototype.setQuery = function(val) {
|
|
|
1778
1803
|
/**
|
|
1779
1804
|
* Returns the current update operations as a JSON object.
|
|
1780
1805
|
*
|
|
1781
|
-
* ####Example:
|
|
1806
|
+
* #### Example:
|
|
1782
1807
|
*
|
|
1783
1808
|
* const query = new Query();
|
|
1784
1809
|
* query.update({}, { $set: { a: 5 } });
|
|
@@ -1795,7 +1820,7 @@ Query.prototype.getUpdate = function() {
|
|
|
1795
1820
|
/**
|
|
1796
1821
|
* Sets the current update operation to new value.
|
|
1797
1822
|
*
|
|
1798
|
-
* ####Example:
|
|
1823
|
+
* #### Example:
|
|
1799
1824
|
*
|
|
1800
1825
|
* const query = new Query();
|
|
1801
1826
|
* query.update({}, { $set: { a: 5 } });
|
|
@@ -1948,7 +1973,7 @@ Query.prototype._optionsForExec = function(model) {
|
|
|
1948
1973
|
* javascript objects, not [Mongoose Documents](/api/document.html). They have no
|
|
1949
1974
|
* `save` method, getters/setters, virtuals, or other Mongoose features.
|
|
1950
1975
|
*
|
|
1951
|
-
* ####Example:
|
|
1976
|
+
* #### Example:
|
|
1952
1977
|
*
|
|
1953
1978
|
* new Query().lean() // true
|
|
1954
1979
|
* new Query().lean(true)
|
|
@@ -1983,7 +2008,7 @@ Query.prototype.lean = function(v) {
|
|
|
1983
2008
|
* This is useful for query middleware so you can add an update regardless
|
|
1984
2009
|
* of whether you use `updateOne()`, `updateMany()`, `findOneAndUpdate()`, etc.
|
|
1985
2010
|
*
|
|
1986
|
-
* ####Example:
|
|
2011
|
+
* #### Example:
|
|
1987
2012
|
*
|
|
1988
2013
|
* // Updates `{ $set: { updatedAt: new Date() } }`
|
|
1989
2014
|
* new Query().updateOne({}, {}).set('updatedAt', new Date());
|
|
@@ -2015,7 +2040,7 @@ Query.prototype.set = function(path, val) {
|
|
|
2015
2040
|
* Useful for writing getters/setters that can work with both update operations
|
|
2016
2041
|
* and `save()`.
|
|
2017
2042
|
*
|
|
2018
|
-
* ####Example:
|
|
2043
|
+
* #### Example:
|
|
2019
2044
|
*
|
|
2020
2045
|
* const query = Model.updateOne({}, { $set: { name: 'Jean-Luc Picard' } });
|
|
2021
2046
|
* query.get('name'); // 'Jean-Luc Picard'
|
|
@@ -2049,7 +2074,7 @@ Query.prototype.get = function get(path) {
|
|
|
2049
2074
|
* Gets/sets the error flag on this query. If this flag is not null or
|
|
2050
2075
|
* undefined, the `exec()` promise will reject without executing.
|
|
2051
2076
|
*
|
|
2052
|
-
* ####Example:
|
|
2077
|
+
* #### Example:
|
|
2053
2078
|
*
|
|
2054
2079
|
* Query().error(); // Get current error value
|
|
2055
2080
|
* Query().error(null); // Unset the current error
|
|
@@ -2063,7 +2088,7 @@ Query.prototype.get = function get(path) {
|
|
|
2063
2088
|
* Note that query casting runs **after** hooks, so cast errors will override
|
|
2064
2089
|
* custom errors.
|
|
2065
2090
|
*
|
|
2066
|
-
* ####Example:
|
|
2091
|
+
* #### Example:
|
|
2067
2092
|
* const TestSchema = new Schema({ num: Number });
|
|
2068
2093
|
* const TestModel = db.model('Test', TestSchema);
|
|
2069
2094
|
* TestModel.find({ num: 'not a number' }).error(new Error('woops')).exec(function(error) {
|
|
@@ -2250,7 +2275,7 @@ Query.prototype._find = wrapThunk(function(callback) {
|
|
|
2250
2275
|
* If there are too many documents in the result to fit in memory, use
|
|
2251
2276
|
* [`Query.prototype.cursor()`](api.html#query_Query-cursor)
|
|
2252
2277
|
*
|
|
2253
|
-
* ####Example
|
|
2278
|
+
* #### Example
|
|
2254
2279
|
*
|
|
2255
2280
|
* // Using async/await
|
|
2256
2281
|
* const arr = await Movie.find({ year: { $gte: 1980, $lte: 1989 } });
|
|
@@ -2456,7 +2481,7 @@ Query.prototype._findOne = wrapThunk(function(callback) {
|
|
|
2456
2481
|
*
|
|
2457
2482
|
* - `findOne()`
|
|
2458
2483
|
*
|
|
2459
|
-
* ####Example
|
|
2484
|
+
* #### Example
|
|
2460
2485
|
*
|
|
2461
2486
|
* const query = Kitten.where({ color: 'white' });
|
|
2462
2487
|
* query.findOne(function (err, kitten) {
|
|
@@ -2607,7 +2632,7 @@ Query.prototype._estimatedDocumentCount = wrapThunk(function(callback) {
|
|
|
2607
2632
|
*
|
|
2608
2633
|
* - `count()`
|
|
2609
2634
|
*
|
|
2610
|
-
* ####Example:
|
|
2635
|
+
* #### Example:
|
|
2611
2636
|
*
|
|
2612
2637
|
* const countQuery = model.where({ 'color': 'black' }).count();
|
|
2613
2638
|
*
|
|
@@ -2664,7 +2689,7 @@ Query.prototype.count = function(filter, callback) {
|
|
|
2664
2689
|
*
|
|
2665
2690
|
* - `estimatedDocumentCount()`
|
|
2666
2691
|
*
|
|
2667
|
-
* ####Example:
|
|
2692
|
+
* #### Example:
|
|
2668
2693
|
*
|
|
2669
2694
|
* await Model.find().estimatedDocumentCount();
|
|
2670
2695
|
*
|
|
@@ -2710,7 +2735,7 @@ Query.prototype.estimatedDocumentCount = function(options, callback) {
|
|
|
2710
2735
|
*
|
|
2711
2736
|
* - `countDocuments()`
|
|
2712
2737
|
*
|
|
2713
|
-
* ####Example:
|
|
2738
|
+
* #### Example:
|
|
2714
2739
|
*
|
|
2715
2740
|
* const countQuery = model.where({ 'color': 'black' }).countDocuments();
|
|
2716
2741
|
*
|
|
@@ -2804,7 +2829,7 @@ Query.prototype.__distinct = wrapThunk(function __distinct(callback) {
|
|
|
2804
2829
|
*
|
|
2805
2830
|
* This function does not trigger any middleware.
|
|
2806
2831
|
*
|
|
2807
|
-
* ####Example
|
|
2832
|
+
* #### Example
|
|
2808
2833
|
*
|
|
2809
2834
|
* distinct(field, conditions, callback)
|
|
2810
2835
|
* distinct(field, conditions)
|
|
@@ -2865,7 +2890,7 @@ Query.prototype.distinct = function(field, conditions, callback) {
|
|
|
2865
2890
|
* sort order of each path is ascending unless the path name is prefixed with `-`
|
|
2866
2891
|
* which will be treated as descending.
|
|
2867
2892
|
*
|
|
2868
|
-
* ####Example
|
|
2893
|
+
* #### Example
|
|
2869
2894
|
*
|
|
2870
2895
|
* // sort by "field" ascending and "test" descending
|
|
2871
2896
|
* query.sort({ field: 'asc', test: -1 });
|
|
@@ -2873,7 +2898,7 @@ Query.prototype.distinct = function(field, conditions, callback) {
|
|
|
2873
2898
|
* // equivalent
|
|
2874
2899
|
* query.sort('field -test');
|
|
2875
2900
|
*
|
|
2876
|
-
* ####Note
|
|
2901
|
+
* #### Note
|
|
2877
2902
|
*
|
|
2878
2903
|
* Cannot be used with `distinct()`
|
|
2879
2904
|
*
|
|
@@ -2898,7 +2923,7 @@ Query.prototype.sort = function(arg) {
|
|
|
2898
2923
|
*
|
|
2899
2924
|
* This function does not trigger any middleware
|
|
2900
2925
|
*
|
|
2901
|
-
* ####Example
|
|
2926
|
+
* #### Example
|
|
2902
2927
|
*
|
|
2903
2928
|
* Character.remove({ name: /Stark/ }, callback);
|
|
2904
2929
|
*
|
|
@@ -2910,13 +2935,13 @@ Query.prototype.sort = function(arg) {
|
|
|
2910
2935
|
* - `deletedCount`: the number of documents deleted
|
|
2911
2936
|
* - `n`: the number of documents deleted. Equal to `deletedCount`.
|
|
2912
2937
|
*
|
|
2913
|
-
* ####Example
|
|
2938
|
+
* #### Example
|
|
2914
2939
|
*
|
|
2915
2940
|
* const res = await Character.remove({ name: /Stark/ });
|
|
2916
2941
|
* // Number of docs deleted
|
|
2917
2942
|
* res.deletedCount;
|
|
2918
2943
|
*
|
|
2919
|
-
* ####Note
|
|
2944
|
+
* #### Note
|
|
2920
2945
|
*
|
|
2921
2946
|
* Calling `remove()` creates a [Mongoose query](./queries.html), and a query
|
|
2922
2947
|
* does not execute until you either pass a callback, call [`Query#then()`](#query_Query-then),
|
|
@@ -2990,7 +3015,7 @@ Query.prototype._remove = wrapThunk(function(callback) {
|
|
|
2990
3015
|
*
|
|
2991
3016
|
* This function triggers `deleteOne` middleware.
|
|
2992
3017
|
*
|
|
2993
|
-
* ####Example
|
|
3018
|
+
* #### Example
|
|
2994
3019
|
*
|
|
2995
3020
|
* await Character.deleteOne({ name: 'Eddard Stark' });
|
|
2996
3021
|
*
|
|
@@ -3005,7 +3030,7 @@ Query.prototype._remove = wrapThunk(function(callback) {
|
|
|
3005
3030
|
* - `deletedCount`: the number of documents deleted
|
|
3006
3031
|
* - `n`: the number of documents deleted. Equal to `deletedCount`.
|
|
3007
3032
|
*
|
|
3008
|
-
* ####Example
|
|
3033
|
+
* #### Example
|
|
3009
3034
|
*
|
|
3010
3035
|
* const res = await Character.deleteOne({ name: 'Eddard Stark' });
|
|
3011
3036
|
* // `1` if MongoDB deleted a doc, `0` if no docs matched the filter `{ name: ... }`
|
|
@@ -3076,7 +3101,7 @@ Query.prototype._deleteOne = wrapThunk(function(callback) {
|
|
|
3076
3101
|
*
|
|
3077
3102
|
* This function triggers `deleteMany` middleware.
|
|
3078
3103
|
*
|
|
3079
|
-
* ####Example
|
|
3104
|
+
* #### Example
|
|
3080
3105
|
*
|
|
3081
3106
|
* await Character.deleteMany({ name: /Stark/, age: { $gte: 18 } });
|
|
3082
3107
|
*
|
|
@@ -3091,7 +3116,7 @@ Query.prototype._deleteOne = wrapThunk(function(callback) {
|
|
|
3091
3116
|
* - `deletedCount`: the number of documents deleted
|
|
3092
3117
|
* - `n`: the number of documents deleted. Equal to `deletedCount`.
|
|
3093
3118
|
*
|
|
3094
|
-
* ####Example
|
|
3119
|
+
* #### Example
|
|
3095
3120
|
*
|
|
3096
3121
|
* const res = await Character.deleteMany({ name: /Stark/, age: { $gte: 18 } });
|
|
3097
3122
|
* // `0` if no docs matched the filter, number of docs deleted otherwise
|
|
@@ -3226,7 +3251,7 @@ function prepareDiscriminatorCriteria(query) {
|
|
|
3226
3251
|
*
|
|
3227
3252
|
* - `findOneAndUpdate()`
|
|
3228
3253
|
*
|
|
3229
|
-
* ####Available options
|
|
3254
|
+
* #### Available options
|
|
3230
3255
|
*
|
|
3231
3256
|
* - `new`: bool - if true, return the modified document rather than the original. defaults to false (changed in 4.0)
|
|
3232
3257
|
* - `upsert`: bool - creates the object if it doesn't exist. defaults to false.
|
|
@@ -3237,13 +3262,13 @@ function prepareDiscriminatorCriteria(query) {
|
|
|
3237
3262
|
* - `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.
|
|
3238
3263
|
* - `rawResult`: if true, returns the [raw result from the MongoDB driver](https://mongodb.github.io/node-mongodb-native/4.3/interfaces/ModifyResult.html)
|
|
3239
3264
|
*
|
|
3240
|
-
* ####Callback Signature
|
|
3265
|
+
* #### Callback Signature
|
|
3241
3266
|
* function(error, doc) {
|
|
3242
3267
|
* // error: any errors that occurred
|
|
3243
3268
|
* // doc: the document before updates are applied if `new: false`, or after updates if `new = true`
|
|
3244
3269
|
* }
|
|
3245
3270
|
*
|
|
3246
|
-
* ####Examples
|
|
3271
|
+
* #### Examples
|
|
3247
3272
|
*
|
|
3248
3273
|
* query.findOneAndUpdate(conditions, update, options, callback) // executes
|
|
3249
3274
|
* query.findOneAndUpdate(conditions, update, options) // returns Query
|
|
@@ -3373,19 +3398,19 @@ Query.prototype._findOneAndUpdate = wrapThunk(function(callback) {
|
|
|
3373
3398
|
*
|
|
3374
3399
|
* - `findOneAndRemove()`
|
|
3375
3400
|
*
|
|
3376
|
-
* ####Available options
|
|
3401
|
+
* #### Available options
|
|
3377
3402
|
*
|
|
3378
3403
|
* - `sort`: if multiple docs are found by the conditions, sets the sort order to choose which doc to update
|
|
3379
3404
|
* - `maxTimeMS`: puts a time limit on the query - requires mongodb >= 2.6.0
|
|
3380
3405
|
* - `rawResult`: if true, resolves to the [raw result from the MongoDB driver](https://mongodb.github.io/node-mongodb-native/4.3/interfaces/ModifyResult.html)
|
|
3381
3406
|
*
|
|
3382
|
-
* ####Callback Signature
|
|
3407
|
+
* #### Callback Signature
|
|
3383
3408
|
* function(error, doc) {
|
|
3384
3409
|
* // error: any errors that occurred
|
|
3385
3410
|
* // doc: the document before updates are applied if `new: false`, or after updates if `new = true`
|
|
3386
3411
|
* }
|
|
3387
3412
|
*
|
|
3388
|
-
* ####Examples
|
|
3413
|
+
* #### Examples
|
|
3389
3414
|
*
|
|
3390
3415
|
* A.where().findOneAndRemove(conditions, options, callback) // executes
|
|
3391
3416
|
* A.where().findOneAndRemove(conditions, options) // return Query
|
|
@@ -3460,19 +3485,19 @@ Query.prototype.findOneAndRemove = function(conditions, options, callback) {
|
|
|
3460
3485
|
* this distinction is purely pedantic. You should use `findOneAndDelete()`
|
|
3461
3486
|
* unless you have a good reason not to.
|
|
3462
3487
|
*
|
|
3463
|
-
* ####Available options
|
|
3488
|
+
* #### Available options
|
|
3464
3489
|
*
|
|
3465
3490
|
* - `sort`: if multiple docs are found by the conditions, sets the sort order to choose which doc to update
|
|
3466
3491
|
* - `maxTimeMS`: puts a time limit on the query - requires mongodb >= 2.6.0
|
|
3467
3492
|
* - `rawResult`: if true, resolves to the [raw result from the MongoDB driver](https://mongodb.github.io/node-mongodb-native/4.3/interfaces/ModifyResult.html)
|
|
3468
3493
|
*
|
|
3469
|
-
* ####Callback Signature
|
|
3494
|
+
* #### Callback Signature
|
|
3470
3495
|
* function(error, doc) {
|
|
3471
3496
|
* // error: any errors that occurred
|
|
3472
3497
|
* // doc: the document before updates are applied if `new: false`, or after updates if `new = true`
|
|
3473
3498
|
* }
|
|
3474
3499
|
*
|
|
3475
|
-
* ####Examples
|
|
3500
|
+
* #### Examples
|
|
3476
3501
|
*
|
|
3477
3502
|
* A.where().findOneAndDelete(conditions, options, callback) // executes
|
|
3478
3503
|
* A.where().findOneAndDelete(conditions, options) // return Query
|
|
@@ -3579,19 +3604,19 @@ Query.prototype._findOneAndDelete = wrapThunk(function(callback) {
|
|
|
3579
3604
|
*
|
|
3580
3605
|
* - `findOneAndReplace()`
|
|
3581
3606
|
*
|
|
3582
|
-
* ####Available options
|
|
3607
|
+
* #### Available options
|
|
3583
3608
|
*
|
|
3584
3609
|
* - `sort`: if multiple docs are found by the conditions, sets the sort order to choose which doc to update
|
|
3585
3610
|
* - `maxTimeMS`: puts a time limit on the query - requires mongodb >= 2.6.0
|
|
3586
3611
|
* - `rawResult`: if true, resolves to the [raw result from the MongoDB driver](https://mongodb.github.io/node-mongodb-native/4.3/interfaces/ModifyResult.html)
|
|
3587
3612
|
*
|
|
3588
|
-
* ####Callback Signature
|
|
3613
|
+
* #### Callback Signature
|
|
3589
3614
|
* function(error, doc) {
|
|
3590
3615
|
* // error: any errors that occurred
|
|
3591
3616
|
* // doc: the document before updates are applied if `new: false`, or after updates if `new = true`
|
|
3592
3617
|
* }
|
|
3593
3618
|
*
|
|
3594
|
-
* ####Examples
|
|
3619
|
+
* #### Examples
|
|
3595
3620
|
*
|
|
3596
3621
|
* A.where().findOneAndReplace(filter, replacement, options, callback); // executes
|
|
3597
3622
|
* A.where().findOneAndReplace(filter, replacement, options); // return Query
|
|
@@ -3689,7 +3714,6 @@ Query.prototype.findOneAndReplace = function(filter, replacement, options, callb
|
|
|
3689
3714
|
*/
|
|
3690
3715
|
Query.prototype._findOneAndReplace = wrapThunk(function(callback) {
|
|
3691
3716
|
this._castConditions();
|
|
3692
|
-
|
|
3693
3717
|
if (this.error() != null) {
|
|
3694
3718
|
callback(this.error());
|
|
3695
3719
|
return null;
|
|
@@ -3700,9 +3724,6 @@ Query.prototype._findOneAndReplace = wrapThunk(function(callback) {
|
|
|
3700
3724
|
convertNewToReturnDocument(options);
|
|
3701
3725
|
let fields = null;
|
|
3702
3726
|
|
|
3703
|
-
let castedDoc = new this.model(this._update, null, true);
|
|
3704
|
-
this._update = castedDoc;
|
|
3705
|
-
|
|
3706
3727
|
this._applyPaths();
|
|
3707
3728
|
if (this._fields != null) {
|
|
3708
3729
|
options.projection = this._castFields(utils.clone(this._fields));
|
|
@@ -3713,7 +3734,34 @@ Query.prototype._findOneAndReplace = wrapThunk(function(callback) {
|
|
|
3713
3734
|
}
|
|
3714
3735
|
}
|
|
3715
3736
|
|
|
3716
|
-
|
|
3737
|
+
const runValidators = _getOption(this, 'runValidators', false);
|
|
3738
|
+
if (runValidators === false) {
|
|
3739
|
+
try {
|
|
3740
|
+
this._update = this._castUpdate(this._update, true);
|
|
3741
|
+
} catch (err) {
|
|
3742
|
+
const validationError = new ValidationError();
|
|
3743
|
+
validationError.errors[err.path] = err;
|
|
3744
|
+
callback(validationError);
|
|
3745
|
+
return null;
|
|
3746
|
+
}
|
|
3747
|
+
|
|
3748
|
+
this._collection.collection.findOneAndReplace(filter, this._update || {}, options, _wrapThunkCallback(this, (err, res) => {
|
|
3749
|
+
if (err) {
|
|
3750
|
+
return callback(err);
|
|
3751
|
+
}
|
|
3752
|
+
|
|
3753
|
+
const doc = res.value;
|
|
3754
|
+
|
|
3755
|
+
return this._completeOne(doc, res, callback);
|
|
3756
|
+
}));
|
|
3757
|
+
|
|
3758
|
+
return;
|
|
3759
|
+
}
|
|
3760
|
+
|
|
3761
|
+
|
|
3762
|
+
let castedDoc = new this.model(this._update, null, true);
|
|
3763
|
+
this._update = castedDoc;
|
|
3764
|
+
castedDoc.validate(err => {
|
|
3717
3765
|
if (err != null) {
|
|
3718
3766
|
return callback(err);
|
|
3719
3767
|
}
|
|
@@ -3836,7 +3884,11 @@ Query.prototype._findAndModify = function(type, callback) {
|
|
|
3836
3884
|
}
|
|
3837
3885
|
|
|
3838
3886
|
if (!isOverwriting) {
|
|
3839
|
-
|
|
3887
|
+
try {
|
|
3888
|
+
this._update = this._castUpdate(this._update, opts.overwrite);
|
|
3889
|
+
} catch (err) {
|
|
3890
|
+
return callback(err);
|
|
3891
|
+
}
|
|
3840
3892
|
const _opts = Object.assign({}, opts, {
|
|
3841
3893
|
setDefaultsOnInsert: this._mongooseOptions.setDefaultsOnInsert
|
|
3842
3894
|
});
|
|
@@ -4031,10 +4083,10 @@ function _updateThunk(op, callback) {
|
|
|
4031
4083
|
}
|
|
4032
4084
|
this._update = new this.model(this._update, null, true);
|
|
4033
4085
|
} else {
|
|
4034
|
-
|
|
4035
|
-
|
|
4036
|
-
|
|
4037
|
-
callback(
|
|
4086
|
+
try {
|
|
4087
|
+
this._update = this._castUpdate(this._update, options.overwrite);
|
|
4088
|
+
} catch (err) {
|
|
4089
|
+
callback(err);
|
|
4038
4090
|
return null;
|
|
4039
4091
|
}
|
|
4040
4092
|
|
|
@@ -4156,15 +4208,15 @@ Query.prototype._replaceOne = wrapThunk(function(callback) {
|
|
|
4156
4208
|
*
|
|
4157
4209
|
* - `update()`
|
|
4158
4210
|
*
|
|
4159
|
-
* ####Example
|
|
4211
|
+
* #### Example
|
|
4160
4212
|
*
|
|
4161
|
-
* Model.where({ _id: id }).update({ title: 'words' })
|
|
4213
|
+
* Model.where({ _id: id }).update({ title: 'words' });
|
|
4162
4214
|
*
|
|
4163
4215
|
* // becomes
|
|
4164
4216
|
*
|
|
4165
|
-
* Model.where({ _id: id }).update({ $set: { title: 'words' }})
|
|
4217
|
+
* Model.where({ _id: id }).update({ $set: { title: 'words' }});
|
|
4166
4218
|
*
|
|
4167
|
-
* ####Valid options:
|
|
4219
|
+
* #### Valid options:
|
|
4168
4220
|
*
|
|
4169
4221
|
* - `upsert` (boolean) whether to create the doc if it doesn't match (false)
|
|
4170
4222
|
* - `multi` (boolean) whether multiple documents should be updated (false)
|
|
@@ -4174,47 +4226,51 @@ Query.prototype._replaceOne = wrapThunk(function(callback) {
|
|
|
4174
4226
|
* - `read`
|
|
4175
4227
|
* - `writeConcern`
|
|
4176
4228
|
*
|
|
4177
|
-
* ####Note
|
|
4229
|
+
* #### Note
|
|
4178
4230
|
*
|
|
4179
4231
|
* Passing an empty object `{}` as the doc will result in a no-op. The update operation will be ignored and the callback executed without sending the command to MongoDB.
|
|
4180
4232
|
*
|
|
4181
|
-
* ####Note
|
|
4233
|
+
* #### Note
|
|
4182
4234
|
*
|
|
4183
4235
|
* The operation is only executed when a callback is passed. To force execution without a callback, we must first call update() and then execute it by using the `exec()` method.
|
|
4184
4236
|
*
|
|
4185
|
-
*
|
|
4186
|
-
*
|
|
4237
|
+
* ```javascript
|
|
4238
|
+
* const q = Model.where({ _id: id });
|
|
4239
|
+
* q.update({ $set: { name: 'bob' }}).update(); // not executed
|
|
4187
4240
|
*
|
|
4188
|
-
*
|
|
4241
|
+
* q.update({ $set: { name: 'bob' }}).exec(); // executed
|
|
4189
4242
|
*
|
|
4190
|
-
*
|
|
4191
|
-
*
|
|
4192
|
-
*
|
|
4243
|
+
* // keys that are not [atomic](https://docs.mongodb.com/manual/tutorial/model-data-for-atomic-operations/#pattern) ops become `$set`.
|
|
4244
|
+
* // this executes the same command as the previous example.
|
|
4245
|
+
* q.update({ name: 'bob' }).exec();
|
|
4193
4246
|
*
|
|
4194
|
-
*
|
|
4195
|
-
*
|
|
4196
|
-
*
|
|
4247
|
+
* // multi updates
|
|
4248
|
+
* Model.where()
|
|
4249
|
+
* .update({ name: /^match/ }, { $set: { arr: [] }}, { multi: true }, callback)
|
|
4197
4250
|
*
|
|
4198
|
-
*
|
|
4199
|
-
*
|
|
4200
|
-
*
|
|
4201
|
-
*
|
|
4251
|
+
* // more multi updates
|
|
4252
|
+
* Model.where()
|
|
4253
|
+
* .setOptions({ multi: true })
|
|
4254
|
+
* .update({ $set: { arr: [] }}, callback)
|
|
4202
4255
|
*
|
|
4203
|
-
*
|
|
4204
|
-
*
|
|
4205
|
-
*
|
|
4256
|
+
* // single update by default
|
|
4257
|
+
* Model.where({ email: 'address@example.com' })
|
|
4258
|
+
* .update({ $inc: { counter: 1 }}, callback)
|
|
4259
|
+
* ```
|
|
4206
4260
|
*
|
|
4207
4261
|
* API summary
|
|
4208
4262
|
*
|
|
4209
|
-
*
|
|
4210
|
-
*
|
|
4211
|
-
*
|
|
4212
|
-
*
|
|
4213
|
-
*
|
|
4214
|
-
*
|
|
4215
|
-
*
|
|
4216
|
-
*
|
|
4217
|
-
*
|
|
4263
|
+
* ```javascript
|
|
4264
|
+
* update(filter, doc, options, cb); // executes
|
|
4265
|
+
* update(filter, doc, options);
|
|
4266
|
+
* update(filter, doc, cb); // executes
|
|
4267
|
+
* update(filter, doc);
|
|
4268
|
+
* update(doc, cb); // executes
|
|
4269
|
+
* update(doc);
|
|
4270
|
+
* update(cb); // executes
|
|
4271
|
+
* update(true); // executes
|
|
4272
|
+
* update();
|
|
4273
|
+
* ```
|
|
4218
4274
|
*
|
|
4219
4275
|
* @param {Object} [filter]
|
|
4220
4276
|
* @param {Object} [doc] the update command
|
|
@@ -4271,7 +4327,7 @@ Query.prototype.update = function(conditions, doc, options, callback) {
|
|
|
4271
4327
|
* **Note** updateMany will _not_ fire update middleware. Use `pre('updateMany')`
|
|
4272
4328
|
* and `post('updateMany')` instead.
|
|
4273
4329
|
*
|
|
4274
|
-
* ####Example:
|
|
4330
|
+
* #### Example:
|
|
4275
4331
|
* const res = await Person.updateMany({ name: /Stark$/ }, { isDeleted: true });
|
|
4276
4332
|
* res.n; // Number of documents matched
|
|
4277
4333
|
* res.nModified; // Number of documents modified
|
|
@@ -4336,7 +4392,7 @@ Query.prototype.updateMany = function(conditions, doc, options, callback) {
|
|
|
4336
4392
|
* **Note** updateOne will _not_ fire update middleware. Use `pre('updateOne')`
|
|
4337
4393
|
* and `post('updateOne')` instead.
|
|
4338
4394
|
*
|
|
4339
|
-
* ####Example:
|
|
4395
|
+
* #### Example:
|
|
4340
4396
|
* const res = await Person.updateOne({ name: 'Jean-Luc Picard' }, { ship: 'USS Enterprise' });
|
|
4341
4397
|
* res.n; // Number of documents matched
|
|
4342
4398
|
* res.nModified; // Number of documents modified
|
|
@@ -4399,7 +4455,7 @@ Query.prototype.updateOne = function(conditions, doc, options, callback) {
|
|
|
4399
4455
|
* **Note** replaceOne will _not_ fire update middleware. Use `pre('replaceOne')`
|
|
4400
4456
|
* and `post('replaceOne')` instead.
|
|
4401
4457
|
*
|
|
4402
|
-
* ####Example:
|
|
4458
|
+
* #### Example:
|
|
4403
4459
|
* const res = await Person.replaceOne({ _id: 24601 }, { name: 'Jean Valjean' });
|
|
4404
4460
|
* res.n; // Number of documents matched
|
|
4405
4461
|
* res.nModified; // Number of documents modified
|
|
@@ -4503,7 +4559,7 @@ function _update(query, op, filter, doc, options, callback) {
|
|
|
4503
4559
|
*
|
|
4504
4560
|
* Any functions you pass to `transform()` will run **after** any post hooks.
|
|
4505
4561
|
*
|
|
4506
|
-
* ####Example:
|
|
4562
|
+
* #### Example:
|
|
4507
4563
|
*
|
|
4508
4564
|
* const res = await MyModel.findOne().transform(res => {
|
|
4509
4565
|
* // Sets a `loadedAt` property on the doc that tells you the time the
|
|
@@ -4530,7 +4586,7 @@ Query.prototype.transform = function(fn) {
|
|
|
4530
4586
|
* This is handy for integrating with async/await, because `orFail()` saves you
|
|
4531
4587
|
* an extra `if` statement to check if no document was found.
|
|
4532
4588
|
*
|
|
4533
|
-
* ####Example:
|
|
4589
|
+
* #### Example:
|
|
4534
4590
|
*
|
|
4535
4591
|
* // Throws if no doc returned
|
|
4536
4592
|
* await Model.findOne({ foo: 'bar' }).orFail();
|
|
@@ -4620,7 +4676,7 @@ function _orFailError(err, query) {
|
|
|
4620
4676
|
/**
|
|
4621
4677
|
* Executes the query
|
|
4622
4678
|
*
|
|
4623
|
-
* ####Examples:
|
|
4679
|
+
* #### Examples:
|
|
4624
4680
|
*
|
|
4625
4681
|
* const promise = query.exec();
|
|
4626
4682
|
* const promise = query.exec('update');
|
|
@@ -4760,7 +4816,7 @@ Query.prototype.catch = function(reject) {
|
|
|
4760
4816
|
* Add pre [middleware](/docs/middleware.html) to this query instance. Doesn't affect
|
|
4761
4817
|
* other queries.
|
|
4762
4818
|
*
|
|
4763
|
-
* ####Example:
|
|
4819
|
+
* #### Example:
|
|
4764
4820
|
*
|
|
4765
4821
|
* const q1 = Question.find({ answer: 42 });
|
|
4766
4822
|
* q1.pre(function middleware() {
|
|
@@ -4786,7 +4842,7 @@ Query.prototype.pre = function(fn) {
|
|
|
4786
4842
|
* Add post [middleware](/docs/middleware.html) to this query instance. Doesn't affect
|
|
4787
4843
|
* other queries.
|
|
4788
4844
|
*
|
|
4789
|
-
* ####Example:
|
|
4845
|
+
* #### Example:
|
|
4790
4846
|
*
|
|
4791
4847
|
* const q1 = Question.find({ answer: 42 });
|
|
4792
4848
|
* q1.post(function middleware() {
|
|
@@ -4869,23 +4925,10 @@ function castQuery(query) {
|
|
|
4869
4925
|
}
|
|
4870
4926
|
}
|
|
4871
4927
|
|
|
4872
|
-
/*!
|
|
4873
|
-
* castDoc
|
|
4874
|
-
* @api private
|
|
4875
|
-
*/
|
|
4876
|
-
|
|
4877
|
-
function castDoc(query, overwrite) {
|
|
4878
|
-
try {
|
|
4879
|
-
return query._castUpdate(query._update, overwrite);
|
|
4880
|
-
} catch (err) {
|
|
4881
|
-
return err;
|
|
4882
|
-
}
|
|
4883
|
-
}
|
|
4884
|
-
|
|
4885
4928
|
/**
|
|
4886
4929
|
* Specifies paths which should be populated with other documents.
|
|
4887
4930
|
*
|
|
4888
|
-
* ####Example:
|
|
4931
|
+
* #### Example:
|
|
4889
4932
|
*
|
|
4890
4933
|
* let book = await Book.findOne().populate('authors');
|
|
4891
4934
|
* book.title; // 'Node.js in Action'
|
|
@@ -4992,14 +5035,14 @@ Query.prototype.populate = function() {
|
|
|
4992
5035
|
/**
|
|
4993
5036
|
* Gets a list of paths to be populated by this query
|
|
4994
5037
|
*
|
|
4995
|
-
* ####Example:
|
|
5038
|
+
* #### Example:
|
|
4996
5039
|
* bookSchema.pre('findOne', function() {
|
|
4997
5040
|
* let keys = this.getPopulatedPaths(); // ['author']
|
|
4998
5041
|
* });
|
|
4999
5042
|
* ...
|
|
5000
5043
|
* Book.findOne({}).populate('author');
|
|
5001
5044
|
*
|
|
5002
|
-
* ####Example:
|
|
5045
|
+
* #### Example:
|
|
5003
5046
|
* // Deep populate
|
|
5004
5047
|
* const q = L1.find().populate({
|
|
5005
5048
|
* path: 'level2',
|
|
@@ -5041,7 +5084,7 @@ function _getPopulatedPaths(list, arr, prefix) {
|
|
|
5041
5084
|
/**
|
|
5042
5085
|
* Casts this query to the schema of `model`
|
|
5043
5086
|
*
|
|
5044
|
-
* ####Note
|
|
5087
|
+
* #### Note
|
|
5045
5088
|
*
|
|
5046
5089
|
* If `obj` is present, it is cast instead of this query.
|
|
5047
5090
|
*
|
|
@@ -5166,7 +5209,7 @@ Query.prototype._applyPaths = function applyPaths() {
|
|
|
5166
5209
|
*
|
|
5167
5210
|
* The `.cursor()` function triggers pre find hooks, but **not** post find hooks.
|
|
5168
5211
|
*
|
|
5169
|
-
* ####Example
|
|
5212
|
+
* #### Example
|
|
5170
5213
|
*
|
|
5171
5214
|
* // There are 2 ways to use a cursor. First, as a stream:
|
|
5172
5215
|
* Thing.
|
|
@@ -5190,7 +5233,7 @@ Query.prototype._applyPaths = function applyPaths() {
|
|
|
5190
5233
|
* console.log(doc);
|
|
5191
5234
|
* }
|
|
5192
5235
|
*
|
|
5193
|
-
* ####Valid options
|
|
5236
|
+
* #### Valid options
|
|
5194
5237
|
*
|
|
5195
5238
|
* - `transform`: optional function which accepts a mongoose document. The return value of the function will be emitted on `data` and returned by `.next()`.
|
|
5196
5239
|
*
|
|
@@ -5237,7 +5280,7 @@ Query.prototype.maxscan = Query.base.maxScan;
|
|
|
5237
5280
|
/**
|
|
5238
5281
|
* Sets the tailable option (for use with capped collections).
|
|
5239
5282
|
*
|
|
5240
|
-
* ####Example
|
|
5283
|
+
* #### Example
|
|
5241
5284
|
*
|
|
5242
5285
|
* query.tailable(); // true
|
|
5243
5286
|
* query.tailable(true);
|
|
@@ -5246,7 +5289,7 @@ Query.prototype.maxscan = Query.base.maxScan;
|
|
|
5246
5289
|
* // Set both `tailable` and `awaitData` options
|
|
5247
5290
|
* query.tailable({ awaitData: true });
|
|
5248
5291
|
*
|
|
5249
|
-
* ####Note
|
|
5292
|
+
* #### Note
|
|
5250
5293
|
*
|
|
5251
5294
|
* Cannot be used with `distinct()`
|
|
5252
5295
|
*
|
|
@@ -5287,23 +5330,23 @@ Query.prototype.tailable = function(val, opts) {
|
|
|
5287
5330
|
/**
|
|
5288
5331
|
* Declares an intersects query for `geometry()`.
|
|
5289
5332
|
*
|
|
5290
|
-
* ####Example
|
|
5333
|
+
* #### Example
|
|
5291
5334
|
*
|
|
5292
5335
|
* query.where('path').intersects().geometry({
|
|
5293
|
-
*
|
|
5294
|
-
*
|
|
5295
|
-
* })
|
|
5336
|
+
* type: 'LineString',
|
|
5337
|
+
* coordinates: [[180.0, 11.0], [180, 9.0]]
|
|
5338
|
+
* });
|
|
5296
5339
|
*
|
|
5297
5340
|
* query.where('path').intersects({
|
|
5298
|
-
*
|
|
5299
|
-
*
|
|
5300
|
-
* })
|
|
5341
|
+
* type: 'LineString',
|
|
5342
|
+
* coordinates: [[180.0, 11.0], [180, 9.0]]
|
|
5343
|
+
* });
|
|
5301
5344
|
*
|
|
5302
|
-
* ####
|
|
5345
|
+
* #### Note:
|
|
5303
5346
|
*
|
|
5304
5347
|
* **MUST** be used after `where()`.
|
|
5305
5348
|
*
|
|
5306
|
-
* ####
|
|
5349
|
+
* #### Note:
|
|
5307
5350
|
*
|
|
5308
5351
|
* In Mongoose 3.7, `intersects` changed from a getter to a function. If you need the old syntax, use [this](https://github.com/ebensing/mongoose-within).
|
|
5309
5352
|
*
|
|
@@ -5320,7 +5363,7 @@ Query.prototype.tailable = function(val, opts) {
|
|
|
5320
5363
|
/**
|
|
5321
5364
|
* Specifies a `$geometry` condition
|
|
5322
5365
|
*
|
|
5323
|
-
* ####Example
|
|
5366
|
+
* #### Example
|
|
5324
5367
|
*
|
|
5325
5368
|
* const polyA = [[[ 10, 20 ], [ 10, 40 ], [ 30, 40 ], [ 30, 20 ]]]
|
|
5326
5369
|
* query.where('loc').within().geometry({ type: 'Polygon', coordinates: polyA })
|
|
@@ -5338,7 +5381,7 @@ Query.prototype.tailable = function(val, opts) {
|
|
|
5338
5381
|
*
|
|
5339
5382
|
* The argument is assigned to the most recent path passed to `where()`.
|
|
5340
5383
|
*
|
|
5341
|
-
* ####
|
|
5384
|
+
* #### Note:
|
|
5342
5385
|
*
|
|
5343
5386
|
* `geometry()` **must** come after either `intersects()` or `within()`.
|
|
5344
5387
|
*
|
|
@@ -5362,7 +5405,7 @@ Query.prototype.tailable = function(val, opts) {
|
|
|
5362
5405
|
*
|
|
5363
5406
|
* These operators return documents sorted by distance.
|
|
5364
5407
|
*
|
|
5365
|
-
* ####Example
|
|
5408
|
+
* #### Example
|
|
5366
5409
|
*
|
|
5367
5410
|
* query.where('loc').near({ center: [10, 10] });
|
|
5368
5411
|
* query.where('loc').near({ center: [10, 10], maxDistance: 5 });
|
|
@@ -5445,13 +5488,13 @@ Query.prototype.near = function() {
|
|
|
5445
5488
|
/**
|
|
5446
5489
|
* _DEPRECATED_ Specifies a `$nearSphere` condition
|
|
5447
5490
|
*
|
|
5448
|
-
* ####Example
|
|
5491
|
+
* #### Example
|
|
5449
5492
|
*
|
|
5450
5493
|
* query.where('loc').nearSphere({ center: [10, 10], maxDistance: 5 });
|
|
5451
5494
|
*
|
|
5452
5495
|
* **Deprecated.** Use `query.near()` instead with the `spherical` option set to `true`.
|
|
5453
5496
|
*
|
|
5454
|
-
* ####Example
|
|
5497
|
+
* #### Example
|
|
5455
5498
|
*
|
|
5456
5499
|
* query.where('loc').near({ center: [10, 10], spherical: true });
|
|
5457
5500
|
*
|
|
@@ -5474,7 +5517,7 @@ Query.prototype.nearSphere = function() {
|
|
|
5474
5517
|
* You do not need to call this function explicitly, the JavaScript runtime
|
|
5475
5518
|
* will call it for you.
|
|
5476
5519
|
*
|
|
5477
|
-
* ####Example
|
|
5520
|
+
* #### Example
|
|
5478
5521
|
*
|
|
5479
5522
|
* for await (const doc of Model.aggregate([{ $sort: { name: 1 } }])) {
|
|
5480
5523
|
* console.log(doc.name);
|
|
@@ -5502,10 +5545,10 @@ if (Symbol.asyncIterator != null) {
|
|
|
5502
5545
|
/**
|
|
5503
5546
|
* Specifies a `$polygon` condition
|
|
5504
5547
|
*
|
|
5505
|
-
* ####Example
|
|
5548
|
+
* #### Example
|
|
5506
5549
|
*
|
|
5507
|
-
* query.where('loc').within().polygon([10,20], [13, 25], [7,15])
|
|
5508
|
-
* query.polygon('loc', [10,20], [13, 25], [7,15])
|
|
5550
|
+
* query.where('loc').within().polygon([10, 20], [13, 25], [7, 15]);
|
|
5551
|
+
* query.polygon('loc', [10, 20], [13, 25], [7, 15]);
|
|
5509
5552
|
*
|
|
5510
5553
|
* @method polygon
|
|
5511
5554
|
* @memberOf Query
|
|
@@ -5521,7 +5564,7 @@ if (Symbol.asyncIterator != null) {
|
|
|
5521
5564
|
/**
|
|
5522
5565
|
* Specifies a `$box` condition
|
|
5523
5566
|
*
|
|
5524
|
-
* ####Example
|
|
5567
|
+
* #### Example
|
|
5525
5568
|
*
|
|
5526
5569
|
* const lowerLeft = [40.73083, -73.99756]
|
|
5527
5570
|
* const upperRight= [40.741404, -73.988135]
|
|
@@ -5558,7 +5601,7 @@ Query.prototype.box = function(ll, ur) {
|
|
|
5558
5601
|
/**
|
|
5559
5602
|
* Specifies a `$center` or `$centerSphere` condition.
|
|
5560
5603
|
*
|
|
5561
|
-
* ####Example
|
|
5604
|
+
* #### Example
|
|
5562
5605
|
*
|
|
5563
5606
|
* const area = { center: [50, 50], radius: 10, unique: true }
|
|
5564
5607
|
* query.where('loc').within().circle(area)
|
|
@@ -5603,7 +5646,7 @@ Query.prototype.center = Query.base.circle;
|
|
|
5603
5646
|
*
|
|
5604
5647
|
* **Deprecated.** Use [circle](#query_Query-circle) instead.
|
|
5605
5648
|
*
|
|
5606
|
-
* ####Example
|
|
5649
|
+
* #### Example
|
|
5607
5650
|
*
|
|
5608
5651
|
* const area = { center: [50, 50], radius: 10 };
|
|
5609
5652
|
* query.where('loc').within().centerSphere(area);
|
|
@@ -5642,9 +5685,9 @@ Query.prototype.centerSphere = function() {
|
|
|
5642
5685
|
/**
|
|
5643
5686
|
* Determines if inclusive field selection has been made.
|
|
5644
5687
|
*
|
|
5645
|
-
* query.selectedInclusively() // false
|
|
5646
|
-
* query.select('name')
|
|
5647
|
-
* query.selectedInclusively() // true
|
|
5688
|
+
* query.selectedInclusively(); // false
|
|
5689
|
+
* query.select('name');
|
|
5690
|
+
* query.selectedInclusively(); // true
|
|
5648
5691
|
*
|
|
5649
5692
|
* @method selectedInclusively
|
|
5650
5693
|
* @memberOf Query
|
|
@@ -5660,10 +5703,10 @@ Query.prototype.selectedInclusively = function selectedInclusively() {
|
|
|
5660
5703
|
/**
|
|
5661
5704
|
* Determines if exclusive field selection has been made.
|
|
5662
5705
|
*
|
|
5663
|
-
* query.selectedExclusively() // false
|
|
5664
|
-
* query.select('-name')
|
|
5665
|
-
* query.selectedExclusively() // true
|
|
5666
|
-
* query.selectedInclusively() // false
|
|
5706
|
+
* query.selectedExclusively(); // false
|
|
5707
|
+
* query.select('-name');
|
|
5708
|
+
* query.selectedExclusively(); // true
|
|
5709
|
+
* query.selectedInclusively(); // false
|
|
5667
5710
|
*
|
|
5668
5711
|
* @method selectedExclusively
|
|
5669
5712
|
* @memberOf Query
|
|
@@ -5696,4 +5739,4 @@ Query.prototype.model;
|
|
|
5696
5739
|
* Export
|
|
5697
5740
|
*/
|
|
5698
5741
|
|
|
5699
|
-
module.exports = Query;
|
|
5742
|
+
module.exports = Query;
|