Giter Club home page Giter Club logo

mongoose-moment's Introduction

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

mongoose-moment's People

Contributors

aldaviva avatar brad-decker avatar gastonelhordoy avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

mongoose-moment's Issues

Required validator fails, even when a value is provided.

If I define a schema like this.

mongoose.schema({
    "occurredAt": {
        "type": "Moment",
        "required": true
    }
});

Mongoose throws a ValidatorError llike this.

{
    "occurredAt": {
        "message": "Path `occurredAt` is required.",
        "name": "ValidatorError",
        "properties": {
            "type": "required",
            "message": "Path `{PATH}` is required.",
            "path": "occurredAt",
            "value": 1472116740000
        },
        "kind": "required",
        "path": "occurredAt",
        "value": 1472116740000
    }
}

Note that the error specifically indicates that value": 1472116740000 was present, even though it is throwing a ValidatorError. This is strange behavior that goes away when I change the type of the property from "Moment" to some other thing.

How to format several fields from querying mongo

How would I parse a query result from mongo?

At the instrucctions you say

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

BUt supose you have a model with several fields like:

var mySchema = new mongoose.Schema({
Name: String,
created: 'Moment'
Street :String
});

How woud you parse internaly each created field for every row in my mongo db database?

In your example you only do a console.log for one registry but what por a query for a full collection in mongo?

var mySchema = MyEschema.find().exec( function(err, list){
response.json(list)
});

Feature Request: Migrate to storing Date objects

MongoDB now supports storing dates. I'd love to still use Moments in code, but it would be nice to migrate all the stored values to Date objects for more readability when using the Mongo console.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.