Newsletter
Join the Community
Subscribe to our newsletter for the latest news and updates
A Nuxt module for simplifying the use of Mongoose in your project.
Nuxt Mongoose is a Nuxt module that simplifies adding the Mongoose ODM to Nuxt projects by auto-importing schemas and providing helper functions for models and connections. It accepts a MongoDB URI and schema definitions as input, and outputs auto-imported Mongoose models plus programmatic connection helpers. The module runs inside any Nuxt app's server directory and is installed via npx nuxi@latest module add nuxt-mongoose.
Nuxt Mongoose is a Nuxt module that integrates Mongoose, a MongoDB object data modeling library, with Nuxt's server environment. It takes a MongoDB URI and your schema files as input, and produces ready-to-use Mongoose models and connections. It runs on Nuxt (which is built on Vue) and is installable with a single nuxi command. The module is distributed through the Nuxt module registry and documented at docs.arashsheyda.me.
models directory under server, and Nuxt Mongoose automatically imports them as models, eliminating manual import statements.NUXT_MONGOOSE_URI environment variable, so minimal setup is just a .env file entry.mongoose block to nuxt.config.ts.defineMongooseModel('User', schemaObject) or an object form with name, schema, and options fields.options.collection in a model definition to bind to an existing MongoDB collection; otherwise Mongoose uses the pluralized model name.models directory and use the options.collection field to point at their current collections.defineMongooseConnection to establish separate connections for different microservices or data domains within the same project.After installation and registration in the modules array, Nuxt Mongoose reads your MongoDB URI from NUXT_MONGOOSE_URI or the mongoose config block. It then scans the configured models directory (default server/models) and auto-imports every schema as a Mongoose model. You can also define models imperatively with defineMongooseModel and create extra connections with defineMongooseConnection. The module follows standard Mongoose connection options, so any options you pass are forwarded to Mongoose's createConnection.
Run npx nuxi@latest module add nuxt-mongoose inside your Nuxt project, then add 'nuxt-mongoose' to the modules array in nuxt.config.ts. After that, set the NUXT_MONGOOSE_URI environment variable in a .env file.
Define your model with defineMongooseModel and set the options.collection field to the exact collection name you already have. For example, if your collection is called products_collection, specify that in the model options; otherwise Mongoose auto-creates a collection using the pluralized model name.
Yes, Nuxt Mongoose is released under the MIT License, so you can use it freely in personal and commercial projects without licensing fees.
Yes, the defineMongooseConnection function lets you create separate Mongoose connections, each with its own URI. You can maintain multiple connections within the same Nuxt application and use them with your models.
