node.js - MongoDB default values are not saved, instead re-calculated at runtime -


i'm building simple rest app on yeoman express mvc generator mongodb.

this mongodb/mongoose model (updated complete update.js model):

var mongoose = require('mongoose'),     schema = mongoose.schema;  var updateschema = new schema({     title: string,     text: string,     authors: string,     url: string,     imageurl: string,     datecreated: { type: date, default: date.now },     reloadneeded: { type: boolean, default: true } });  mongoose.model('update', updateschema); 

this data looks in mongo client:

> db.updates.find(); { "_id" : objectid("5476453f8920d05ecdef4eec"), "title" : "hello world", "text" : "yoda yoda" } { "_id" : objectid("547653748920d05ecdef4eed"), "title" : "hihi", "text" : "mookie" } 

and json output express app:

[     {"_id":"5476453f8920d05ecdef4eec","title":"hello world","text":"yoda yoda","reloadneeded":true,"datecreated":"2014-11-27t10:50:10.078z"},     {"_id":"547653748920d05ecdef4eed","title":"hihi","text":"mookie","reloadneeded":true,"datecreated":"2014-11-27t10:50:10.078z"} ] 

so, datecreated , reloadneeded set at runtime - i'd rather want them set (and persisted) when create documents. what's going on?


update: seems values are persisted if create mongoose rather mongodb shell.

do use mongoose data model? if so, default values created on document construction http://mongoosejs.com/docs/2.7.x/docs/defaults.html

anyway assume reason in defaults


Comments

Popular posts from this blog

javascript - Any ideas when Firefox is likely to implement lengthAdjust and textLength? -

matlab - "Contour not rendered for non-finite ZData" -

delphi - Indy UDP Read Contents of Adata -