Skip to content

Store Moment dates in Mongo using Mongoose internally as millisecond timestamps.

License

Notifications You must be signed in to change notification settings

gastonelhordoy/mongoose-moment

 
 

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

25 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

mongoose-moment

Store Moment dates in Mongo using Mongoose internally as millisecond timestamps.

Installing

$ npm install mongoose moment mongoose-moment
var mongoose = require('mongoose')
require('mongoose-moment')(mongoose);

Configuration

Use the new SchemaType 'Moment' in your schema definitions:

var mySchema = new mongoose.Schema({
	created: 'Moment'
});
var MyModel = new mongoose.Model('MyModel', mySchema);

Usage

Writing models

var Moment = require('moment');
var myModel = new MyModel({ created: new Moment() });
myModel.save();

Reading models

var myModel = MyModel.findById('123', function(err, doc){
	console.log(doc.created.format());
});

Querying for models

Mongoose doesn't transform query values according to schemas, so you'll have to do it yourself. Internally, Moment values are stored in the database as the number of milliseconds since the Unix epoch. To find a document using a Moment field, use a query like

MyModel.find({ created: new Moment('2014-03-29').valueOf() }, function(err, docs) {});

Thanks

About

Store Moment dates in Mongo using Mongoose internally as millisecond timestamps.

Topics

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages

  • JavaScript 97.5%
  • Makefile 2.5%