Registry / database / mongoose-moment

mongoose-moment

JSON →
library0.1.3jsnpmunverified

Mongoose schema type for storing Moment.js dates as millisecond timestamps in MongoDB. Current version 0.1.3, last updated in 2014. This package registers a custom schema type 'Moment' that automatically converts Moment instances to UNIX timestamps (ms) on save and hydrates them back to Moment objects on read. It requires mongoose >=3.5.7 and moment 2.x as peer dependencies. Key differentiator: seamless integration with Moment.js for date manipulation in Mongoose schemas. Note: query values must be manually converted to timestamps using .valueOf() as Mongoose does not cast query parameters by default. The package is unchanged since 2014 and may not work with modern Mongoose versions.

npm install mongoose-moment
INSTALL
IMPORT
SIG · MONGOOSE-MOMENT
M
mongoose-moment
databasejavascriptv0.1.3
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.

mongoose-moment
require('mongoose-moment')(mongoose);
import mongooseMoment from 'mongoose-moment'; mongooseMoment(mongoose);
The package exports a function that must be called with the mongoose instance. ESM import may not work; use CommonJS require() as shown.
Moment
import moment from 'moment';
const Moment = require('mongoose-moment').Moment;
Moment.js instances come from the 'moment' package, not mongoose-moment. The schema type is string 'Moment' in schema definitions, not a constructor.
Schema Type 'Moment'
var schema = new mongoose.Schema({ field: 'Moment' });
var schema = new mongoose.Schema({ field: Moment });
In the schema definition, use the string 'Moment' (case-sensitive) as the type, not the required Moment object.

Shows registering the custom schema type, saving a document with a Moment value, and querying with manual value conversion.

const mongoose = require('mongoose'); require('mongoose-moment')(mongoose); const moment = require('moment'); const schema = new mongoose.Schema({ createdAt: 'Moment' }); const Model = mongoose.model('Test', schema); // Save const doc = new Model({ createdAt: moment() }); doc.save((err) => { if (err) console.error(err); else console.log('Saved'); }); // Query (must use .valueOf()) Model.find({ createdAt: moment('2024-01-01').valueOf() }, (err, docs) => { if (err) console.error(err); else docs.forEach(d => console.log(d.createdAt.format())); });
Debug
Known issues
deprecatedPackage is no longer maintained and may not work with current Mongoose versions (4.x+). Mongoose now supports custom schema types natively, and Moment.js is considered legacy.
fix
Use Mongoose's built-in Date type or a custom schema type using mongoose.Schema.Types. Alternatively, consider using native Date objects or a newer library like Luxon.
affects: >=0.0.0
gotchaQuery values are not automatically cast to timestamps; you must manually call .valueOf() on Moment instances.
fix
Always convert Moment objects to milliseconds: Model.find({ field: moment('2024-01-01').valueOf() }) or use $gte/$lte with raw numbers.
affects: >=0.0.0
gotchaRequire the package and call it as a function with mongoose instance immediately. Doing it too late or in a callback may cause errors.
fix
Call require('mongoose-moment')(mongoose); right after requiring mongoose and before any schema definition.
affects: >=0.0.0
gotchaThe schema type string is case-sensitive: 'Moment' (capital M). Using 'moment' will be interpreted as unknown type.
fix
Use exactly 'Moment' in schema definitions.
affects: >=0.0.0
Errors
Common errors & fixes
TypeError: mongoose_moment is not a function
Using ESM import or calling require incorrectly; the package exports a function that expects mongoose.
fix
const mongoose = require('mongoose'); require('mongoose-moment')(mongoose);
CastError: Cast to number failed for value "Moment<...>" at path "createdAt"
Trying to save a Moment object without the custom type registered, or saving before calling the initialization.
fix
Ensure require('mongoose-moment')(mongoose) is called before schema definition, and use 'Moment' type in schema.
No query results returned even though documents exist with matching dates
Query is using Moment object directly instead of timestamp value.
fix
Use Model.find({ field: moment('2024-01-01').valueOf() }) to query with milliseconds.
Upgrade
Version history
0.1.3latest on npm
Audit
Dependencies
mongooserequiredPeer dependency required for schema type registration and model creation.
momentrequiredPeer dependency required for Moment.js instance handling.
Agent activity
8 hits · last 30 days
node
6
Amazon
1
Resources
mongoose-moment — npm install mongoose-moment · libregistry