LoginSignup
6
7

More than 5 years have passed since last update.

MongooseでSchemaにデフォルトの配列を定義する方法

Last updated at Posted at 2014-04-10

ごめんなさい。元の内容嘘でした。直接defaultに設定できます。

var schema = new Schema({
    mycolumn: { 
      type: [String], 
      default: ["DefaultString1", "DefaultString2"]
    }
});

もしも一個だったら配列にしないでおいても融通きかせて配列にしてくれます

var schema = new Schema({
    mycolumn: { 
      type: [String], 
      default: "DefaultString1"
    }
});

以下の内容は嘘です。

https://groups.google.com/forum/#!topic/mongoose-orm/W-fVEyx3O9A
から
https://gist.github.com/aheckmann/3801422
へ誘導されて

var schema = new Schema({
    name: String
  , docs: { type: [docs], default: function () {
      // need to manaually add the ObjectId in this case
      return [{ name: 20, _id: new mongoose.Types.ObjectId }]
    }}
});

ようするに、defaultに関数を設定できるということだそうだ。

var schema = new Schema({
    mycolumn: { 
      type: [String], 
      default: function () {
          return ["DefaultString1", "DefaultString2"];
      }
    }
});
6
7
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
6
7