packages/mongodb/README.md
MikroORM driver for MongoDB databases, built on top of the official mongodb Node.js driver.
npm install @mikro-orm/core @mikro-orm/mongodb
import { defineEntity, p, MikroORM } from '@mikro-orm/mongodb';
const AuthorSchema = defineEntity({
name: 'Author',
properties: {
id: p.objectId().primary(),
name: p.string(),
books: () => p.oneToMany(Book).mappedBy('author'),
},
});
export class Author extends AuthorSchema.class {}
AuthorSchema.setClass(Author);
const BookSchema = defineEntity({
name: 'Book',
properties: {
id: p.objectId().primary(),
title: p.string(),
author: () => p.manyToOne(Author).inversedBy('books'),
},
});
export class Book extends BookSchema.class {}
BookSchema.setClass(Book);
const orm = await MikroORM.init({
entities: [Author, Book],
dbName: 'my-db',
clientUrl: 'mongodb://localhost:27017',
});
const author = orm.em.create(Author, { name: 'Jon Snow' });
orm.em.create(Book, { title: 'My Life on The Wall', author });
await orm.em.flush();
const authors = await orm.em.find(
Author,
{ name: /Jon/ },
{
populate: ['books'],
},
);
$regex, $exists, $elemMatch, and more_id ↔ id)@mikro-orm/migrations-mongodbSee the official MikroORM documentation and the MongoDB usage guide.
Copyright © 2018-present Martin Adámek. Licensed under the MIT License.