Install & Compatibility
Where this runs
No compatibility data collected yet for this library.
Code
Verified usage
Verified import paths — ran on the pinned version, not inferred.
Entity
✓ import { Entity } from 'type-mongodb'
✗ const Entity = require('type-mongodb').Entity
ESM-only since v2.0.0-beta. TypeScript decorator export.
getRepository
✓ import { getRepository } from 'type-mongodb'
✗ import { Repository } from 'type-mongodb'
getRepository is a function, not a class. Repository instances are created via getRepository().
createConnection
✓ import { createConnection } from 'type-mongodb'
✗ import { connect } from 'type-mongodb'
Use createConnection() to establish a MongoDB connection. 'connect' does not exist.
Defines a User entity using decorators, establishes a MongoDB connection, saves and retrieves documents using a repository.
import 'reflect-metadata';
import { createConnection, Entity, PrimaryGeneratedColumn, Column, getRepository } from 'type-mongodb';
@Entity('users')
class User {
@PrimaryGeneratedColumn('uuid')
_id: string;
@Column()
name: string;
@Column({ type: 'int' })
age: number;
}
async function main() {
const connection = await createConnection({
host: 'localhost',
port: 27017,
database: 'test'
});
const userRepository = getRepository(User);
const user = new User();
user.name = 'Alice';
user.age = 30;
await userRepository.save(user);
const users = await userRepository.find();
console.log(users);
await connection.close();
}
main().catch(console.error);
Debug
Known issues
breakingv2.0.0-beta changed from CommonJS to ESM only. Use import syntax instead of require(). This breaks Node.js versions <12 and projects not configured for ES modules.fixConvert your project to use ES modules (type: module in package.json) and replace require() with import statements.
affects: >=2.0.0-beta <3
breakingv2.0.0-beta dropped support for MongoDB driver v3. Only MongoDB driver ^4.1.0 is supported now.fixUpdate your mongodb package to version ^4.1.0 or later.
affects: >=2.0.0-beta
deprecatedThe 'useNewUrlParser' option is deprecated in MongoDB driver v4. type-mongodb no longer uses it.fixRemove useNewUrlParser, useUnifiedTopology options from connection config.
affects: >=2.0.0-beta <2.0.0
gotchaThis package requires reflect-metadata to be imported at the top of the entry file before using decorators. Omitting it causes 'Reflect is not defined' errors.fixAdd import 'reflect-metadata' as the first line of your application entry file.
affects: >=1.0.0
gotchaColumn types are limited to MongoDB BSON types. Using unsupported types (e.g., 'bigint') may cause runtime errors.fixUse compatible types: string, number, boolean, Date, ObjectId, Buffer, etc.
affects: >=1.0.0 <3
Errors
Common errors & fixes
Reflect is not defined
reflect-metadata polyfill not imported at application entry point.
fixAdd import 'reflect-metadata' at the very top of your main file.
MongoError: auth fails
Incorrect database credentials or authentication mechanism not supported by MongoDB driver v4.
fixVerify username, password, and database name. For SCRAM-SHA-256, ensure MongoDB server version >=4.0.
Cannot use import statement outside a module
Running ES module import syntax in a CommonJS project.
fixAdd type: module to package.json or rename file to .mjs.
Upgrade
Version history
2.0.0-beta.12latest on npm
Audit
Dependencies
mongodbrequiredCore database driver for MongoDB connections and operations. Required peer dependency.
reflect-metadatarequiredRequired polyfill for experimental decorator support in TypeScript. Must be imported early in application entry point.