@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.
Files changed (302) hide show
  1. package/README.md +80 -70
  2. package/eslint.config.mjs +202 -0
  3. package/index.js +2 -1
  4. package/lib/aggregate.js +276 -174
  5. package/lib/cast/bigint.js +46 -0
  6. package/lib/cast/boolean.js +3 -3
  7. package/lib/cast/decimal128.js +4 -1
  8. package/lib/cast/double.js +56 -0
  9. package/lib/cast/int32.js +42 -0
  10. package/lib/cast/number.js +6 -6
  11. package/lib/cast/objectid.js +3 -3
  12. package/lib/cast/string.js +3 -3
  13. package/lib/cast/uuid.js +35 -0
  14. package/lib/cast.js +100 -92
  15. package/lib/collection.js +31 -22
  16. package/lib/connection.js +792 -507
  17. package/lib/constants.js +73 -0
  18. package/lib/cursor/{AggregationCursor.js → aggregationCursor.js} +205 -52
  19. package/lib/cursor/changeStream.js +218 -0
  20. package/lib/cursor/queryCursor.js +676 -0
  21. package/lib/defaults/schemaDefaultsCache.js +62 -0
  22. package/lib/document.js +2419 -1306
  23. package/lib/drivers/node-mongodb-native/bulkWriteResult.js +5 -0
  24. package/lib/drivers/node-mongodb-native/collection.js +101 -135
  25. package/lib/drivers/node-mongodb-native/connection.js +386 -28
  26. package/lib/drivers/node-mongodb-native/index.js +3 -5
  27. package/lib/error/browserMissingSchema.js +5 -4
  28. package/lib/error/bulkSaveIncompleteError.js +44 -0
  29. package/lib/error/cast.js +30 -21
  30. package/lib/error/createCollectionsError.js +26 -0
  31. package/lib/error/divergentArray.js +11 -9
  32. package/lib/error/eachAsyncMultiError.js +2 -2
  33. package/lib/error/index.js +38 -29
  34. package/lib/error/invalidSchemaOption.js +32 -0
  35. package/lib/error/messages.js +4 -3
  36. package/lib/error/missingSchema.js +8 -6
  37. package/lib/error/notFound.js +7 -5
  38. package/lib/error/objectExpected.js +9 -8
  39. package/lib/error/objectParameter.js +12 -11
  40. package/lib/error/overwriteModel.js +7 -6
  41. package/lib/error/parallelSave.js +11 -8
  42. package/lib/error/parallelValidate.js +9 -7
  43. package/lib/error/serverSelection.js +1 -0
  44. package/lib/error/setOptionError.js +11 -9
  45. package/lib/error/strict.js +12 -10
  46. package/lib/error/strictPopulate.js +11 -9
  47. package/lib/error/syncIndexes.js +2 -2
  48. package/lib/error/validation.js +11 -17
  49. package/lib/error/validator.js +9 -8
  50. package/lib/error/version.js +12 -10
  51. package/lib/filters/operators.js +8 -0
  52. package/lib/helpers/aggregate/prepareDiscriminatorPipeline.js +4 -4
  53. package/lib/helpers/buildMiddlewareFilter.js +42 -0
  54. package/lib/helpers/clone.js +121 -35
  55. package/lib/helpers/common.js +7 -7
  56. package/lib/helpers/cursor/eachAsync.js +14 -11
  57. package/lib/helpers/cursor/waitForInitialization.js +17 -0
  58. package/lib/helpers/discriminator/applyEmbeddedDiscriminators.js +36 -0
  59. package/lib/helpers/discriminator/areDiscriminatorValuesEqual.js +1 -1
  60. package/lib/helpers/discriminator/getConstructor.js +9 -6
  61. package/lib/helpers/discriminator/getDiscriminatorByKey.js +1 -1
  62. package/lib/helpers/discriminator/getDiscriminatorByValue.js +1 -1
  63. package/lib/helpers/discriminator/getDiscriminatorValueFromFilter.js +30 -0
  64. package/lib/helpers/discriminator/getSchemaDiscriminatorByValue.js +1 -1
  65. package/lib/helpers/discriminator/mergeDiscriminatorSchema.js +41 -9
  66. package/lib/helpers/document/applyDefaults.js +82 -14
  67. package/lib/helpers/document/applyTimestamps.js +106 -0
  68. package/lib/helpers/document/applyVirtuals.js +147 -0
  69. package/lib/helpers/document/cleanModifiedSubpaths.js +14 -4
  70. package/lib/helpers/document/compile.js +20 -15
  71. package/lib/helpers/document/getDeepestSubdocumentForPath.js +38 -0
  72. package/lib/helpers/document/getEmbeddedDiscriminatorPath.js +6 -3
  73. package/lib/helpers/document/handleSpreadDoc.js +2 -1
  74. package/lib/helpers/document/isInPathsToSave.js +24 -0
  75. package/lib/helpers/document/toObjectRecursive.js +49 -0
  76. package/lib/helpers/get.js +1 -1
  77. package/lib/helpers/getDefaultBulkwriteResult.js +11 -20
  78. package/lib/helpers/getInheritedProperty.js +28 -0
  79. package/lib/helpers/getOwnProperty.js +19 -0
  80. package/lib/helpers/indexes/applySchemaCollation.js +1 -1
  81. package/lib/helpers/indexes/decorateDiscriminatorIndexOptions.js +1 -1
  82. package/lib/helpers/indexes/getRelatedIndexes.js +8 -4
  83. package/lib/helpers/indexes/isDefaultIdIndex.js +1 -1
  84. package/lib/helpers/indexes/isIndexEqual.js +13 -12
  85. package/lib/helpers/indexes/isIndexSpecEqual.js +32 -0
  86. package/lib/helpers/indexes/isTimeseriesIndex.js +16 -0
  87. package/lib/helpers/isBsonType.js +3 -6
  88. package/lib/helpers/isMongooseDocument.js +24 -0
  89. package/lib/helpers/isMongooseObject.js +6 -4
  90. package/lib/helpers/isObject.js +2 -2
  91. package/lib/helpers/isPOJO.js +12 -0
  92. package/lib/helpers/isSimpleValidator.js +2 -2
  93. package/lib/helpers/minimize.js +41 -0
  94. package/lib/helpers/model/applyDefaultsToPOJO.js +2 -2
  95. package/lib/helpers/model/applyHooks.js +72 -54
  96. package/lib/helpers/model/applyMethods.js +3 -3
  97. package/lib/helpers/model/applyStaticHooks.js +20 -50
  98. package/lib/helpers/model/castBulkWrite.js +278 -172
  99. package/lib/helpers/model/decorateBulkWriteResult.js +8 -0
  100. package/lib/helpers/model/discriminator.js +77 -21
  101. package/lib/helpers/omitUndefined.js +20 -0
  102. package/lib/helpers/parallelLimit.js +61 -34
  103. package/lib/helpers/path/setDottedPath.js +1 -1
  104. package/lib/helpers/pathTrie.js +167 -0
  105. package/lib/helpers/pluralize.js +5 -4
  106. package/lib/helpers/populate/assignRawDocsToIdStructure.js +12 -7
  107. package/lib/helpers/populate/assignVals.js +46 -21
  108. package/lib/helpers/populate/createPopulateQueryFilter.js +13 -5
  109. package/lib/helpers/populate/getModelsMapForPopulate.js +250 -108
  110. package/lib/helpers/populate/getSchemaTypes.js +27 -30
  111. package/lib/helpers/populate/getVirtual.js +31 -0
  112. package/lib/helpers/populate/markArraySubdocsPopulated.js +6 -4
  113. package/lib/helpers/populate/modelNamesFromRefPath.js +11 -5
  114. package/lib/helpers/populate/removeDeselectedForeignField.js +3 -2
  115. package/lib/helpers/populate/setInheritedReadOptions.js +21 -0
  116. package/lib/helpers/populate/setPopulatedVirtualValue.js +33 -0
  117. package/lib/helpers/populate/splitPopulateQuery.js +81 -0
  118. package/lib/helpers/printJestWarning.js +1 -1
  119. package/lib/helpers/processConnectionOptions.js +7 -2
  120. package/lib/helpers/projection/applyProjection.js +8 -2
  121. package/lib/helpers/projection/hasIncludedChildren.js +1 -1
  122. package/lib/helpers/projection/isExclusive.js +8 -6
  123. package/lib/helpers/projection/isInclusive.js +2 -1
  124. package/lib/helpers/projection/isPathExcluded.js +3 -3
  125. package/lib/helpers/projection/isPathSelectedInclusive.js +1 -11
  126. package/lib/helpers/projection/isSubpath.js +1 -1
  127. package/lib/helpers/projection/parseProjection.js +9 -4
  128. package/lib/helpers/query/applyGlobalOption.js +9 -9
  129. package/lib/helpers/query/cast$expr.js +52 -23
  130. package/lib/helpers/query/castFilterPath.js +22 -22
  131. package/lib/helpers/query/castUpdate.js +207 -79
  132. package/lib/helpers/query/cloneMongooseOptions.js +44 -0
  133. package/lib/helpers/query/getEmbeddedDiscriminatorPath.js +27 -13
  134. package/lib/helpers/query/handleImmutable.js +18 -2
  135. package/lib/helpers/query/handleReadPreferenceAliases.js +23 -0
  136. package/lib/helpers/query/sanitizeFilter.js +3 -2
  137. package/lib/helpers/query/selectPopulatedFields.js +13 -0
  138. package/lib/helpers/query/validOps.js +1 -22
  139. package/lib/helpers/schema/applyPlugins.js +2 -2
  140. package/lib/helpers/schema/applyReadConcern.js +20 -0
  141. package/lib/helpers/schema/applyWriteConcern.js +12 -3
  142. package/lib/helpers/schema/cleanPositionalOperators.js +5 -3
  143. package/lib/helpers/schema/getIndexes.js +20 -13
  144. package/lib/helpers/schema/getKeysInSchemaOrder.js +13 -16
  145. package/lib/helpers/schema/getPath.js +11 -3
  146. package/lib/helpers/schema/getSubdocumentStrictValue.js +32 -0
  147. package/lib/helpers/schema/handleIdOption.js +1 -1
  148. package/lib/helpers/schema/idGetter.js +7 -2
  149. package/lib/helpers/schema/merge.js +9 -1
  150. package/lib/helpers/schematype/getConditionalHandler.js +23 -0
  151. package/lib/helpers/schematype/handleImmutable.js +1 -1
  152. package/lib/helpers/setDefaultsOnInsert.js +85 -43
  153. package/lib/helpers/symbols.js +4 -4
  154. package/lib/helpers/timestamps/setDocumentTimestamps.js +2 -2
  155. package/lib/helpers/timestamps/setupTimestamps.js +69 -37
  156. package/lib/helpers/update/applyTimestampsToChildren.js +1 -1
  157. package/lib/helpers/update/applyTimestampsToUpdate.js +53 -37
  158. package/lib/helpers/update/castArrayFilters.js +68 -29
  159. package/lib/helpers/update/decorateUpdateWithVersionKey.js +35 -0
  160. package/lib/helpers/update/modifiedPaths.js +2 -2
  161. package/lib/helpers/update/removeUnusedArrayFilters.js +11 -2
  162. package/lib/helpers/updateValidators.js +87 -128
  163. package/lib/index.js +6 -1320
  164. package/lib/internal.js +7 -1
  165. package/lib/model.js +2587 -2618
  166. package/lib/modifiedPathsSnapshot.js +9 -0
  167. package/lib/mongoose.js +1456 -0
  168. package/lib/options/saveOptions.js +2 -0
  169. package/lib/options/{SchemaArrayOptions.js → schemaArrayOptions.js} +16 -5
  170. package/lib/options/{SchemaBufferOptions.js → schemaBufferOptions.js} +2 -2
  171. package/lib/options/{SchemaDateOptions.js → schemaDateOptions.js} +1 -1
  172. package/lib/options/{SchemaDocumentArrayOptions.js → schemaDocumentArrayOptions.js} +24 -1
  173. package/lib/options/{SchemaMapOptions.js → schemaMapOptions.js} +1 -1
  174. package/lib/options/{SchemaNumberOptions.js → schemaNumberOptions.js} +5 -5
  175. package/lib/options/{SchemaObjectIdOptions.js → schemaObjectIdOptions.js} +4 -4
  176. package/lib/options/{SchemaStringOptions.js → schemaStringOptions.js} +10 -10
  177. package/lib/options/schemaSubdocumentOptions.js +89 -0
  178. package/lib/options/{SchemaTypeOptions.js → schemaTypeOptions.js} +28 -14
  179. package/lib/options/schemaUnionOptions.js +32 -0
  180. package/lib/options/{VirtualOptions.js → virtualOptions.js} +13 -13
  181. package/lib/options.js +4 -1
  182. package/lib/plugins/index.js +0 -2
  183. package/lib/plugins/saveSubdocs.js +85 -50
  184. package/lib/plugins/sharding.js +43 -15
  185. package/lib/plugins/trackTransaction.js +46 -34
  186. package/lib/query.js +1844 -1909
  187. package/lib/{queryhelpers.js → queryHelpers.js} +184 -81
  188. package/lib/schema/array.js +229 -169
  189. package/lib/schema/bigint.js +281 -0
  190. package/lib/schema/boolean.js +90 -26
  191. package/lib/schema/buffer.js +119 -50
  192. package/lib/schema/date.js +102 -38
  193. package/lib/schema/decimal128.js +88 -37
  194. package/lib/schema/{documentarray.js → documentArray.js} +259 -151
  195. package/lib/schema/documentArrayElement.js +180 -0
  196. package/lib/schema/double.js +244 -0
  197. package/lib/schema/index.js +12 -19
  198. package/lib/schema/int32.js +288 -0
  199. package/lib/schema/map.js +129 -20
  200. package/lib/schema/mixed.js +40 -26
  201. package/lib/schema/number.js +128 -51
  202. package/lib/schema/{objectid.js → objectId.js} +89 -47
  203. package/lib/schema/operators/exists.js +1 -1
  204. package/lib/schema/operators/geospatial.js +2 -2
  205. package/lib/schema/operators/text.js +4 -4
  206. package/lib/schema/string.js +125 -66
  207. package/lib/schema/subdocument.js +443 -0
  208. package/lib/schema/symbols.js +2 -0
  209. package/lib/schema/union.js +167 -0
  210. package/lib/schema/uuid.js +90 -120
  211. package/lib/schema.js +1267 -388
  212. package/lib/{schematype.js → schemaType.js} +481 -246
  213. package/lib/standardSchema/convertErrorToIssues.js +20 -0
  214. package/lib/{statemachine.js → stateMachine.js} +66 -17
  215. package/lib/tracing.js +38 -0
  216. package/lib/types/array/index.js +13 -10
  217. package/lib/types/array/methods/index.js +243 -63
  218. package/lib/types/{ArraySubdocument.js → arraySubdocument.js} +35 -14
  219. package/lib/types/buffer.js +31 -14
  220. package/lib/types/decimal128.js +1 -1
  221. package/lib/types/{DocumentArray → documentArray}/index.js +13 -13
  222. package/lib/types/{DocumentArray → documentArray}/methods/index.js +197 -31
  223. package/lib/types/double.js +13 -0
  224. package/lib/types/index.js +5 -2
  225. package/lib/types/map.js +62 -25
  226. package/lib/types/objectid.js +2 -2
  227. package/lib/types/subdocument.js +79 -58
  228. package/lib/types/uuid.js +13 -0
  229. package/lib/utils.js +201 -155
  230. package/lib/{validoptions.js → validOptions.js} +10 -2
  231. package/lib/{virtualtype.js → virtualType.js} +47 -18
  232. package/package.json +75 -92
  233. package/types/aggregate.d.ts +31 -7
  234. package/types/augmentations.d.ts +2 -2
  235. package/types/collection.d.ts +21 -16
  236. package/types/connection.d.ts +82 -27
  237. package/types/cursor.d.ts +15 -10
  238. package/types/document.d.ts +137 -56
  239. package/types/error.d.ts +11 -6
  240. package/types/expressions.d.ts +33 -2
  241. package/types/index.d.ts +805 -190
  242. package/types/indexes.d.ts +85 -13
  243. package/types/inferhydrateddoctype.d.ts +151 -0
  244. package/types/inferrawdoctype.d.ts +201 -0
  245. package/types/inferschematype.d.ts +287 -128
  246. package/types/middlewares.d.ts +50 -7
  247. package/types/models.d.ts +1555 -268
  248. package/types/mongooseoptions.d.ts +56 -7
  249. package/types/pipelinestage.d.ts +42 -3
  250. package/types/populate.d.ts +63 -1
  251. package/types/query.d.ts +592 -255
  252. package/types/schemaoptions.d.ts +78 -12
  253. package/types/schematypes.d.ts +254 -65
  254. package/types/session.d.ts +0 -4
  255. package/types/tracing.d.ts +10 -0
  256. package/types/types.d.ts +25 -8
  257. package/types/utility.d.ts +329 -30
  258. package/types/validation.d.ts +19 -14
  259. package/types/virtuals.d.ts +10 -10
  260. package/.eslintrc.js +0 -232
  261. package/.mocharc.yml +0 -4
  262. package/browser.js +0 -8
  263. package/lgtm.yml +0 -12
  264. package/lib/browser.js +0 -158
  265. package/lib/browserDocument.js +0 -101
  266. package/lib/cursor/ChangeStream.js +0 -154
  267. package/lib/cursor/QueryCursor.js +0 -545
  268. package/lib/document_provider.js +0 -30
  269. package/lib/drivers/browser/ReadPreference.js +0 -7
  270. package/lib/drivers/browser/binary.js +0 -14
  271. package/lib/drivers/browser/decimal128.js +0 -7
  272. package/lib/drivers/browser/index.js +0 -16
  273. package/lib/drivers/browser/objectid.js +0 -29
  274. package/lib/drivers/node-mongodb-native/ReadPreference.js +0 -47
  275. package/lib/drivers/node-mongodb-native/binary.js +0 -10
  276. package/lib/drivers/node-mongodb-native/decimal128.js +0 -7
  277. package/lib/drivers/node-mongodb-native/objectid.js +0 -16
  278. package/lib/error/disconnected.js +0 -33
  279. package/lib/helpers/printStrictQueryWarning.js +0 -11
  280. package/lib/helpers/promiseOrCallback.js +0 -55
  281. package/lib/helpers/query/applyQueryMiddleware.js +0 -79
  282. package/lib/helpers/query/completeMany.js +0 -52
  283. package/lib/helpers/query/wrapThunk.js +0 -36
  284. package/lib/options/SchemaSubdocumentOptions.js +0 -42
  285. package/lib/options/removeOptions.js +0 -14
  286. package/lib/plugins/removeSubdocs.js +0 -31
  287. package/lib/plugins/validateBeforeSave.js +0 -45
  288. package/lib/promise_provider.js +0 -49
  289. package/lib/schema/DocumentArrayElement.js +0 -100
  290. package/lib/schema/SubdocumentPath.js +0 -369
  291. package/scripts/build-browser.js +0 -18
  292. package/scripts/create-tarball.js +0 -7
  293. package/scripts/generateSearch.js +0 -154
  294. package/scripts/tsc-diagnostics-check.js +0 -15
  295. package/tools/auth.js +0 -31
  296. package/tools/repl.js +0 -35
  297. package/tools/sharded.js +0 -46
  298. package/tsconfig.json +0 -9
  299. /package/lib/{connectionstate.js → connectionState.js} +0 -0
  300. /package/lib/helpers/populate/{SkipPopulateValue.js → skipPopulateValue.js} +0 -0
  301. /package/lib/options/{PopulateOptions.js → populateOptions.js} +0 -0
  302. /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](http://mongoosejs.com/).
14
+ The official documentation website is [mongoosejs.com](https://mongoosejs.com/).
15
15
 
16
- Mongoose 6.0.0 was released on August 24, 2021. You can find more details on [backwards breaking changes in 6.0.0 on our docs site](https://mongoosejs.com/docs/migrating_to_6.html).
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
- - [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/)
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](http://plugins.mongoosejs.io/) to see hundreds of related modules from the community. Next, learn how to write your own plugin from the [docs](http://mongoosejs.com/docs/plugins.html) or [this blog post](http://thecodebarbarian.com/2015/03/06/guide-to-mongoose-plugins).
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 requests makes documentation changes, please do **not**
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](http://nodejs.org/) and [MongoDB](https://www.mongodb.org/downloads). Then:
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
- $ npm install mongoose
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:** _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._
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](http://mongoosejs.com/docs/validation.html) (async and sync)
122
- * [Defaults](http://mongoosejs.com/docs/api/schematype.html#schematype_SchemaType-default)
123
- * [Getters](http://mongoosejs.com/docs/api/schematype.html#schematype_SchemaType-get)
124
- * [Setters](http://mongoosejs.com/docs/api/schematype.html#schematype_SchemaType-set)
125
- * [Indexes](http://mongoosejs.com/docs/guide.html#indexes)
126
- * [Middleware](http://mongoosejs.com/docs/middleware.html)
127
- * [Methods](http://mongoosejs.com/docs/guide.html#methods) definition
128
- * [Statics](http://mongoosejs.com/docs/guide.html#statics) definition
129
- * [Plugins](http://mongoosejs.com/docs/plugins.html)
130
- * [pseudo-JOINs](http://mongoosejs.com/docs/populate.html)
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 _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
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 __tickets__ collection, not the __ticket__ collection. For more details read the [model docs](https://mongoosejs.com/docs/api/mongoose.html#mongoose_Mongoose-model).
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(function(err) {
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({}, function(err, docs) {
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](http://mongoosejs.com/docs/queries.html).
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(function(err) {
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, function(err, post) {
253
- if (!err) {
254
- post.comments[0].remove();
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. Whenever an error occurs, it's bubbled to the `save()` error callback, so error handling is a snap!
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](http://mongoosejs.com/docs/middleware.html) page.
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](http://mongodb.github.io/node-mongodb-native/2.1/api/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
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
- Find the API docs [here](http://mongoosejs.com/docs/api/mongoose.html), generated using [dox](https://github.com/tj/dox)
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
- #### MongoDB Runners
355
+ ### MongoDB Runners
346
356
 
347
- - [run-rs](https://www.npmjs.com/package/run-rs)
348
- - [mongodb-memory-server](https://www.npmjs.com/package/mongodb-memory-server)
349
- - [mongodb-topology-manager](https://www.npmjs.com/package/mongodb-topology-manager)
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
- #### Unofficial CLIs
361
+ ### Unofficial CLIs
352
362
 
353
- - [mongoosejs-cli](https://www.npmjs.com/package/mongoosejs-cli)
363
+ * [mongoosejs-cli](https://www.npmjs.com/package/mongoosejs-cli)
354
364
 
355
- #### Data Seeding
365
+ ### Data Seeding
356
366
 
357
- - [dookie](https://www.npmjs.com/package/dookie)
358
- - [seedgoose](https://www.npmjs.com/package/seedgoose)
359
- - [mongoose-data-seed](https://www.npmjs.com/package/mongoose-data-seed)
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
- #### Express Session Stores
371
+ ### Express Session Stores
362
372
 
363
- - [connect-mongodb-session](https://www.npmjs.com/package/connect-mongodb-session)
364
- - [connect-mongo](https://www.npmjs.com/package/connect-mongo)
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;