Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/aldaviva/mongoose-moment
🕰 Store Moment dates in Mongo using Mongoose internally as millisecond timestamps.
https://github.com/aldaviva/mongoose-moment
momentjs mongoose mongoose-plugin orm timestamps
Last synced: about 1 month ago
JSON representation
🕰 Store Moment dates in Mongo using Mongoose internally as millisecond timestamps.
- Host: GitHub
- URL: https://github.com/aldaviva/mongoose-moment
- Owner: Aldaviva
- License: apache-2.0
- Created: 2014-03-29T09:42:16.000Z (over 10 years ago)
- Default Branch: master
- Last Pushed: 2017-02-05T07:43:54.000Z (almost 8 years ago)
- Last Synced: 2024-10-11T12:20:11.474Z (about 1 month ago)
- Topics: momentjs, mongoose, mongoose-plugin, orm, timestamps
- Language: JavaScript
- Homepage: https://www.npmjs.com/package/mongoose-moment
- Size: 19.5 KB
- Stars: 9
- Watchers: 3
- Forks: 9
- Open Issues: 1
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
mongoose-moment
===============Store [Moment](http://momentjs.com/docs) dates in [Mongo](http://docs.mongodb.org/manual/) using [Mongoose](http://mongoosejs.com/docs/index.html) internally as millisecond timestamps.
## Installing
$ npm install mongoose moment mongoose-moment
```javascript
var mongoose = require('mongoose')
require('mongoose-moment')(mongoose);
```## Configuration
Use the new SchemaType `'Moment'` in your schema definitions:
```javascript
var mySchema = new mongoose.Schema({
created: 'Moment'
});
var MyModel = new mongoose.Model('MyModel', mySchema);
```## Usage
### Writing models
```javascript
var Moment = require('moment');
var myModel = new MyModel({ created: new Moment() });
myModel.save();
```### Reading models
```javascript
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
```javascript
MyModel.find({ created: new Moment('2014-03-29').valueOf() }, function(err, docs) {});
```## Thanks
* Aaron Heckmann's [mongoose-regexp](https://github.com/aheckmann/mongoose-regexp)