Shaun Xu

The Sheep-Pen of the Shaun


News

logo

Shaun, the author of this blog is a semi-geek, clumsy developer, passionate speaker and incapable architect with about 10 years’ experience in .NET and JavaScript. He hopes to prove that software development is art rather than manufacturing. He's into cloud computing platform and technologies (Windows Azure, Amazon and Aliyun) and right now, Shaun is being attracted by JavaScript (Angular.js and Node.js) and he likes it.

Shaun is working at Worktile Inc. as the chief architect for overall design and develop worktile, a web-based collaboration and task management tool, and lesschat, a real-time communication aggregation tool.

MVP

My Stats

  • Posts - 122
  • Comments - 542
  • Trackbacks - 0

Tag Cloud


Recent Comments


Recent Posts


Article Categories


Archives


Post Categories


.NET



In Worktile Pro we are using MongoDB and Mongoose as the backend database with Mongoose Node.js module for ODM (Object Document Mapping) framework.

Although Mongoose brings "Schema" back it still provides some advantages such as validation, abstraction, reference and default value. In this post I will talk about the default value based on a bug I found when developing Worktile Pro.

 

We can define default value against a model  schema. In the code below I have a blog schema, with default value against "created_by" and "created_at" properties.

1
2
3
4
5
let BlogSchema = new Schema({
    title: {type: String},
    created_by: {type: String, default: 'Shaun Xu'},
    created_at: {type: Number, default: Date.now}
});

If we tried to add a new blog document without specifying them, Mongoose will use default value as below.

1
2
3
4
5
6
7
8
let blog1 = new BlogModel({
    title: 'Implement Schema Inherence in Mongoose'
});

co(function *() {
    let savedBlog1 = yield blog1.save();
    console.log(`Blog: ${JSON.stringify(savedBlog1, null, 2)}`);
});

 

But if we specified "null", they will be marked as "defined" so that Mongoose will set "null" rather than the default value.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
let blog2 = new BlogModel({
    title: 'Angular Toolkits: Customizable Bootstrap Tabs',
    created_by: null,
    created_at: null
});

co(function *() {
    let savedBlog2 = yield blog2.save();
    console.log(`Blog: ${JSON.stringify(savedBlog2, null, 2)}`);
});

 

How about "undefined"? Let's look at the code below. Will Mongoose use default value?

1
2
3
4
5
let blog3 = new BlogModel({
    title: 'Pay Attention to "angular.merge" in 1.4 on Date Properties',
    created_by: undefined,
    created_at: undefined
});

The result was shown below, Mongoose didn't use default value for "blog3".

Screen Shot 2016-01-26 at 17.36.27

This means even though we set property to "undefined", Mongoose will NOT use default value. This is because Mongoose just check if the document object contains properties. If yes, no matter it has value, it's "null", or even it's "undefined", it was defined. So Mongoose will NOT use default value.

 

So finally rule: If you want Mongoose to use default value on a property, do NOT specify it in the document object, even though set it to "null" or "undefined".

 

Hope this helps,

Shaun

All documents and related graphics, codes are provided "AS IS" without warranty of any kind.
Copyright © Shaun Xu. This work is licensed under the Creative Commons License.

Comments

Gravatar # geekwithblog question
Posted by jc on 1/27/2016 9:26 PM
how do u post a blog on geekwithblog? i tried posting blog on the site but it never show up on homepage. not even most recent.
Gravatar # re: When Mongoose Uses Schema Default Value
Posted by Shaun Xu on 1/28/2016 9:46 AM
@jc please send email to support {at} geekswithblogs {dot} com for help when you fond issues with GWB. Just a kind reminder, if you are publishing posts through GWB web admin page, ensure to check "public", "not-draft" or something else checkboxes.
Gravatar # re: When Mongoose Uses Schema Default Value
Posted by fineticket on 6/14/2016 9:41 PM
Very informative post.Thanks for sharing with us all.
instafollowersjunction.com

Post A Comment
Title:
Name:
Email:
Comment:
Verification: