2017-08-13 3 views
0

私はカスタムマングーススキーマを作成したいと思います。カスタムマングーススキーマ

は私が

{ 
    "year": 2016, 
    "day": 306 
} 

などのJSONファイルを持っていると私のスキーマは、私は一日306 2016年11月1日に変換する関数を持っている

Schema = mongoose.Schema; 

var FluxSchema = new Schema({ 
    year: { type: Number }, 
    day: { type: Number }}) 

です。私は、データベースにデータが挿入されると自動的にこの関数を呼び出すカスタムスキーマを作成できるかどうかを知りたいと思います。

答えて

0

スキーマをインポートして.saveメソッドを使用できます。 例:

var FluxSchema = require('path/to/schema/fluxSchema'), 
    yourJSON = require('path/to/yourjson.json'); 
// ...some other code 
var flux = new FluxSchema({ 
      year: yourJSON.year, 
      day: yourJSON.day 
     }); 
flux.save(function(err){ 
    if(err) throw err; 
    // Your convertion code 
});