mikro-orm with mongo database, do not forget to install
mongodb dependency. As
is the default one, you do not need to provide it.
MikroORM.init as part of bootstrapping your app:
When defining entity, do not forget to define primary key like this:
_id: ObjectIdwill be saved in the database.
id: stringis virtual and is also optional.
ObjectId and string id duality
Every entity has both
string id available, also all methods of
EntityRepository supports querying by both of them.
ManyToMany collections with inlined pivot array
As opposed to SQL drivers that use pivot tables, in mongo we can leverage available array type to store array of collection items (identifiers). This approach has two main benefits:
- Collection is stored on owning side entity, so we know how many items are there even before initializing the collection.
- As there are no pivot tables, resulting database queries are much simpler.
Native collection methods
Sometimes you need to perform some bulk operation, or you just want to populate your
database with initial fixtures. Using ORM for such operations can bring unnecessary
boilerplate code. In this case, you can use one of
Those methods execute native driver methods like Mongo's
insertOne/updateMany/deleteMany collection methods respectively.
This is common interface for all drivers, so for MySQL driver, it will fire native SQL queries.
Keep in mind that they do not hydrate results to entities, and they do not trigger lifecycle hooks.
They are also available as
There is also shortcut for calling