2017-12-20 12 views
0
内のオブジェクトの配列にデフォルト値を設定する
var mongoose = require('mongoose'); 
var Schema = mongoose.Schema; 

var patientSchema = new Schema({ 
    resourceType : {type :String, default : 'Patient' }, 
    id : {type : String, default : 'example'}, 
    text : [{ 
     status : {type : String, default : 'generated'}, 
     div :{type : String, default :'<div> Something </div>'} 
    }], 
    active : {type : String, default : 'true'}, 
    identifier : [{ 
     use : {type : String, default : 'official'}, 
     system : {type : String, default : 'urn:oid:1.2.36.146.595.217.0.1'}, 
     assinger :[{ 
      display : {type : String, default : 'Acme Healthcare'}, 
     }] 

    }], 
    name: [{ 
     use : {type : String, default : 'official'}, 
     first_name : {type : String, default : ''}, 
     second_name : {type : String, default : ''} 
    }], 
    gender :{type : String, default : ''}, 
    birthDate :{type : String, default : ''}, 
    telecom : [{ 
     system : {type : String, default : ''}, 
     value : {type : String, default : ''} 
    }], 
    address : [{ 
     use : {type : String, default : 'official'}, 
     text : {type : String, default : ''}, 
     city : {type : String, default : ''}, 
     district : {type : String, default : ''}, 
     state : {type : String, default : ''}, 
     postalcode :{type : String, default : ''} 
    }] 
}); 

var patients = mongoose.model('Patients',patientSchema); 
module.exports = patients; 

これは私が、ポストマンツールからフィールドなどの配列内部 デフォルト値を値を送信しています、私のモデルクラスです。 マングース

text : [{ 
     status : {type : String, default : 'generated'}, 
     div :{type : String, default :'<div> Something </div>'} 
    }], 

状態とdivの

は、デフォルト値

私はデフォルトとして地位とDIVの値を格納する必要が

を格納していません!

答えて

2

代わりにサブスキーム/ドキュメントを使用することができます。

var patientTextSchema = new Schema({ 
    status : {type : String, default : 'generated'}, 
    div :{type : String, default :'<div> Something </div>'} 
}); 

... ommited for clarity 
var patientSchema = new Schema({ 
    text: [patientTextSchema] 
}) 

あなたはデフォルトのpatientTextSchema、またはpatient.text.pushを追加するためのpatient.text.push({})を行うことができますこの方法:(部分的に)満たされたスキームのために({状態 "another_status"}) 。

出典:http://mongoosejs.com/docs/subdocs.html

+0

だからあなたは(私は答えを理解していることを確認するために頼む)私は、スキーマとしてテキストフィールドを持っているし、テキストフィールド内にそれをプッシュします。 – Grijan

+1

はい、あなたは正しく理解しました。それはもっと多くの作業のように見えますが、再利用できるので、テキストフィールドを別の場所に置いておきたいときにも時間を節約できます! –