Registry / database / mongodb-moment

mongodb-moment

JSON →
library1.0.2jsnpmunverified

Plugin for the MongoDB Node.js driver that enables automatic serialization of Moment.js objects to Date objects when saving to MongoDB, and deserialization when retrieving. Current version 1.0.2 is stable, with no updates since 2017. It mutates the global Moment prototype to add a custom `toBSON` method, making it transparent to use. Compared to alternatives like `moment-mongoose` or manual transformation, this package integrates directly with the driver's serialization cycle.

npm install mongodb-moment
INSTALL
IMPORT
SIG · MONGODB-MOMENT
M
mongodb-moment
databasejavascriptv1.0.2
harness data pending
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.

default
require('mongodb-moment')(moment);
import mongodbMoment from 'mongodb-moment';
Module exports a function that must be called with Moment.js as argument. Only CommonJS supported.
toBSON
const moment = require('moment'); require('mongodb-moment')(moment);
const { toBSON } = require('mongodb-moment');
toBSON is added to Moment prototype, not exported.
typeaugmentation
import 'mongodb-moment'; // assuming type definitions exist
import { MongoMoment } from 'mongodb-moment';
No TypeScript types provided; package modifies global types implicitly.

Shows how to register mongodb-moment and save/load a Moment.js object as a Date in MongoDB.

const moment = require('moment'); require('mongodb-moment')(moment); const { MongoClient } = require('mongodb'); async function run() { const client = new MongoClient('mongodb://localhost:27017'); await client.connect(); const db = client.db('test'); const collection = db.collection('dates'); const doc = { name: 'event', date: moment.utc('2023-01-01') }; await collection.insertOne(doc); const saved = await collection.findOne({ name: 'event' }); console.log(saved.date instanceof Date); // true await client.close(); } run().catch(console.error);
Debug
Known issues
breakingmongodb-moment mutates the global Moment prototype; may conflict with other libraries that also modify Moment.
fix
Use isolated Moment instances or serialize manually to avoid prototype pollution.
affects: >=0.0.0
deprecatedPackage uses deprecated MongoClient.connect callback style in tests.
fix
Ensure you use async/await or Promise-based connection; mongo driver v3+ requires this.
affects: >=1.0.0
gotchaOnly works with CommonJS; ESM imports are not supported. May cause runtime errors in ESM projects.
fix
Use dynamic import or switch to CommonJS. Alternatively, apply serialization manually.
affects: >=1.0.0
gotchaNo TypeScript definitions; using with TypeScript will require custom type declarations.
fix
Create a .d.ts file declaring module augmentation for Moment.
affects: >=1.0.0
deprecatedPackage has not been updated since 2017; potential incompatibility with newer MongoDB driver versions.
fix
Test with your MongoDB driver version; consider alternatives like manual serialization.
affects: >=1.0.0
Errors
Common errors & fixes
TypeError: moment(...).toBSON is not a function
mongodb-moment was not applied to the moment instance; or called with wrong argument.
fix
Ensure you call require('mongodb-moment')(moment) before using moment objects with MongoDB.
Cannot use import statement outside a module
Attempting to use ES module syntax (import) with this CommonJS-only package.
fix
Use require() instead of import, or place the code in a CommonJS file.
MongoError: unknown message type
Incompatibility with MongoDB Node.js driver version 4+ due to binary protocol changes.
fix
Downgrade MongoDB driver to version 3.x or handle serialization manually.
Upgrade
Version history
1.0.2latest on npm
Audit
Dependencies
mongodbrequiredModifies MongoDB driver's serialization behavior
momentrequiredMutates Moment.js prototype to add toBSON
Agent activity
6 hits · last 30 days
node
6
Resources
mongodb-moment — npm install mongodb-moment · libregistry