Mongoose + CoffeeScript 'default' property - Getting it to work? - node.js

As you know, MongooseJS has a "default" property available. For instance, if I want a Date property on my object, and I want that date to automatically default to the time at which the record is created, I would define it in the schema as:
var myObject = mongoose.Schema({
date: {type: Date, default: Date.now}
});
Now, the problem with doing this in CoffeeScript is that default is a reserved keyword in JavaScript, so the CoffeeScript compiler automatically wraps default in double quotes, so this CoffeeScript code:
myObject = mongoose.Schema
date:
type: Date
default: Date.now
is compiled as:
var myObject;
myObject = mongoose.Schema({
date: {type: Date, "default": Date.now}
});
This results in the default parameter not working as intended. Perhaps I'm missing something but everything I have tried just is not working. I shouldn't need to manually set the date when saving the record, as the default keyword already provides this functionality.
Does anyone know how to get around this?

I have to admit I hate CoffeeScript and the like, but you probably might get around this by doing something like this:
var schema = {
type: Date
};
schema["default"] = Date.now;
myObject = mongoose.Schema(schema);

So, the solution to my problem was rather simple, and a rookie mistake: I forgot to specify the Date property to return in myObject.find()...

Related

Using MomentJS for Default Date in MongoDB Document

I'm trying to use MomentJS to get non-localized UTC time and set that as the default for new documents created in Mongo.
var SubFavoriteSchema = new Schema({
user : { type: String, ref: 'Account'},
date : {type: Date, default: moment.utc()}
});
The problem is, moment.utc() returns the date that the Node server started. Thus if I start the server on Jan 1, all the documents get a UTC time of Jan 1, even if the document is created on Jan 10.
Any idea why it keeps getting the server's start time instead of the current time?
The problem is that you're calling the moment.utc() function (once) when creating the schema, so the same resulting value is used when creating new documents.
However, you don't need to use moment for this, default: Date.now will do what you want as that function returns the current UTC time. Note that you don't call the now function, you just pass the function itself. That's the key difference.
You need to use a factory function for that.
var currDate(){
return function(){
return moment.utc();
}
}
var SubFavoriteSchema = new Schema({
user : { type: String, ref: 'Account'},
date : {type: Date, default: currDate()}
});

MongoDB date index

I am using node.js and mongodb for my small application and I want to index my document by date object. For Example
var MySchema = new Schema({
created_at: { type: Date, default: Date.now }
});
myschema.index({created_at: -1});
But how I understand, each object will have nearly unique create_id field. Wouldit work well ? and would this method give me effect. If you can, please also send me any articles about mongodb date indexing.

Constant property value in mongoose schema

I have schema where a property always equals 1. I have found a solution, but I don't like it:
var schema = new Schema({
a: Number
});
schema.pre('save', function(){
this.a = 1;
});
Can you please tell me if there is better way to do this? For example:
var schema = new Schema({
a: 1
});
How about using a default value, does it achieve what you want ?
var schema = new Schema({
a: {type: Number, default: 1}
});
If you want to force it, the pre version is the best option.
Another way to achieve this is to use a virtual property. Virtuals are document properties that you can get and set but that do not get persisted to MongoDB. Instead you can specify a getter function that get's called every time you access the a property:
schema.virtual('a').get(function () {
return 1;
});
Now every document of schema will have a property a that equals 1. Note however that because virtuals don't get persisted you are not able to query for them.
Store constants as model properties.
var mySchema = new Schema({
// ...
});
var myModel = mongoose.model('MyModel', mySchema);
myModel.a = 1;
Maybe too late, but for the future, you could use default value with a custom setter that always returns the old value, something like ...
var schema = new Schema({
a: {
type: Number,
default: 1,
set(value) {
return this.a;
},
}
});
The default option will initialize the field and the custom setter will ignore any new value and always reset the field to its previous value (that you set with default).

Mongoose Model Options field

Hi I just started playing with Mongoose. It seems pretty awesome!
Now coming from a Django background, how would one implement a type of options field like:
STATUS_OPTIONS : [{"Open",1},{"Closed",2},{"Pending",3"}]
status: { type:String, required:true, options:STATUS_OPTIONS },
So that it can be set like status = Open or something like that.
Or should this just be a normal String field and I set it accordingly in my app?
You can constrain a Mongoose schema string field to a set of enumeration values with the enum attribute:
var s = new Schema({
status: { type: String, enum: ['Open', 'Closed', 'Pending'] }
});
What you may be trying to do is reference some possibilities, right? Probably like an enum field type.
Well, you may have better luck using directly an String or using another Schema (but if you only need the strings Closed, Open, Pending, this wouldn't be needed).

How to update a mongodb/mongoose schema from a single value field to a multi value field

I have a schema defined in Mongoose like this:
var Stuff = new Schema({
href: String,
thing: Number,
});
But now the "thing" field is more complicated than a single number, so I'd like to update my model make "thing" have embedded fields:
var Stuff = new Schema({
href: String,
thing: { thinglabel: String,
thingvalue: Number}
});
I'm wondering if there is an elegant way to do this. For the time being, I've hacked around this problem by adding a second field but I was thinking that there might be a better solution the next time I run into this problem.

Resources