[{"data":1,"prerenderedAt":-1},["ShallowReactive",2],{"project-2896":3},{"id":4,"name":5,"fullName":6,"owner":7,"repo":5,"description":8,"homepage":9,"htmlUrl":10,"language":11,"languages":10,"totalLinesOfCode":10,"stars":12,"forks":13,"watchers":14,"openIssues":15,"contributorsCount":16,"subscribersCount":16,"size":16,"stars1d":16,"stars7d":17,"stars30d":18,"stars90d":16,"forks30d":16,"starsTrendScore":19,"compositeScore":20,"rankGlobal":10,"rankLanguage":10,"license":21,"archived":22,"fork":22,"defaultBranch":23,"hasWiki":24,"hasPages":24,"topics":25,"createdAt":10,"pushedAt":10,"updatedAt":31,"readmeContent":32,"aiSummary":33,"trendingCount":16,"starSnapshotCount":16,"syncStatus":19,"lastSyncTime":34,"discoverSource":35},2896,"mongoose","Automattic\u002Fmongoose","Automattic","MongoDB object modeling designed to work in an asynchronous environment.","https:\u002F\u002Fmongoosejs.com",null,"JavaScript",27483,3990,461,168,0,5,14,2,73.9,"MIT License",false,"master",true,[26,27,28,29,30],"mongo","mongodb","nodejs","odm","orm","2026-06-12 04:00:16","# Mongoose\n\nMongoose is a [MongoDB](https:\u002F\u002Fwww.mongodb.org\u002F) object modeling tool designed to work in an asynchronous environment. Mongoose supports [Node.js](https:\u002F\u002Fnodejs.org\u002Fen\u002F) and [Deno](https:\u002F\u002Fdeno.land\u002F) (alpha).\n\n[![Build Status](https:\u002F\u002Fgithub.com\u002FAutomattic\u002Fmongoose\u002Fworkflows\u002FTest\u002Fbadge.svg)](https:\u002F\u002Fgithub.com\u002FAutomattic\u002Fmongoose)\n[![NPM version](https:\u002F\u002Fbadge.fury.io\u002Fjs\u002Fmongoose.svg)](http:\u002F\u002Fbadge.fury.io\u002Fjs\u002Fmongoose)\n[![Deno version](https:\u002F\u002Fdeno.land\u002Fbadge\u002Fmongoose\u002Fversion)](https:\u002F\u002Fdeno.land\u002Fx\u002Fmongoose)\n[![Deno popularity](https:\u002F\u002Fdeno.land\u002Fbadge\u002Fmongoose\u002Fpopularity)](https:\u002F\u002Fdeno.land\u002Fx\u002Fmongoose)\n\n[![npm](https:\u002F\u002Fnodei.co\u002Fnpm\u002Fmongoose.png)](https:\u002F\u002Fwww.npmjs.com\u002Fpackage\u002Fmongoose)\n\n## Documentation\n\nThe official documentation website is [mongoosejs.com](https:\u002F\u002Fmongoosejs.com\u002F).\n\nMongoose 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:\u002F\u002Fmongoosejs.com\u002Fdocs\u002Fmigrating_to_9.html).\n\n## Support\n\n* [Stack Overflow](http:\u002F\u002Fstackoverflow.com\u002Fquestions\u002Ftagged\u002Fmongoose)\n* [Bug Reports](https:\u002F\u002Fgithub.com\u002FAutomattic\u002Fmongoose\u002Fissues\u002F)\n* [Mongoose Slack Channel](http:\u002F\u002Fslack.mongoosejs.io\u002F)\n* [Help Forum](http:\u002F\u002Fgroups.google.com\u002Fgroup\u002Fmongoose-orm)\n* [MongoDB Support](https:\u002F\u002Fwww.mongodb.com\u002Fdocs\u002Fmanual\u002Fsupport\u002F)\n\n## Plugins\n\nCheck out the [plugins search site](https:\u002F\u002Fplugins.mongoosejs.io\u002F) to see hundreds of related modules from the community. Next, learn how to write your own plugin from the [docs](https:\u002F\u002Fmongoosejs.com\u002Fdocs\u002Fplugins.html) or [this blog post](http:\u002F\u002Fthecodebarbarian.com\u002F2015\u002F03\u002F06\u002Fguide-to-mongoose-plugins).\n\n## Contributors\n\nPull requests are always welcome! Please base pull requests against the `master`\nbranch and follow the [contributing guide](https:\u002F\u002Fgithub.com\u002FAutomattic\u002Fmongoose\u002Fblob\u002Fmaster\u002FCONTRIBUTING.md).\n\nIf your pull requests makes documentation changes, please do **not**\nmodify any `.html` files. The `.html` files are compiled code, so please make\nyour changes in `docs\u002F*.pug`, `lib\u002F*.js`, or `test\u002Fdocs\u002F*.js`.\n\nView all 400+ [contributors](https:\u002F\u002Fgithub.com\u002FAutomattic\u002Fmongoose\u002Fgraphs\u002Fcontributors).\n\n## Installation\n\nFirst install [Node.js](https:\u002F\u002Fnodejs.org\u002F) and [MongoDB](https:\u002F\u002Fwww.mongodb.org\u002Fdownloads), then install the `mongoose` package using your preferred package manager:\n\n### Using npm\n\n```sh\nnpm install mongoose\n```\n\n### Using pnpm\n\n```sh\npnpm add mongoose\n```\n\n### Using Yarn\n\n```sh\nyarn add mongoose\n```\n\n### Using Bun\n\n```sh\nbun add mongoose\n```\n\nMongoose 6.8.0 also includes alpha support for [Deno](https:\u002F\u002Fdeno.land\u002F).\n\n## Importing\n\n```javascript\n\u002F\u002F Using Node.js `require()`\nconst mongoose = require('mongoose');\n\n\u002F\u002F Using ES6 imports\nimport mongoose from 'mongoose';\n```\n\nOr, using [Deno's `createRequire()` for CommonJS support](https:\u002F\u002Fdeno.land\u002Fstd@0.113.0\u002Fnode\u002FREADME.md?source=#commonjs-modules-loading) as follows.\n\n```javascript\nimport { createRequire } from 'https:\u002F\u002Fdeno.land\u002Fstd@0.177.0\u002Fnode\u002Fmodule.ts';\nconst require = createRequire(import.meta.url);\n\nconst mongoose = require('mongoose');\n\nmongoose.connect('mongodb:\u002F\u002F127.0.0.1:27017\u002Ftest')\n  .then(() => console.log('Connected!'));\n```\n\nYou can then run the above script using the following.\n\n```sh\ndeno run --allow-net --allow-read --allow-sys --allow-env mongoose-test.js\n```\n\n## Mongoose for Enterprise\n\nAvailable as part of the Tidelift Subscription\n\nThe maintainers of mongoose and thousands of other packages are working with Tidelift to deliver commercial support and maintenance for the open source dependencies you use to build your applications. Save time, reduce risk, and improve code health, while paying the maintainers of the exact dependencies you use. [Learn more.](https:\u002F\u002Ftidelift.com\u002Fsubscription\u002Fpkg\u002Fnpm-mongoose?utm_source=npm-mongoose&utm_medium=referral&utm_campaign=enterprise&utm_term=repo)\n\n## Overview\n\n### Connecting to MongoDB\n\nFirst, we need to define a connection. If your app uses only one database, you should use `mongoose.connect`. If you need to create additional connections, use `mongoose.createConnection`.\n\nBoth `connect` and `createConnection` take a `mongodb:\u002F\u002F` URI, or the parameters `host, database, port, options`.\n\n```js\nawait mongoose.connect('mongodb:\u002F\u002F127.0.0.1\u002Fmy_database');\n```\n\nOnce 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`.\n\n**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.*\n\n**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.\n\n### Defining a Model\n\nModels are defined through the `Schema` interface.\n\n```js\nconst Schema = mongoose.Schema;\nconst ObjectId = Schema.ObjectId;\n\nconst BlogPost = new Schema({\n  author: ObjectId,\n  title: String,\n  body: String,\n  date: Date\n});\n```\n\nAside from defining the structure of your documents and the types of data you're storing, a Schema handles the definition of:\n\n* [Validators](https:\u002F\u002Fmongoosejs.com\u002Fdocs\u002Fvalidation.html) (async and sync)\n* [Defaults](https:\u002F\u002Fmongoosejs.com\u002Fdocs\u002Fapi\u002Fschematype.html#schematype_SchemaType-default)\n* [Getters](https:\u002F\u002Fmongoosejs.com\u002Fdocs\u002Fapi\u002Fschematype.html#schematype_SchemaType-get)\n* [Setters](https:\u002F\u002Fmongoosejs.com\u002Fdocs\u002Fapi\u002Fschematype.html#schematype_SchemaType-set)\n* [Indexes](https:\u002F\u002Fmongoosejs.com\u002Fdocs\u002Fguide.html#indexes)\n* [Middleware](https:\u002F\u002Fmongoosejs.com\u002Fdocs\u002Fmiddleware.html)\n* [Methods](https:\u002F\u002Fmongoosejs.com\u002Fdocs\u002Fguide.html#methods) definition\n* [Statics](https:\u002F\u002Fmongoosejs.com\u002Fdocs\u002Fguide.html#statics) definition\n* [Plugins](https:\u002F\u002Fmongoosejs.com\u002Fdocs\u002Fplugins.html)\n* [pseudo-JOINs](https:\u002F\u002Fmongoosejs.com\u002Fdocs\u002Fpopulate.html)\n\nThe following example shows some of these features:\n\n```js\nconst Comment = new Schema({\n  name: { type: String, default: 'hahaha' },\n  age: { type: Number, min: 18, index: true },\n  bio: { type: String, match: \u002F[a-z]\u002F },\n  date: { type: Date, default: Date.now },\n  buff: Buffer\n});\n\n\u002F\u002F a setter\nComment.path('name').set(function(v) {\n  return capitalize(v);\n});\n\n\u002F\u002F middleware\nComment.pre('save', function(next) {\n  notify(this.get('email'));\n  next();\n});\n```\n\nTake a look at the example in [`examples\u002Fschema\u002Fschema.js`](https:\u002F\u002Fgithub.com\u002FAutomattic\u002Fmongoose\u002Fblob\u002Fmaster\u002Fexamples\u002Fschema\u002Fschema.js) for an end-to-end example of a typical setup.\n\n### Accessing a Model\n\nOnce we define a model through `mongoose.model('ModelName', mySchema)`, we can access it through the same function\n\n```js\nconst MyModel = mongoose.model('ModelName');\n```\n\nOr just do it all at once\n\n```js\nconst MyModel = mongoose.model('ModelName', mySchema);\n```\n\nThe 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\n\n```js\nconst MyModel = mongoose.model('Ticket', mySchema);\n```\n\nThen `MyModel` will use the **tickets** collection, not the **ticket** collection. For more details read the [model docs](https:\u002F\u002Fmongoosejs.com\u002Fdocs\u002Fapi\u002Fmongoose.html#mongoose_Mongoose-model).\n\nOnce we have our model, we can then instantiate it, and save it:\n\n```js\nconst instance = new MyModel();\ninstance.my.key = 'hello';\nawait instance.save();\n```\n\nOr we can find documents from the same collection\n\n```js\nawait MyModel.find({});\n```\n\nYou can also `findOne`, `findById`, `update`, etc.\n\n```js\nconst instance = await MyModel.findOne({ \u002F* ... *\u002F });\nconsole.log(instance.my.key); \u002F\u002F 'hello'\n```\n\nFor more details check out [the docs](https:\u002F\u002Fmongoosejs.com\u002Fdocs\u002Fqueries.html).\n\n**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:\n\n```js\nconst conn = mongoose.createConnection('your connection string');\nconst MyModel = conn.model('ModelName', schema);\nconst m = new MyModel();\nawait m.save(); \u002F\u002F works\n```\n\nvs\n\n```js\nconst conn = mongoose.createConnection('your connection string');\nconst MyModel = mongoose.model('ModelName', schema);\nconst m = new MyModel();\nawait m.save(); \u002F\u002F does not work b\u002Fc the default connection object was never connected\n```\n\n### Embedded Documents\n\nIn the first example snippet, we defined a key in the Schema that looks like:\n\n```txt\ncomments: [Comment]\n```\n\nWhere `Comment` is a `Schema` we created. This means that creating embedded documents is as simple as:\n\n```js\n\u002F\u002F retrieve my model\nconst BlogPost = mongoose.model('BlogPost');\n\n\u002F\u002F create a blog post\nconst post = new BlogPost();\n\n\u002F\u002F create a comment\npost.comments.push({ title: 'My comment' });\n\nawait post.save();\n```\n\nThe same goes for removing them:\n\n```js\nconst post = await BlogPost.findById(myId);\npost.comments[0].deleteOne();\nawait post.save();\n```\n\nEmbedded documents enjoy all the same features as your models. Defaults, validators, middleware.\n\n### Middleware\n\nSee the [docs](https:\u002F\u002Fmongoosejs.com\u002Fdocs\u002Fmiddleware.html) page.\n\n#### Intercepting and mutating method arguments\n\nYou can intercept method arguments via middleware.\n\nFor example, this would allow you to broadcast changes about your Documents every time someone `set`s a path in your Document to a new value:\n\n```js\nschema.pre('set', function(next, path, val, typel) {\n  \u002F\u002F `this` is the current Document\n  this.emit('set', path, val);\n\n  \u002F\u002F Pass control to the next pre\n  next();\n});\n```\n\nMoreover, you can mutate the incoming `method` arguments so that subsequent middleware see different values for those arguments. To do so, just pass the new values to `next`:\n\n```js\nschema.pre(method, function firstPre(next, methodArg1, methodArg2) {\n  \u002F\u002F Mutate methodArg1\n  next('altered-' + methodArg1.toString(), methodArg2);\n});\n\n\u002F\u002F pre declaration is chainable\nschema.pre(method, function secondPre(next, methodArg1, methodArg2) {\n  console.log(methodArg1);\n  \u002F\u002F => 'altered-originalValOfMethodArg1'\n\n  console.log(methodArg2);\n  \u002F\u002F => 'originalValOfMethodArg2'\n\n  \u002F\u002F Passing no arguments to `next` automatically passes along the current argument values\n  \u002F\u002F i.e., the following `next()` is equivalent to `next(methodArg1, methodArg2)`\n  \u002F\u002F and also equivalent to, with the example method arg\n  \u002F\u002F values, `next('altered-originalValOfMethodArg1', 'originalValOfMethodArg2')`\n  next();\n});\n```\n\n#### Schema gotcha\n\n`type`, when used in a schema has special meaning within Mongoose. If your schema requires using `type` as a nested property you must use object notation:\n\n```js\nnew Schema({\n  broken: { type: Boolean },\n  asset: {\n    name: String,\n    type: String \u002F\u002F uh oh, it broke. asset will be interpreted as String\n  }\n});\n\nnew Schema({\n  works: { type: Boolean },\n  asset: {\n    name: String,\n    type: { type: String } \u002F\u002F works. asset is an object with a type property\n  }\n});\n```\n\n### Driver Access\n\nMongoose is built on top of the [official MongoDB Node.js driver](https:\u002F\u002Fgithub.com\u002Fmongodb\u002Fnode-mongodb-native). Each mongoose model keeps a reference to a [native MongoDB driver collection](http:\u002F\u002Fmongodb.github.io\u002Fnode-mongodb-native\u002F2.1\u002Fapi\u002FCollection.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\nnotable exception that `YourModel.collection` still buffers\ncommands. As such, `YourModel.collection.find()` will **not**\nreturn a cursor.\n\n## API Docs\n\n[Mongoose API documentation](https:\u002F\u002Fmongoosejs.com\u002Fdocs\u002Fapi\u002Fmongoose.html), generated using [dox](https:\u002F\u002Fgithub.com\u002Ftj\u002Fdox)\nand [acquit](https:\u002F\u002Fgithub.com\u002Fvkarpov15\u002Facquit).\n\n## Related Projects\n\n### MongoDB Runners\n\n* [run-rs](https:\u002F\u002Fwww.npmjs.com\u002Fpackage\u002Frun-rs)\n* [mongodb-memory-server](https:\u002F\u002Fwww.npmjs.com\u002Fpackage\u002Fmongodb-memory-server)\n* [mongodb-topology-manager](https:\u002F\u002Fwww.npmjs.com\u002Fpackage\u002Fmongodb-topology-manager)\n\n### Unofficial CLIs\n\n* [mongoosejs-cli](https:\u002F\u002Fwww.npmjs.com\u002Fpackage\u002Fmongoosejs-cli)\n\n### Data Seeding\n\n* [dookie](https:\u002F\u002Fwww.npmjs.com\u002Fpackage\u002Fdookie)\n* [seedgoose](https:\u002F\u002Fwww.npmjs.com\u002Fpackage\u002Fseedgoose)\n* [mongoose-data-seed](https:\u002F\u002Fwww.npmjs.com\u002Fpackage\u002Fmongoose-data-seed)\n\n### Express Session Stores\n\n* [connect-mongodb-session](https:\u002F\u002Fwww.npmjs.com\u002Fpackage\u002Fconnect-mongodb-session)\n* [connect-mongo](https:\u002F\u002Fwww.npmjs.com\u002Fpackage\u002Fconnect-mongo)\n\n## License\n\nCopyright (c) 2010 LearnBoost &lt;dev@learnboost.com&gt;\n\nPermission is hereby granted, free of charge, to any person obtaining\na copy of this software and associated documentation files (the\n'Software'), to deal in the Software without restriction, including\nwithout limitation the rights to use, copy, modify, merge, publish,\ndistribute, sublicense, and\u002For sell copies of the Software, and to\npermit persons to whom the Software is furnished to do so, subject to\nthe following conditions:\n\nThe above copyright notice and this permission notice shall be\nincluded in all copies or substantial portions of the Software.\n\nTHE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND,\nEXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF\nMERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.\nIN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY\nCLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,\nTORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE\nSOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.\n","Mongoose 是一个为异步环境设计的 MongoDB 对象建模工具。它支持 Node.js 和 Deno（alpha 版本），提供了丰富的数据验证、类型转换和中间件功能，简化了与 MongoDB 的交互。Mongoose 通过其强大的模式定义能力，使得开发者可以方便地定义数据结构，并自动处理数据的一致性和完整性。此外，它还支持插件扩展，增强了其灵活性和功能性。适用于需要高效管理和操作 MongoDB 数据的应用场景，特别是在构建基于 Node.js 或 Deno 的 Web 应用时。","2026-06-11 02:51:32","top_language"]