@datanimbus/mongoose 9.13.1 → 9.13.2
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/README.md +80 -70
- package/eslint.config.mjs +202 -0
- package/index.js +2 -1
- package/lib/aggregate.js +276 -174
- package/lib/cast/bigint.js +46 -0
- package/lib/cast/boolean.js +3 -3
- package/lib/cast/decimal128.js +4 -1
- package/lib/cast/double.js +56 -0
- package/lib/cast/int32.js +42 -0
- package/lib/cast/number.js +6 -6
- package/lib/cast/objectid.js +3 -3
- package/lib/cast/string.js +3 -3
- package/lib/cast/uuid.js +35 -0
- package/lib/cast.js +100 -92
- package/lib/collection.js +31 -22
- package/lib/connection.js +792 -507
- package/lib/constants.js +73 -0
- package/lib/cursor/{AggregationCursor.js → aggregationCursor.js} +205 -52
- package/lib/cursor/changeStream.js +218 -0
- package/lib/cursor/queryCursor.js +676 -0
- package/lib/defaults/schemaDefaultsCache.js +62 -0
- package/lib/document.js +2419 -1306
- package/lib/drivers/node-mongodb-native/bulkWriteResult.js +5 -0
- package/lib/drivers/node-mongodb-native/collection.js +101 -135
- package/lib/drivers/node-mongodb-native/connection.js +386 -28
- package/lib/drivers/node-mongodb-native/index.js +3 -5
- package/lib/error/browserMissingSchema.js +5 -4
- package/lib/error/bulkSaveIncompleteError.js +44 -0
- package/lib/error/cast.js +30 -21
- package/lib/error/createCollectionsError.js +26 -0
- package/lib/error/divergentArray.js +11 -9
- package/lib/error/eachAsyncMultiError.js +2 -2
- package/lib/error/index.js +38 -29
- package/lib/error/invalidSchemaOption.js +32 -0
- package/lib/error/messages.js +4 -3
- package/lib/error/missingSchema.js +8 -6
- package/lib/error/notFound.js +7 -5
- package/lib/error/objectExpected.js +9 -8
- package/lib/error/objectParameter.js +12 -11
- package/lib/error/overwriteModel.js +7 -6
- package/lib/error/parallelSave.js +11 -8
- package/lib/error/parallelValidate.js +9 -7
- package/lib/error/serverSelection.js +1 -0
- package/lib/error/setOptionError.js +11 -9
- package/lib/error/strict.js +12 -10
- package/lib/error/strictPopulate.js +11 -9
- package/lib/error/syncIndexes.js +2 -2
- package/lib/error/validation.js +11 -17
- package/lib/error/validator.js +9 -8
- package/lib/error/version.js +12 -10
- package/lib/filters/operators.js +8 -0
- package/lib/helpers/aggregate/prepareDiscriminatorPipeline.js +4 -4
- package/lib/helpers/buildMiddlewareFilter.js +42 -0
- package/lib/helpers/clone.js +121 -35
- package/lib/helpers/common.js +7 -7
- package/lib/helpers/cursor/eachAsync.js +14 -11
- package/lib/helpers/cursor/waitForInitialization.js +17 -0
- package/lib/helpers/discriminator/applyEmbeddedDiscriminators.js +36 -0
- package/lib/helpers/discriminator/areDiscriminatorValuesEqual.js +1 -1
- package/lib/helpers/discriminator/getConstructor.js +9 -6
- package/lib/helpers/discriminator/getDiscriminatorByKey.js +1 -1
- package/lib/helpers/discriminator/getDiscriminatorByValue.js +1 -1
- package/lib/helpers/discriminator/getDiscriminatorValueFromFilter.js +30 -0
- package/lib/helpers/discriminator/getSchemaDiscriminatorByValue.js +1 -1
- package/lib/helpers/discriminator/mergeDiscriminatorSchema.js +41 -9
- package/lib/helpers/document/applyDefaults.js +82 -14
- package/lib/helpers/document/applyTimestamps.js +106 -0
- package/lib/helpers/document/applyVirtuals.js +147 -0
- package/lib/helpers/document/cleanModifiedSubpaths.js +14 -4
- package/lib/helpers/document/compile.js +20 -15
- package/lib/helpers/document/getDeepestSubdocumentForPath.js +38 -0
- package/lib/helpers/document/getEmbeddedDiscriminatorPath.js +6 -3
- package/lib/helpers/document/handleSpreadDoc.js +2 -1
- package/lib/helpers/document/isInPathsToSave.js +24 -0
- package/lib/helpers/document/toObjectRecursive.js +49 -0
- package/lib/helpers/get.js +1 -1
- package/lib/helpers/getDefaultBulkwriteResult.js +11 -20
- package/lib/helpers/getInheritedProperty.js +28 -0
- package/lib/helpers/getOwnProperty.js +19 -0
- package/lib/helpers/indexes/applySchemaCollation.js +1 -1
- package/lib/helpers/indexes/decorateDiscriminatorIndexOptions.js +1 -1
- package/lib/helpers/indexes/getRelatedIndexes.js +8 -4
- package/lib/helpers/indexes/isDefaultIdIndex.js +1 -1
- package/lib/helpers/indexes/isIndexEqual.js +13 -12
- package/lib/helpers/indexes/isIndexSpecEqual.js +32 -0
- package/lib/helpers/indexes/isTimeseriesIndex.js +16 -0
- package/lib/helpers/isBsonType.js +3 -6
- package/lib/helpers/isMongooseDocument.js +24 -0
- package/lib/helpers/isMongooseObject.js +6 -4
- package/lib/helpers/isObject.js +2 -2
- package/lib/helpers/isPOJO.js +12 -0
- package/lib/helpers/isSimpleValidator.js +2 -2
- package/lib/helpers/minimize.js +41 -0
- package/lib/helpers/model/applyDefaultsToPOJO.js +2 -2
- package/lib/helpers/model/applyHooks.js +72 -54
- package/lib/helpers/model/applyMethods.js +3 -3
- package/lib/helpers/model/applyStaticHooks.js +20 -50
- package/lib/helpers/model/castBulkWrite.js +278 -172
- package/lib/helpers/model/decorateBulkWriteResult.js +8 -0
- package/lib/helpers/model/discriminator.js +77 -21
- package/lib/helpers/omitUndefined.js +20 -0
- package/lib/helpers/parallelLimit.js +61 -34
- package/lib/helpers/path/setDottedPath.js +1 -1
- package/lib/helpers/pathTrie.js +167 -0
- package/lib/helpers/pluralize.js +5 -4
- package/lib/helpers/populate/assignRawDocsToIdStructure.js +12 -7
- package/lib/helpers/populate/assignVals.js +46 -21
- package/lib/helpers/populate/createPopulateQueryFilter.js +13 -5
- package/lib/helpers/populate/getModelsMapForPopulate.js +250 -108
- package/lib/helpers/populate/getSchemaTypes.js +27 -30
- package/lib/helpers/populate/getVirtual.js +31 -0
- package/lib/helpers/populate/markArraySubdocsPopulated.js +6 -4
- package/lib/helpers/populate/modelNamesFromRefPath.js +11 -5
- package/lib/helpers/populate/removeDeselectedForeignField.js +3 -2
- package/lib/helpers/populate/setInheritedReadOptions.js +21 -0
- package/lib/helpers/populate/setPopulatedVirtualValue.js +33 -0
- package/lib/helpers/populate/splitPopulateQuery.js +81 -0
- package/lib/helpers/printJestWarning.js +1 -1
- package/lib/helpers/processConnectionOptions.js +7 -2
- package/lib/helpers/projection/applyProjection.js +8 -2
- package/lib/helpers/projection/hasIncludedChildren.js +1 -1
- package/lib/helpers/projection/isExclusive.js +8 -6
- package/lib/helpers/projection/isInclusive.js +2 -1
- package/lib/helpers/projection/isPathExcluded.js +3 -3
- package/lib/helpers/projection/isPathSelectedInclusive.js +1 -11
- package/lib/helpers/projection/isSubpath.js +1 -1
- package/lib/helpers/projection/parseProjection.js +9 -4
- package/lib/helpers/query/applyGlobalOption.js +9 -9
- package/lib/helpers/query/cast$expr.js +52 -23
- package/lib/helpers/query/castFilterPath.js +22 -22
- package/lib/helpers/query/castUpdate.js +207 -79
- package/lib/helpers/query/cloneMongooseOptions.js +44 -0
- package/lib/helpers/query/getEmbeddedDiscriminatorPath.js +27 -13
- package/lib/helpers/query/handleImmutable.js +18 -2
- package/lib/helpers/query/handleReadPreferenceAliases.js +23 -0
- package/lib/helpers/query/sanitizeFilter.js +3 -2
- package/lib/helpers/query/selectPopulatedFields.js +13 -0
- package/lib/helpers/query/validOps.js +1 -22
- package/lib/helpers/schema/applyPlugins.js +2 -2
- package/lib/helpers/schema/applyReadConcern.js +20 -0
- package/lib/helpers/schema/applyWriteConcern.js +12 -3
- package/lib/helpers/schema/cleanPositionalOperators.js +5 -3
- package/lib/helpers/schema/getIndexes.js +20 -13
- package/lib/helpers/schema/getKeysInSchemaOrder.js +13 -16
- package/lib/helpers/schema/getPath.js +11 -3
- package/lib/helpers/schema/getSubdocumentStrictValue.js +32 -0
- package/lib/helpers/schema/handleIdOption.js +1 -1
- package/lib/helpers/schema/idGetter.js +7 -2
- package/lib/helpers/schema/merge.js +9 -1
- package/lib/helpers/schematype/getConditionalHandler.js +23 -0
- package/lib/helpers/schematype/handleImmutable.js +1 -1
- package/lib/helpers/setDefaultsOnInsert.js +85 -43
- package/lib/helpers/symbols.js +4 -4
- package/lib/helpers/timestamps/setDocumentTimestamps.js +2 -2
- package/lib/helpers/timestamps/setupTimestamps.js +69 -37
- package/lib/helpers/update/applyTimestampsToChildren.js +1 -1
- package/lib/helpers/update/applyTimestampsToUpdate.js +53 -37
- package/lib/helpers/update/castArrayFilters.js +68 -29
- package/lib/helpers/update/decorateUpdateWithVersionKey.js +35 -0
- package/lib/helpers/update/modifiedPaths.js +2 -2
- package/lib/helpers/update/removeUnusedArrayFilters.js +11 -2
- package/lib/helpers/updateValidators.js +87 -128
- package/lib/index.js +6 -1320
- package/lib/internal.js +7 -1
- package/lib/model.js +2587 -2618
- package/lib/modifiedPathsSnapshot.js +9 -0
- package/lib/mongoose.js +1456 -0
- package/lib/options/saveOptions.js +2 -0
- package/lib/options/{SchemaArrayOptions.js → schemaArrayOptions.js} +16 -5
- package/lib/options/{SchemaBufferOptions.js → schemaBufferOptions.js} +2 -2
- package/lib/options/{SchemaDateOptions.js → schemaDateOptions.js} +1 -1
- package/lib/options/{SchemaDocumentArrayOptions.js → schemaDocumentArrayOptions.js} +24 -1
- package/lib/options/{SchemaMapOptions.js → schemaMapOptions.js} +1 -1
- package/lib/options/{SchemaNumberOptions.js → schemaNumberOptions.js} +5 -5
- package/lib/options/{SchemaObjectIdOptions.js → schemaObjectIdOptions.js} +4 -4
- package/lib/options/{SchemaStringOptions.js → schemaStringOptions.js} +10 -10
- package/lib/options/schemaSubdocumentOptions.js +89 -0
- package/lib/options/{SchemaTypeOptions.js → schemaTypeOptions.js} +28 -14
- package/lib/options/schemaUnionOptions.js +32 -0
- package/lib/options/{VirtualOptions.js → virtualOptions.js} +13 -13
- package/lib/options.js +4 -1
- package/lib/plugins/index.js +0 -2
- package/lib/plugins/saveSubdocs.js +85 -50
- package/lib/plugins/sharding.js +43 -15
- package/lib/plugins/trackTransaction.js +46 -34
- package/lib/query.js +1844 -1909
- package/lib/{queryhelpers.js → queryHelpers.js} +184 -81
- package/lib/schema/array.js +229 -169
- package/lib/schema/bigint.js +281 -0
- package/lib/schema/boolean.js +90 -26
- package/lib/schema/buffer.js +119 -50
- package/lib/schema/date.js +102 -38
- package/lib/schema/decimal128.js +88 -37
- package/lib/schema/{documentarray.js → documentArray.js} +259 -151
- package/lib/schema/documentArrayElement.js +180 -0
- package/lib/schema/double.js +244 -0
- package/lib/schema/index.js +12 -19
- package/lib/schema/int32.js +288 -0
- package/lib/schema/map.js +129 -20
- package/lib/schema/mixed.js +40 -26
- package/lib/schema/number.js +128 -51
- package/lib/schema/{objectid.js → objectId.js} +89 -47
- package/lib/schema/operators/exists.js +1 -1
- package/lib/schema/operators/geospatial.js +2 -2
- package/lib/schema/operators/text.js +4 -4
- package/lib/schema/string.js +125 -66
- package/lib/schema/subdocument.js +443 -0
- package/lib/schema/symbols.js +2 -0
- package/lib/schema/union.js +167 -0
- package/lib/schema/uuid.js +90 -120
- package/lib/schema.js +1267 -388
- package/lib/{schematype.js → schemaType.js} +481 -246
- package/lib/standardSchema/convertErrorToIssues.js +20 -0
- package/lib/{statemachine.js → stateMachine.js} +66 -17
- package/lib/tracing.js +38 -0
- package/lib/types/array/index.js +13 -10
- package/lib/types/array/methods/index.js +243 -63
- package/lib/types/{ArraySubdocument.js → arraySubdocument.js} +35 -14
- package/lib/types/buffer.js +31 -14
- package/lib/types/decimal128.js +1 -1
- package/lib/types/{DocumentArray → documentArray}/index.js +13 -13
- package/lib/types/{DocumentArray → documentArray}/methods/index.js +197 -31
- package/lib/types/double.js +13 -0
- package/lib/types/index.js +5 -2
- package/lib/types/map.js +62 -25
- package/lib/types/objectid.js +2 -2
- package/lib/types/subdocument.js +79 -58
- package/lib/types/uuid.js +13 -0
- package/lib/utils.js +201 -155
- package/lib/{validoptions.js → validOptions.js} +10 -2
- package/lib/{virtualtype.js → virtualType.js} +47 -18
- package/package.json +75 -92
- package/types/aggregate.d.ts +31 -7
- package/types/augmentations.d.ts +2 -2
- package/types/collection.d.ts +21 -16
- package/types/connection.d.ts +82 -27
- package/types/cursor.d.ts +15 -10
- package/types/document.d.ts +137 -56
- package/types/error.d.ts +11 -6
- package/types/expressions.d.ts +33 -2
- package/types/index.d.ts +805 -190
- package/types/indexes.d.ts +85 -13
- package/types/inferhydrateddoctype.d.ts +151 -0
- package/types/inferrawdoctype.d.ts +201 -0
- package/types/inferschematype.d.ts +287 -128
- package/types/middlewares.d.ts +50 -7
- package/types/models.d.ts +1555 -268
- package/types/mongooseoptions.d.ts +56 -7
- package/types/pipelinestage.d.ts +42 -3
- package/types/populate.d.ts +63 -1
- package/types/query.d.ts +592 -255
- package/types/schemaoptions.d.ts +78 -12
- package/types/schematypes.d.ts +254 -65
- package/types/session.d.ts +0 -4
- package/types/tracing.d.ts +10 -0
- package/types/types.d.ts +25 -8
- package/types/utility.d.ts +329 -30
- package/types/validation.d.ts +19 -14
- package/types/virtuals.d.ts +10 -10
- package/.eslintrc.js +0 -232
- package/.mocharc.yml +0 -4
- package/browser.js +0 -8
- package/lgtm.yml +0 -12
- package/lib/browser.js +0 -158
- package/lib/browserDocument.js +0 -101
- package/lib/cursor/ChangeStream.js +0 -154
- package/lib/cursor/QueryCursor.js +0 -545
- package/lib/document_provider.js +0 -30
- package/lib/drivers/browser/ReadPreference.js +0 -7
- package/lib/drivers/browser/binary.js +0 -14
- package/lib/drivers/browser/decimal128.js +0 -7
- package/lib/drivers/browser/index.js +0 -16
- package/lib/drivers/browser/objectid.js +0 -29
- package/lib/drivers/node-mongodb-native/ReadPreference.js +0 -47
- package/lib/drivers/node-mongodb-native/binary.js +0 -10
- package/lib/drivers/node-mongodb-native/decimal128.js +0 -7
- package/lib/drivers/node-mongodb-native/objectid.js +0 -16
- package/lib/error/disconnected.js +0 -33
- package/lib/helpers/printStrictQueryWarning.js +0 -11
- package/lib/helpers/promiseOrCallback.js +0 -55
- package/lib/helpers/query/applyQueryMiddleware.js +0 -79
- package/lib/helpers/query/completeMany.js +0 -52
- package/lib/helpers/query/wrapThunk.js +0 -36
- package/lib/options/SchemaSubdocumentOptions.js +0 -42
- package/lib/options/removeOptions.js +0 -14
- package/lib/plugins/removeSubdocs.js +0 -31
- package/lib/plugins/validateBeforeSave.js +0 -45
- package/lib/promise_provider.js +0 -49
- package/lib/schema/DocumentArrayElement.js +0 -100
- package/lib/schema/SubdocumentPath.js +0 -369
- package/scripts/build-browser.js +0 -18
- package/scripts/create-tarball.js +0 -7
- package/scripts/generateSearch.js +0 -154
- package/scripts/tsc-diagnostics-check.js +0 -15
- package/tools/auth.js +0 -31
- package/tools/repl.js +0 -35
- package/tools/sharded.js +0 -46
- package/tsconfig.json +0 -9
- /package/lib/{connectionstate.js → connectionState.js} +0 -0
- /package/lib/helpers/populate/{SkipPopulateValue.js → skipPopulateValue.js} +0 -0
- /package/lib/options/{PopulateOptions.js → populateOptions.js} +0 -0
- /package/lib/types/{DocumentArray → documentArray}/isMongooseDocumentArray.js +0 -0
package/README.md
CHANGED
|
@@ -11,28 +11,28 @@ Mongoose is a [MongoDB](https://www.mongodb.org/) object modeling tool designed
|
|
|
11
11
|
|
|
12
12
|
## Documentation
|
|
13
13
|
|
|
14
|
-
The official documentation website is [mongoosejs.com](
|
|
14
|
+
The official documentation website is [mongoosejs.com](https://mongoosejs.com/).
|
|
15
15
|
|
|
16
|
-
Mongoose
|
|
16
|
+
Mongoose 9.0.0 was released on November 21, 2025. You can find more details on [backwards breaking changes in 9.0.0 on our docs site](https://mongoosejs.com/docs/migrating_to_9.html).
|
|
17
17
|
|
|
18
18
|
## Support
|
|
19
19
|
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
20
|
+
* [Stack Overflow](http://stackoverflow.com/questions/tagged/mongoose)
|
|
21
|
+
* [Bug Reports](https://github.com/Automattic/mongoose/issues/)
|
|
22
|
+
* [Mongoose Slack Channel](http://slack.mongoosejs.io/)
|
|
23
|
+
* [Help Forum](http://groups.google.com/group/mongoose-orm)
|
|
24
|
+
* [MongoDB Support](https://www.mongodb.com/docs/manual/support/)
|
|
25
25
|
|
|
26
26
|
## Plugins
|
|
27
27
|
|
|
28
|
-
Check out the [plugins search site](
|
|
28
|
+
Check out the [plugins search site](https://plugins.mongoosejs.io/) to see hundreds of related modules from the community. Next, learn how to write your own plugin from the [docs](https://mongoosejs.com/docs/plugins.html) or [this blog post](http://thecodebarbarian.com/2015/03/06/guide-to-mongoose-plugins).
|
|
29
29
|
|
|
30
30
|
## Contributors
|
|
31
31
|
|
|
32
32
|
Pull requests are always welcome! Please base pull requests against the `master`
|
|
33
33
|
branch and follow the [contributing guide](https://github.com/Automattic/mongoose/blob/master/CONTRIBUTING.md).
|
|
34
34
|
|
|
35
|
-
If your pull
|
|
35
|
+
If your pull request makes documentation changes, please do **not**
|
|
36
36
|
modify any `.html` files. The `.html` files are compiled code, so please make
|
|
37
37
|
your changes in `docs/*.pug`, `lib/*.js`, or `test/docs/*.js`.
|
|
38
38
|
|
|
@@ -40,10 +40,30 @@ View all 400+ [contributors](https://github.com/Automattic/mongoose/graphs/contr
|
|
|
40
40
|
|
|
41
41
|
## Installation
|
|
42
42
|
|
|
43
|
-
First install [Node.js](
|
|
43
|
+
First install [Node.js](https://nodejs.org/) and [MongoDB](https://www.mongodb.org/downloads), then install the `mongoose` package using your preferred package manager:
|
|
44
|
+
|
|
45
|
+
### Using npm
|
|
46
|
+
|
|
47
|
+
```sh
|
|
48
|
+
npm install mongoose
|
|
49
|
+
```
|
|
50
|
+
|
|
51
|
+
### Using pnpm
|
|
52
|
+
|
|
53
|
+
```sh
|
|
54
|
+
pnpm add mongoose
|
|
55
|
+
```
|
|
56
|
+
|
|
57
|
+
### Using Yarn
|
|
58
|
+
|
|
59
|
+
```sh
|
|
60
|
+
yarn add mongoose
|
|
61
|
+
```
|
|
62
|
+
|
|
63
|
+
### Using Bun
|
|
44
64
|
|
|
45
65
|
```sh
|
|
46
|
-
|
|
66
|
+
bun add mongoose
|
|
47
67
|
```
|
|
48
68
|
|
|
49
69
|
Mongoose 6.8.0 also includes alpha support for [Deno](https://deno.land/).
|
|
@@ -61,7 +81,7 @@ import mongoose from 'mongoose';
|
|
|
61
81
|
Or, using [Deno's `createRequire()` for CommonJS support](https://deno.land/std@0.113.0/node/README.md?source=#commonjs-modules-loading) as follows.
|
|
62
82
|
|
|
63
83
|
```javascript
|
|
64
|
-
import { createRequire } from 'https://deno.land/std/node/module.ts';
|
|
84
|
+
import { createRequire } from 'https://deno.land/std@0.177.0/node/module.ts';
|
|
65
85
|
const require = createRequire(import.meta.url);
|
|
66
86
|
|
|
67
87
|
const mongoose = require('mongoose');
|
|
@@ -72,10 +92,14 @@ mongoose.connect('mongodb://127.0.0.1:27017/test')
|
|
|
72
92
|
|
|
73
93
|
You can then run the above script using the following.
|
|
74
94
|
|
|
75
|
-
```
|
|
95
|
+
```sh
|
|
76
96
|
deno run --allow-net --allow-read --allow-sys --allow-env mongoose-test.js
|
|
77
97
|
```
|
|
78
98
|
|
|
99
|
+
## Mongoose Studio
|
|
100
|
+
|
|
101
|
+
[Mongoose Studio](https://mongoosestudio.app?utm_source=mongoose&utm_medium=referral&utm_campaign=readme) is a free, fully open-source, browser-based MongoDB GUI built by the Mongoose team for apps that already use Mongoose. Install `@mongoosejs/studio` from npm and run it alongside your app as Express middleware, or deploy it on Vercel or Netlify, to browse and edit documents, query with your existing models and schemas with autocomplete, build dashboards, visualize and edit GeoJSON, and use AI-assisted MongoDB workflows without moving data into a hosted third-party workspace or sharing raw MongoDB connection strings.
|
|
102
|
+
|
|
79
103
|
## Mongoose for Enterprise
|
|
80
104
|
|
|
81
105
|
Available as part of the Tidelift Subscription
|
|
@@ -96,7 +120,7 @@ await mongoose.connect('mongodb://127.0.0.1/my_database');
|
|
|
96
120
|
|
|
97
121
|
Once connected, the `open` event is fired on the `Connection` instance. If you're using `mongoose.connect`, the `Connection` is `mongoose.connection`. Otherwise, `mongoose.createConnection` return value is a `Connection`.
|
|
98
122
|
|
|
99
|
-
**Note:**
|
|
123
|
+
**Note:** *If the local connection fails then try using 127.0.0.1 instead of localhost. Sometimes issues may arise when the local hostname has been changed.*
|
|
100
124
|
|
|
101
125
|
**Important!** Mongoose buffers all the commands until it's connected to the database. This means that you don't have to wait until it connects to MongoDB in order to define models, run queries, etc.
|
|
102
126
|
|
|
@@ -118,16 +142,16 @@ const BlogPost = new Schema({
|
|
|
118
142
|
|
|
119
143
|
Aside from defining the structure of your documents and the types of data you're storing, a Schema handles the definition of:
|
|
120
144
|
|
|
121
|
-
* [Validators](
|
|
122
|
-
* [Defaults](
|
|
123
|
-
* [Getters](
|
|
124
|
-
* [Setters](
|
|
125
|
-
* [Indexes](
|
|
126
|
-
* [Middleware](
|
|
127
|
-
* [Methods](
|
|
128
|
-
* [Statics](
|
|
129
|
-
* [Plugins](
|
|
130
|
-
* [pseudo-JOINs](
|
|
145
|
+
* [Validators](https://mongoosejs.com/docs/validation.html) (async and sync)
|
|
146
|
+
* [Defaults](https://mongoosejs.com/docs/api/schematype.html#schematype_SchemaType-default)
|
|
147
|
+
* [Getters](https://mongoosejs.com/docs/api/schematype.html#schematype_SchemaType-get)
|
|
148
|
+
* [Setters](https://mongoosejs.com/docs/api/schematype.html#schematype_SchemaType-set)
|
|
149
|
+
* [Indexes](https://mongoosejs.com/docs/guide.html#indexes)
|
|
150
|
+
* [Middleware](https://mongoosejs.com/docs/middleware.html)
|
|
151
|
+
* [Methods](https://mongoosejs.com/docs/guide.html#methods) definition
|
|
152
|
+
* [Statics](https://mongoosejs.com/docs/guide.html#statics) definition
|
|
153
|
+
* [Plugins](https://mongoosejs.com/docs/plugins.html)
|
|
154
|
+
* [pseudo-JOINs](https://mongoosejs.com/docs/populate.html)
|
|
131
155
|
|
|
132
156
|
The following example shows some of these features:
|
|
133
157
|
|
|
@@ -152,8 +176,6 @@ Comment.pre('save', function(next) {
|
|
|
152
176
|
});
|
|
153
177
|
```
|
|
154
178
|
|
|
155
|
-
Take a look at the example in [`examples/schema/schema.js`](https://github.com/Automattic/mongoose/blob/master/examples/schema/schema.js) for an end-to-end example of a typical setup.
|
|
156
|
-
|
|
157
179
|
### Accessing a Model
|
|
158
180
|
|
|
159
181
|
Once we define a model through `mongoose.model('ModelName', mySchema)`, we can access it through the same function
|
|
@@ -168,30 +190,26 @@ Or just do it all at once
|
|
|
168
190
|
const MyModel = mongoose.model('ModelName', mySchema);
|
|
169
191
|
```
|
|
170
192
|
|
|
171
|
-
The first argument is the
|
|
193
|
+
The first argument is the *singular* name of the collection your model is for. **Mongoose automatically looks for the *plural* version of your model name.** For example, if you use
|
|
172
194
|
|
|
173
195
|
```js
|
|
174
196
|
const MyModel = mongoose.model('Ticket', mySchema);
|
|
175
197
|
```
|
|
176
198
|
|
|
177
|
-
Then `MyModel` will use the
|
|
199
|
+
Then `MyModel` will use the **tickets** collection, not the **ticket** collection. For more details read the [model docs](https://mongoosejs.com/docs/api/mongoose.html#mongoose_Mongoose-model).
|
|
178
200
|
|
|
179
201
|
Once we have our model, we can then instantiate it, and save it:
|
|
180
202
|
|
|
181
203
|
```js
|
|
182
204
|
const instance = new MyModel();
|
|
183
205
|
instance.my.key = 'hello';
|
|
184
|
-
instance.save(
|
|
185
|
-
//
|
|
186
|
-
});
|
|
206
|
+
await instance.save();
|
|
187
207
|
```
|
|
188
208
|
|
|
189
209
|
Or we can find documents from the same collection
|
|
190
210
|
|
|
191
211
|
```js
|
|
192
|
-
MyModel.find({}
|
|
193
|
-
// docs.forEach
|
|
194
|
-
});
|
|
212
|
+
await MyModel.find({});
|
|
195
213
|
```
|
|
196
214
|
|
|
197
215
|
You can also `findOne`, `findById`, `update`, etc.
|
|
@@ -201,15 +219,15 @@ const instance = await MyModel.findOne({ /* ... */ });
|
|
|
201
219
|
console.log(instance.my.key); // 'hello'
|
|
202
220
|
```
|
|
203
221
|
|
|
204
|
-
For more details check out [the docs](
|
|
222
|
+
For more details check out [the docs](https://mongoosejs.com/docs/queries.html).
|
|
205
223
|
|
|
206
224
|
**Important!** If you opened a separate connection using `mongoose.createConnection()` but attempt to access the model through `mongoose.model('ModelName')` it will not work as expected since it is not hooked up to an active db connection. In this case access your model through the connection you created:
|
|
207
225
|
|
|
208
226
|
```js
|
|
209
227
|
const conn = mongoose.createConnection('your connection string');
|
|
210
228
|
const MyModel = conn.model('ModelName', schema);
|
|
211
|
-
const m = new MyModel;
|
|
212
|
-
m.save(); // works
|
|
229
|
+
const m = new MyModel();
|
|
230
|
+
await m.save(); // works
|
|
213
231
|
```
|
|
214
232
|
|
|
215
233
|
vs
|
|
@@ -217,15 +235,15 @@ vs
|
|
|
217
235
|
```js
|
|
218
236
|
const conn = mongoose.createConnection('your connection string');
|
|
219
237
|
const MyModel = mongoose.model('ModelName', schema);
|
|
220
|
-
const m = new MyModel;
|
|
221
|
-
m.save(); // does not work b/c the default connection object was never connected
|
|
238
|
+
const m = new MyModel();
|
|
239
|
+
await m.save(); // does not work b/c the default connection object was never connected
|
|
222
240
|
```
|
|
223
241
|
|
|
224
242
|
### Embedded Documents
|
|
225
243
|
|
|
226
244
|
In the first example snippet, we defined a key in the Schema that looks like:
|
|
227
245
|
|
|
228
|
-
```
|
|
246
|
+
```txt
|
|
229
247
|
comments: [Comment]
|
|
230
248
|
```
|
|
231
249
|
|
|
@@ -241,30 +259,22 @@ const post = new BlogPost();
|
|
|
241
259
|
// create a comment
|
|
242
260
|
post.comments.push({ title: 'My comment' });
|
|
243
261
|
|
|
244
|
-
post.save(
|
|
245
|
-
if (!err) console.log('Success!');
|
|
246
|
-
});
|
|
262
|
+
await post.save();
|
|
247
263
|
```
|
|
248
264
|
|
|
249
265
|
The same goes for removing them:
|
|
250
266
|
|
|
251
267
|
```js
|
|
252
|
-
BlogPost.findById(myId
|
|
253
|
-
|
|
254
|
-
|
|
255
|
-
post.save(function(err) {
|
|
256
|
-
// do something
|
|
257
|
-
});
|
|
258
|
-
}
|
|
259
|
-
});
|
|
268
|
+
const post = await BlogPost.findById(myId);
|
|
269
|
+
post.comments[0].deleteOne();
|
|
270
|
+
await post.save();
|
|
260
271
|
```
|
|
261
272
|
|
|
262
|
-
Embedded documents enjoy all the same features as your models. Defaults, validators, middleware.
|
|
263
|
-
|
|
273
|
+
Embedded documents enjoy all the same features as your models. Defaults, validators, middleware.
|
|
264
274
|
|
|
265
275
|
### Middleware
|
|
266
276
|
|
|
267
|
-
See the [docs](
|
|
277
|
+
See the [docs](https://mongoosejs.com/docs/middleware.html) page.
|
|
268
278
|
|
|
269
279
|
#### Intercepting and mutating method arguments
|
|
270
280
|
|
|
@@ -330,38 +340,38 @@ new Schema({
|
|
|
330
340
|
|
|
331
341
|
### Driver Access
|
|
332
342
|
|
|
333
|
-
Mongoose is built on top of the [official MongoDB Node.js driver](https://github.com/mongodb/node-mongodb-native). Each mongoose model keeps a reference to a [native MongoDB driver collection](
|
|
334
|
-
notable exception that `YourModel.collection` still buffers
|
|
343
|
+
Mongoose is built on top of the [official MongoDB Node.js driver](https://github.com/mongodb/node-mongodb-native). Each mongoose model keeps a reference to a [native MongoDB driver collection](https://mongodb.github.io/node-mongodb-native/7.5/classes/Collection.html). The collection object can be accessed using `YourModel.collection`. However, using the collection object directly bypasses all mongoose features, including hooks, validation, etc. The one
|
|
344
|
+
notable exception is that `YourModel.collection` still buffers
|
|
335
345
|
commands. As such, `YourModel.collection.find()` will **not**
|
|
336
346
|
return a cursor.
|
|
337
347
|
|
|
338
348
|
## API Docs
|
|
339
349
|
|
|
340
|
-
|
|
350
|
+
[Mongoose API documentation](https://mongoosejs.com/docs/api/mongoose.html), generated using [dox](https://github.com/tj/dox)
|
|
341
351
|
and [acquit](https://github.com/vkarpov15/acquit).
|
|
342
352
|
|
|
343
353
|
## Related Projects
|
|
344
354
|
|
|
345
|
-
|
|
355
|
+
### MongoDB Runners
|
|
346
356
|
|
|
347
|
-
|
|
348
|
-
|
|
349
|
-
|
|
357
|
+
* [run-rs](https://www.npmjs.com/package/run-rs)
|
|
358
|
+
* [mongodb-memory-server](https://www.npmjs.com/package/mongodb-memory-server)
|
|
359
|
+
* [mongodb-topology-manager](https://www.npmjs.com/package/mongodb-topology-manager)
|
|
350
360
|
|
|
351
|
-
|
|
361
|
+
### Unofficial CLIs
|
|
352
362
|
|
|
353
|
-
|
|
363
|
+
* [mongoosejs-cli](https://www.npmjs.com/package/mongoosejs-cli)
|
|
354
364
|
|
|
355
|
-
|
|
365
|
+
### Data Seeding
|
|
356
366
|
|
|
357
|
-
|
|
358
|
-
|
|
359
|
-
|
|
367
|
+
* [dookie](https://www.npmjs.com/package/dookie)
|
|
368
|
+
* [seedgoose](https://www.npmjs.com/package/seedgoose)
|
|
369
|
+
* [mongoose-data-seed](https://www.npmjs.com/package/mongoose-data-seed)
|
|
360
370
|
|
|
361
|
-
|
|
371
|
+
### Express Session Stores
|
|
362
372
|
|
|
363
|
-
|
|
364
|
-
|
|
373
|
+
* [connect-mongodb-session](https://www.npmjs.com/package/connect-mongodb-session)
|
|
374
|
+
* [connect-mongo](https://www.npmjs.com/package/connect-mongo)
|
|
365
375
|
|
|
366
376
|
## License
|
|
367
377
|
|
|
@@ -0,0 +1,202 @@
|
|
|
1
|
+
import { defineConfig, globalIgnores } from 'eslint/config';
|
|
2
|
+
import mochaNoOnly from 'eslint-plugin-mocha-no-only';
|
|
3
|
+
import globals from 'globals';
|
|
4
|
+
import tseslint from 'typescript-eslint';
|
|
5
|
+
import js from '@eslint/js';
|
|
6
|
+
|
|
7
|
+
export default defineConfig([
|
|
8
|
+
globalIgnores([
|
|
9
|
+
'**/tools',
|
|
10
|
+
'**/dist',
|
|
11
|
+
'test/files/*',
|
|
12
|
+
'**/benchmarks',
|
|
13
|
+
'**/*.min.js',
|
|
14
|
+
'**/docs/js/native.js',
|
|
15
|
+
'!**/.*',
|
|
16
|
+
'**/node_modules',
|
|
17
|
+
'**/.git',
|
|
18
|
+
'**/data',
|
|
19
|
+
'**/docs/3.*/**',
|
|
20
|
+
'**/docs/5.*/**',
|
|
21
|
+
'**/docs/vendor/*'
|
|
22
|
+
]),
|
|
23
|
+
js.configs.recommended,
|
|
24
|
+
// general options
|
|
25
|
+
{
|
|
26
|
+
languageOptions: {
|
|
27
|
+
globals: globals.node,
|
|
28
|
+
ecmaVersion: 2022, // nodejs 18.0.0,
|
|
29
|
+
sourceType: 'commonjs'
|
|
30
|
+
},
|
|
31
|
+
rules: {
|
|
32
|
+
'comma-style': 'error',
|
|
33
|
+
|
|
34
|
+
indent: ['error', 2, {
|
|
35
|
+
SwitchCase: 1,
|
|
36
|
+
VariableDeclarator: 2
|
|
37
|
+
}],
|
|
38
|
+
|
|
39
|
+
'keyword-spacing': 'error',
|
|
40
|
+
'no-whitespace-before-property': 'error',
|
|
41
|
+
'no-buffer-constructor': 'warn',
|
|
42
|
+
'no-console': 'off',
|
|
43
|
+
'no-constant-condition': 'off',
|
|
44
|
+
'no-multi-spaces': 'error',
|
|
45
|
+
'func-call-spacing': 'error',
|
|
46
|
+
'no-unused-vars': ['error', { ignoreRestSiblings: true }],
|
|
47
|
+
'no-trailing-spaces': 'error',
|
|
48
|
+
'no-undef': 'error',
|
|
49
|
+
'no-unneeded-ternary': 'error',
|
|
50
|
+
'no-const-assign': 'error',
|
|
51
|
+
'no-useless-rename': 'error',
|
|
52
|
+
'no-dupe-keys': 'error',
|
|
53
|
+
'space-in-parens': ['error', 'never'],
|
|
54
|
+
|
|
55
|
+
'spaced-comment': ['error', 'always', {
|
|
56
|
+
block: {
|
|
57
|
+
markers: ['!'],
|
|
58
|
+
balanced: true
|
|
59
|
+
}
|
|
60
|
+
}],
|
|
61
|
+
|
|
62
|
+
'key-spacing': ['error', {
|
|
63
|
+
beforeColon: false,
|
|
64
|
+
afterColon: true
|
|
65
|
+
}],
|
|
66
|
+
|
|
67
|
+
'comma-spacing': ['error', {
|
|
68
|
+
before: false,
|
|
69
|
+
after: true
|
|
70
|
+
}],
|
|
71
|
+
|
|
72
|
+
'array-bracket-spacing': 1,
|
|
73
|
+
|
|
74
|
+
'arrow-spacing': ['error', {
|
|
75
|
+
before: true,
|
|
76
|
+
after: true
|
|
77
|
+
}],
|
|
78
|
+
|
|
79
|
+
'object-curly-spacing': ['error', 'always'],
|
|
80
|
+
'comma-dangle': ['error', 'never'],
|
|
81
|
+
'no-unreachable': 'error',
|
|
82
|
+
quotes: ['error', 'single'],
|
|
83
|
+
'quote-props': ['error', 'as-needed'],
|
|
84
|
+
semi: 'error',
|
|
85
|
+
'no-extra-semi': 'error',
|
|
86
|
+
'semi-spacing': 'error',
|
|
87
|
+
'no-spaced-func': 'error',
|
|
88
|
+
'no-throw-literal': 'error',
|
|
89
|
+
'space-before-blocks': 'error',
|
|
90
|
+
'space-before-function-paren': ['error', 'never'],
|
|
91
|
+
'space-infix-ops': 'error',
|
|
92
|
+
'space-unary-ops': 'error',
|
|
93
|
+
'no-var': 'warn',
|
|
94
|
+
'prefer-const': 'warn',
|
|
95
|
+
strict: ['error', 'global'],
|
|
96
|
+
|
|
97
|
+
'no-restricted-globals': ['error', {
|
|
98
|
+
name: 'context',
|
|
99
|
+
message: 'Don\'t use Mocha\'s global context'
|
|
100
|
+
}],
|
|
101
|
+
|
|
102
|
+
'no-prototype-builtins': 'off',
|
|
103
|
+
'no-empty': 'off',
|
|
104
|
+
'eol-last': 'warn',
|
|
105
|
+
|
|
106
|
+
'no-multiple-empty-lines': ['warn', {
|
|
107
|
+
max: 2
|
|
108
|
+
}]
|
|
109
|
+
}
|
|
110
|
+
},
|
|
111
|
+
// general typescript options
|
|
112
|
+
{
|
|
113
|
+
files: ['**/*.{ts,tsx}', '**/*.md/*.ts', '**/*.md/*.typescript'],
|
|
114
|
+
extends: [
|
|
115
|
+
tseslint.configs.recommended
|
|
116
|
+
],
|
|
117
|
+
languageOptions: {
|
|
118
|
+
parserOptions: {
|
|
119
|
+
projectService: {
|
|
120
|
+
allowDefaultProject: [],
|
|
121
|
+
defaultProject: 'tsconfig.json'
|
|
122
|
+
}
|
|
123
|
+
}
|
|
124
|
+
},
|
|
125
|
+
rules: {
|
|
126
|
+
'@typescript-eslint/triple-slash-reference': 'off',
|
|
127
|
+
'@typescript-eslint/no-non-null-assertion': 'off',
|
|
128
|
+
'@typescript-eslint/no-empty-function': 'off',
|
|
129
|
+
|
|
130
|
+
'spaced-comment': ['error', 'always', {
|
|
131
|
+
block: {
|
|
132
|
+
markers: ['!'],
|
|
133
|
+
balanced: true
|
|
134
|
+
},
|
|
135
|
+
|
|
136
|
+
markers: ['/']
|
|
137
|
+
}],
|
|
138
|
+
|
|
139
|
+
'@typescript-eslint/no-explicit-any': 'off',
|
|
140
|
+
'@typescript-eslint/ban-types': 'off',
|
|
141
|
+
'@typescript-eslint/no-unused-vars': 'off',
|
|
142
|
+
'@typescript-eslint/explicit-module-boundary-types': 'off',
|
|
143
|
+
'@typescript-eslint/prefer-optional-chain': 'error',
|
|
144
|
+
'@typescript-eslint/no-dupe-class-members': 'error',
|
|
145
|
+
'@typescript-eslint/no-redeclare': 'error',
|
|
146
|
+
'@typescript-eslint/space-infix-ops': 'off',
|
|
147
|
+
'@typescript-eslint/no-require-imports': 'off',
|
|
148
|
+
'@typescript-eslint/no-empty-object-type': 'off',
|
|
149
|
+
'@typescript-eslint/no-wrapper-object-types': 'off',
|
|
150
|
+
'@typescript-eslint/no-unused-expressions': 'off',
|
|
151
|
+
'@typescript-eslint/no-unsafe-function-type': 'off'
|
|
152
|
+
}
|
|
153
|
+
},
|
|
154
|
+
// type test specific options
|
|
155
|
+
{
|
|
156
|
+
files: ['test/types/**/*.ts'],
|
|
157
|
+
rules: {
|
|
158
|
+
'@typescript-eslint/no-empty-interface': 'off'
|
|
159
|
+
}
|
|
160
|
+
},
|
|
161
|
+
// test specific options (including type tests)
|
|
162
|
+
{
|
|
163
|
+
files: ['test/**/*.js', 'test/**/*.ts'],
|
|
164
|
+
ignores: ['deno*.mjs'],
|
|
165
|
+
plugins: {
|
|
166
|
+
'mocha-no-only': mochaNoOnly
|
|
167
|
+
},
|
|
168
|
+
languageOptions: {
|
|
169
|
+
globals: globals.mocha
|
|
170
|
+
},
|
|
171
|
+
rules: {
|
|
172
|
+
'no-self-assign': 'off',
|
|
173
|
+
'mocha-no-only/mocha-no-only': ['error']
|
|
174
|
+
}
|
|
175
|
+
},
|
|
176
|
+
// deno specific options
|
|
177
|
+
{
|
|
178
|
+
files: ['**/deno*.mjs'],
|
|
179
|
+
languageOptions: {
|
|
180
|
+
globals: {
|
|
181
|
+
// "globals" currently has no definition for deno
|
|
182
|
+
Deno: 'readonly'
|
|
183
|
+
}
|
|
184
|
+
}
|
|
185
|
+
},
|
|
186
|
+
// general options for module files
|
|
187
|
+
{
|
|
188
|
+
files: ['**/*.mjs'],
|
|
189
|
+
languageOptions: {
|
|
190
|
+
sourceType: 'module'
|
|
191
|
+
}
|
|
192
|
+
},
|
|
193
|
+
// doc script specific options
|
|
194
|
+
{
|
|
195
|
+
files: ['**/docs/js/**/*.js'],
|
|
196
|
+
languageOptions: {
|
|
197
|
+
globals: {
|
|
198
|
+
...Object.fromEntries(Object.entries(globals.node).map(([key]) => [key, 'off'])),
|
|
199
|
+
...globals.browser }
|
|
200
|
+
}
|
|
201
|
+
}
|
|
202
|
+
]);
|
package/index.js
CHANGED
|
@@ -28,6 +28,7 @@ module.exports.modelNames = mongoose.modelNames;
|
|
|
28
28
|
module.exports.plugin = mongoose.plugin;
|
|
29
29
|
module.exports.connections = mongoose.connections;
|
|
30
30
|
module.exports.version = mongoose.version;
|
|
31
|
+
module.exports.Aggregate = mongoose.Aggregate;
|
|
31
32
|
module.exports.Mongoose = mongoose.Mongoose;
|
|
32
33
|
module.exports.Schema = mongoose.Schema;
|
|
33
34
|
module.exports.SchemaType = mongoose.SchemaType;
|
|
@@ -35,7 +36,6 @@ module.exports.SchemaTypes = mongoose.SchemaTypes;
|
|
|
35
36
|
module.exports.VirtualType = mongoose.VirtualType;
|
|
36
37
|
module.exports.Types = mongoose.Types;
|
|
37
38
|
module.exports.Query = mongoose.Query;
|
|
38
|
-
module.exports.Promise = mongoose.Promise;
|
|
39
39
|
module.exports.Model = mongoose.Model;
|
|
40
40
|
module.exports.Document = mongoose.Document;
|
|
41
41
|
module.exports.ObjectId = mongoose.ObjectId;
|
|
@@ -47,6 +47,7 @@ module.exports.Mixed = mongoose.Mixed;
|
|
|
47
47
|
module.exports.Date = mongoose.Date;
|
|
48
48
|
module.exports.Number = mongoose.Number;
|
|
49
49
|
module.exports.Error = mongoose.Error;
|
|
50
|
+
module.exports.MongooseError = mongoose.MongooseError;
|
|
50
51
|
module.exports.now = mongoose.now;
|
|
51
52
|
module.exports.CastError = mongoose.CastError;
|
|
52
53
|
module.exports.SchemaTypeOptions = mongoose.SchemaTypeOptions;
|