Usage with MongoDB
To use mikro-orm
with mongo database, do not forget to install mongodb
dependency. As MongoDriver
is the default one, you do not need to provide it.
Then call MikroORM.init
as part of bootstrapping your app:
#
Defining entityWhen defining entity, do not forget to define primary key like this:
Only
_id: ObjectId
will be saved in the database.id: string
is virtual and is also optional.
#
ObjectId and string id dualityEvery entity has both ObjectId
and string
id available, also all methods of EntityManager
and EntityRepository
supports querying by both of them.
#
ManyToMany collections with inlined pivot arrayAs 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.
#
TransactionsStarting with v3.4, MongoDB driver supports transactions. To use transactions, there are several things you need to respect:
- you need to use replica set (see run-rs)
- implicit transactions are disabled by default
- use
implicitTransactions: true
to enable them globally - or use explicit transaction demarcation via
em.transactional()
- use
- you need to explicitly create all collections before working with them
- use
em.getDriver().createCollections()
method to do so
- use
The
createCollections
method is present on theMongoDriver
class only. You need to have the entity manager correctly typed (asEntityManager<MongoDriver>
).
#
IndexesStarting with v3.4, MongoDB driver supports indexes and unique constraints. You can
use @Index()
and @Unique()
as described in Defining Entities section.
To automatically create new indexes when initializing the ORM, you need to enable
ensureIndexes
option.
Alternatively you can call ensureIndexes()
method on the MongoDriver
:
You can pass additional index/unique options via
options
parameter:
@Unique({ options: { partialFilterExpression: { name: { $exists: true } } }})
You can also create text indexes by passing
type
parameter:
@Index({ properties: ['name', 'caption'], type: 'text' })
If you provide only
options
in the index definition, it will be used as is, this allows to define any kind of index:
@Index({ options: { point: '2dsphere', title: -1 } })
#
Native collection methodsSometimes 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 nativeInsert/nativeUpdate/nativeDelete
methods:
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 EntityRepository
shortcuts:
There is also shortcut for calling aggregate
method: