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-momentNo compatibility data collected yet for this library.
Verified import paths — ran on the pinned version, not inferred.
Shows registering the custom schema type, saving a document with a Moment value, and querying with manual value conversion.
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.
Always convert Moment objects to milliseconds: Model.find({ field: moment('2024-01-01').valueOf() }) or use $gte/$lte with raw numbers.Call require('mongoose-moment')(mongoose); right after requiring mongoose and before any schema definition.Use exactly 'Moment' in schema definitions.
const mongoose = require('mongoose'); require('mongoose-moment')(mongoose);Ensure require('mongoose-moment')(mongoose) is called before schema definition, and use 'Moment' type in schema.Use Model.find({ field: moment('2024-01-01').valueOf() }) to query with milliseconds.