packages/migrations-mongodb/README.md
Database migration support for MikroORM MongoDB driver. Provides migration generation and execution for MongoDB databases.
npm install @mikro-orm/migrations-mongodb
# Create a blank migration
npx mikro-orm migration:create --blank
# Run pending migrations
npx mikro-orm migration:up
# Revert the last migration
npx mikro-orm migration:down
import { MikroORM } from '@mikro-orm/mongodb';
const orm = await MikroORM.init({
entities: [Author, Book],
dbName: 'my-db',
clientUrl: 'mongodb://localhost:27017',
migrations: {
path: './src/migrations',
},
});
const migrator = orm.migrator;
await migrator.up();
MongoDB migrations use the native MongoDB driver API:
import { Migration } from '@mikro-orm/migrations-mongodb';
export class Migration20250101 extends Migration {
async up(): Promise<void> {
const collection = this.getCollection('users');
await collection.updateMany({}, { $set: { active: true } });
}
async down(): Promise<void> {
const collection = this.getCollection('users');
await collection.updateMany({}, { $unset: { active: '' } });
}
}
See the official MikroORM documentation.
Copyright © 2018-present Martin Adámek. Licensed under the MIT License.