jtbitt
jtbitt

Reputation: 581

Which schema type should I use for an attribute that is an Array of Objects in Mongoose?

I've been searching through questions on here/Google, but haven't found a clear answer. I feel like this should be straightforward.

I am making my personal portfolio site with MEAN stack, have all the front end done, and want to load all my site's content info from the backend, instead of doing it from my Angular Controller. One example of content to be loaded is different portfolio pieces into my html. Right now, those portfolio pieces are in my Angular controller, are being defined there, and are an array of objects, with 'name', 'language', 'reference', and 'image reference' as my attributes for each project.

What I would like to do instead is make a content model in my backend, which has all my site's content in it, which each piece of content being an attribute of the 'Content' model, create routes for it, and link it via Angular services.

I'm not worried about the practicality of using Node/Mongoose here because I'm doing it so I can demonstrate my knowledge of how to use services/node backend, and connecting the two.

Here is what the portfolio array looks like in my controller (I want to store this in the backend instead) -

vm.projects = [
{
  name: 'Tic-Tac-Toe',
  language: 'ANGULAR.JS',
  reference: 'angular',
  image: '/assets/images/tic-tac-toe.png'
},
{
  name: 'Escrow',
  language: 'RUBY ON RAILS',
  reference: 'ruby',
  image: '/assets/images/escrow.png'
},
{
  name: 'Feed.Me',
  language: 'RUBY ON RAILS',
  reference: 'ruby',
  image: '/assets/images/feed-me.png'
},
{
  name: 'Gone-In',
  language: 'NODE.JS',
  reference: 'node',
  image: '/assets/images/gone-in.png'
}
];

Here is what my Mongoose model currently looks like. I'm having trouble defining the schema. These attributes are all the different pieces of content I want to load into my website. The attribute that corresponds to the array above is 'projects'. This attribute, along with most others, needs to be an array of objects. I'm not seeing a clear attribute type on Mongoose's official documentation for arrays of objects. I don't feel like [Schema.Types.Mixed] is really the correct answer. What is?

var mongoose = require('mongoose');
var Schema = mongoose.Schema;

var contentSchema = new Schema({
  aboutParagraphs: [String],
  socialMediaProfiles: [Schema.Types.Mixed],
  skills: [Schema.Types.Mixed],
  languages: [Schema.Types.Mixed],
  projects: [Schema.Types.Mixed],
  schools: [Schema.Types.Mixed],
  companies: [Schema.Types.Mixed]
})

Upvotes: 0

Views: 129

Answers (1)

JohnnyHK
JohnnyHK

Reputation: 311865

You define the structure of embedded sub-documents using their own schema.

So in this case you can create:

var projectSchema = new Schema({
    name: String,
    language: String,
    reference: String,
    image: String
});

And then use the schema as the type when you define your projects array:

var contentSchema = new Schema({
    aboutParagraphs: [String],
    ...
    projects: [projectSchema],
    ...
});

Note that you can also declare the sub-document schema implicitly, right in the parent schema:

var contentSchema = new Schema({
    aboutParagraphs: [String],
    ...
    projects: [{
        name: String,
        language: String,
        reference: String,
        image: String
    }],
    ...
});

Upvotes: 2

Related Questions