Mongoose schema to store an array of JSON objects for subdocuments

I have a JSON object that I want to create a schema for using mongoose

    { ProjectName: 'asdf',
  Embargo: 'Yes',
  Angle: '1',
  Facts: '[{"count":1,"fact":"dsafdsaf","content":"dsafdsaf"}, {"count":3,"fact":"dsafdsaf","content":"dsafdsaf" } , {"count":2,"fact":"dsafdsaf","content":"dsafdsaf"}]',
  About: '<p>Anthony Bodin&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</p>',
  EditorNote: 'No',
  OrderId: 'tok_14kGRO2Jju1nvjb47YF9jQTJ',
  Payment: 'Paid' }

      

My problem is that the Facts item will contain an array of objects as well, and I'm not really sure how to store it in the database as Here is the schema I have right now

var ArticleSchema = new mongoose.Schema({
    userId: {
        type: String,
        default: ''
    },
    userEmail: {
        type: String,
        default: ''
    },
    article: {
        date: {
            type: Date,
            default: Date()
        },
        ProjectName: {
            type: String,
            default: ''
        },
        Embargo: {
            type: String,
            default: true
        },
        Angle: {
            type: String,
            default: ''
        },
        Facts: {                
            type:Array
        },
        About: {
            type:String,
            default:''
        },
        EditorNote: {
            type:String,
            default:''
        },  
        Payment: {
            type: String,
            default: 'Not Paid'
        },
        OrderId: {
            type:String,
            default:''
        }
    }
});

      

Is storing data as an array the correct way to store the Facts element?

+3


source to share


1 answer


It's very simple. Just make facts an array. So change

Facts: {                
            type:Array
        },

      



before Facts: []

and it will be able to store an array of objects you have in a Facts

json field .

+16


source







All Articles