MongoDB
MongoDB support
TypeORM has basic MongoDB support. Most of TypeORM functionality is RDBMS-specific, this page contains all MongoDB-specific functionality documentation.
Defining entities and columns
Defining entities and columns is almost the same as in relational databases, the main difference is that you must use @ObjectIdColumn
instead of @PrimaryColumn
or @PrimaryGeneratedColumn
.
Simple entity example:
And this is how you bootstrap the app:
Defining subdocuments (embed documents)
Since MongoDB stores objects and objects inside objects (or documents inside documents) you can do the same in TypeORM:
If you save this entity:
Following document will be saved in the database:
Using MongoEntityManager
and MongoRepository
MongoEntityManager
and MongoRepository
You can use the majority of methods inside the EntityManager
(except for RDBMS-specific, like query
and transaction
). For example:
For MongoDB there is also a separate MongoEntityManager
which extends EntityManager
.
Just like separate like MongoEntityManager
there is a MongoRepository
with extended Repository
:
Use Advanced options in find():
Equal:
LessThan:
In:
Not in:
Or:
Querying subdocuments
Querying Array of subdocuments
Both MongoEntityManager
and MongoRepository
contain lot of useful MongoDB-specific methods:
createCursor
createCursor
Creates a cursor for a query that can be used to iterate over results from MongoDB.
createEntityCursor
createEntityCursor
Creates a cursor for a query that can be used to iterate over results from MongoDB. This returns a modified version of the cursor that transforms each result into Entity models.
aggregate
aggregate
Execute an aggregation framework pipeline against the collection.
bulkWrite
bulkWrite
Perform a bulkWrite operation without a fluent API.
count
count
Count number of matching documents in the db to a query.
countDocuments
countDocuments
Count number of matching documents in the db to a query.
createCollectionIndex
createCollectionIndex
Creates an index on the db and collection.
createCollectionIndexes
createCollectionIndexes
Creates multiple indexes in the collection, this method is only supported in MongoDB 2.6 or higher. Earlier version of MongoDB will throw a command not supported error. Index specifications are defined at http://docs.mongodb.org/manual/reference/command/createIndexes/.
deleteMany
deleteMany
Delete multiple documents on MongoDB.
deleteOne
deleteOne
Delete a document on MongoDB.
distinct
distinct
The distinct command returns a list of distinct values for the given key across a collection.
dropCollectionIndex
dropCollectionIndex
Drops an index from this collection.
dropCollectionIndexes
dropCollectionIndexes
Drops all indexes from the collection.
findOneAndDelete
findOneAndDelete
Find a document and delete it in one atomic operation, requires a write lock for the duration of the operation.
findOneAndReplace
findOneAndReplace
Find a document and replace it in one atomic operation, requires a write lock for the duration of the operation.
findOneAndUpdate
findOneAndUpdate
Find a document and update it in one atomic operation, requires a write lock for the duration of the operation.
geoHaystackSearch
geoHaystackSearch
Execute a geo search using a geo haystack index on a collection.
geoNear
geoNear
Execute the geoNear command to search for items in the collection.
group
group
Run a group command across a collection.
collectionIndexes
collectionIndexes
Retrieve all the indexes on the collection.
collectionIndexExists
collectionIndexExists
Retrieve if an index exists on the collection
collectionIndexInformation
collectionIndexInformation
Retrieves this collections index info.
initializeOrderedBulkOp
initializeOrderedBulkOp
Initiate an In order bulk write operation, operations will be serially executed in the order they are added, creating a new operation for each switch in types.
initializeUnorderedBulkOp
initializeUnorderedBulkOp
Initiate a Out of order batch write operation. All operations will be buffered into insert/update/remove commands executed out of order.
insertMany
insertMany
Inserts an array of documents into MongoDB.
insertOne
insertOne
Inserts a single document into MongoDB.
isCapped
isCapped
Returns if the collection is a capped collection.
listCollectionIndexes
listCollectionIndexes
Get the list of all indexes information for the collection.
parallelCollectionScan
parallelCollectionScan
Return N number of parallel cursors for a collection allowing parallel reading of entire collection. There are no ordering guarantees for returned results
reIndex
reIndex
Reindex all indexes on the collection Warning: reIndex is a blocking operation (indexes are rebuilt in the foreground) and will be slow for large collections.
rename
rename
Changes the name of an existing collection.
replaceOne
replaceOne
Replace a document on MongoDB.
stats
stats
Get all the collection statistics.
updateMany
updateMany
Updates multiple documents within the collection based on the filter.
updateOne
updateOne
Updates a single document within the collection based on the filter.
Last updated