最新消息: 电脑我帮您提供丰富的电脑知识,编程学习,软件下载,win7系统下载。

将默认值设置为mongoose中的对象数组

IT培训 admin 5浏览 0评论

将默认值设置为mongoose中的对象数组

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;

这是我的模型类,我通过post-man工具发送值,字段数组中的默认值,例如。

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

status和div不存储默认值

我需要将status和div的值存储为默认值!

回答如下:

您可以使用子方案/文档代替!

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]
})

通过这种方式,您可以使用patient.text.push({})为(部分)填充方案添加默认的patientTextSchema或patient.text.push({status:“another_status”})。

资料来源:http://mongoosejs/docs/subdocs.html

将默认值设置为mongoose中的对象数组

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;

这是我的模型类,我通过post-man工具发送值,字段数组中的默认值,例如。

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

status和div不存储默认值

我需要将status和div的值存储为默认值!

回答如下:

您可以使用子方案/文档代替!

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]
})

通过这种方式,您可以使用patient.text.push({})为(部分)填充方案添加默认的patientTextSchema或patient.text.push({status:“another_status”})。

资料来源:http://mongoosejs/docs/subdocs.html

发布评论

评论列表 (0)

  1. 暂无评论