Skip to main content
Version: 2.7


OneToMany and ManyToMany collections are stored in a Collection wrapper. It implements iterator so you can use for of loop to iterate through it.

Another way to access collection items is to use bracket syntax like when you access array items. Keep in mind that this approach will not check if the collection is initialed, while using get method will throw error in this case.

Note that array access in Collection is available only for reading already loaded items, you cannot add new items to Collection this way.

const author = orm.em.findOne(Author, '...', ['books']); // populating books collection
// or we could lazy load books collection later via `init()` method
await author.books.init();
for (const book of author.books) {
console.log(book.title); // initialized
console.log(; // true
console.log(; // Jon Snow
console.log(book.publisher); // just reference
console.log(book.publisher.isInitialized()); // false
console.log(; // undefined
// collection needs to be initialized before you can work with it
console.log(author.books.contains(book)); // true
console.log(author.books.contains(book)); // false
console.log(author.books.count()); // 1
console.log(author.books.contains(book)); // false
console.log(author.books.count()); // 0
console.log(author.books.getItems()); // Book[]
console.log(author.books.getIdentifiers()); // array of string | number
console.log(author.books.getIdentifiers('_id')); // array of ObjectID
// array access works as well
console.log(author.books[1]); // Book
console.log(author.books[12345]); // undefined, even if the collection is not initialized

OneToMany collections#

OneToMany collections are inverse side of ManyToOne references, to which they need to point via fk attribute:

export class Book {
_id: ObjectID;
author: Author;
export class Author {
_id: ObjectID;
@OneToMany({ entity: () => Book, mappedBy: 'author' })
books = new Collection<Book>(this);

ManyToMany collections#

As opposed to SQL databases, with MongoDB we do not need to have join tables for ManyToMany relations. All references are stored as an array of ObjectIDs on owning entity.


Unidirectional ManyToMany relations are defined only on one side, and marked explicitly as owner:

@ManyToMany({ entity: () => Book, owner: true })
books = new Collection<Book>(this);


Bidirectional ManyToMany relations are defined on both sides, while one is owning side (where references are store), marked by inversedBy attribute pointing to the inverse side:

@ManyToMany({ entity: () => BookTag, inversedBy: 'books' })
tags = new Collection<BookTag>(this);

And on the inversed side we define it with mappedBy attribute poining back to the owner:

@ManyToMany({ entity: () => Book, mappedBy: 'tags' })
books = new Collection<Book>(this);

Propagation of Collection's add() and remove() operations#

When you use one of Collection.add() method, the item is added to given collection, and this action is also propagated to its counterpart.

// one to many
const author = new Author(...);
const book = new Book(...);
console.log(; // author will be set thanks to the propagation

For M:N this works in both ways, either from owning side, or from inverse side.

// many to many works both from owning side and from inverse side
const book = new Book(...);
const tag = new BookTag(...);
console.log(tag.books.contains(book)); // true
console.log(book.tags.contains(tag)); // true

Collections on both sides have to be initialized, otherwise propagation won't work.

Although this propagation works also for M:N inverse side, you should always use owning side to manipulate the collection.

Same applies for Collection.remove().

← Back to table of contents

Last updated on by Martin Adámek