3
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;

This is my model class, i'm sending values through post-man tool, The default values inside the array of fields eg.

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

the status and div are not storing the default values

i need to store the values of status and div as default!

2 Answers 2

7

You could use a sub-scheme/document instead!

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

This way you you can do patient.text.push({}) for adding a default patientTextSchema, or patient.text.push({status: "another_status"}) for a (partially) filled scheme.

Source: http://mongoosejs.com/docs/subdocs.html

Sign up to request clarification or add additional context in comments.

2 Comments

So you want me to have the text field as a schema and then push it inside the text field(Asking to make sure i have understood the answer).
Yes you understood correctly, It looks like a lot more work, but its also reusable, so it saves you time when you want to have a text field somewhere else too!
2

You can use the following way to create Array of Objects with a default in mongoose:

const organisationSchema = new mongoose.Schema({
        name: {
            type: String,
            required: true
        },
        brands: {
            type: [
                {
                    type: mongoose.Schema.Types.ObjectId,
                    ref: 'brand'
                }
            ],
            default: []
        },
        users: {
            type: [
                {
                    type: mongoose.Schema.Types.ObjectId,
                    ref: 'user'
                }
            ],
            default: []
        }
    }, { timestamps: true });

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.