Version: 2.2.0 (última)

MongoDB (noSQL)

Creating a new project#

To generate a new project that uses MongoDB, run the command createapp with the flag --mongodb.

foal createapp my-app --mongodb


npm install mongodb
type: mongodb
url: mongodb://localhost:27017/test-foo-bar

Defining Entities and Columns#

More documentation here:

The definition of entities and columns is the same as in relational databases, except that the ID type must be an ObjectID and the column decorator must be @ObjectIdColumn.

import { Entity, ObjectID, ObjectIdColumn, Column } from 'typeorm';
export class User {
id: ObjectID;
firstName: string;
lastName: string;


import { getMongoRepository } from 'typeorm';
const user = await getMongoRepository(User).findOne('xxxx');


The fetchMongoDBUser function#


import { Entity, ObjectID, ObjectIdColumn } from 'typeorm';
export class User {
id: ObjectID;

Example with JSON Web Tokens:

import { JWTRequired } from '@foal/jwt';
import { fetchMongoDBUser } from '@foal/typeorm';
import { User } from '../entities';
@JWTRequired({ user: fetchMongoDBUser(User) })
class MyController {}

The MongoDBStore#

npm install @foal/mongodb

If you use sessions with @UseSessions, you must use the MongoDBStore from @foal/mongodb. The TypeORMStore does not work with noSQL databases.


When using MongoDB, there are some features that are not available:

  • the foal g rest-api <name> command,
  • and the Groups & Permissions system.