Skip to main content
Version: 2.6.0 (terbaru)

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
database:  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';
@Entity()export class User {        @ObjectIdColumn()    id: ObjectID;        @Column()    firstName: string;        @Column()    lastName: string;    }


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


The fetchMongoDBUser function#


import { Entity, ObjectID, ObjectIdColumn } from 'typeorm';
@Entity()export class User {
  @ObjectIdColumn()  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.