Certainly! Below is a Mongoose Schema for the provided topScorerPlayer data. This schema defines the structure of the data you provided, including nested objects and arrays.
topScorerPlayer.js
javascript
const mongoose = require('mongoose');
const { Schema } = mongoose;
// Schema for Area
const AreaSchema = new Schema({
id: {
type: Number,
required: true
},
name: {
type: String,
required: true
},
code: {
type: String,
required: true
},
flag: {
type: String, // URL to the flag image
required: true
}
}, { _id: false });
// Schema for Competition
const CompetitionSchema = new Schema({
id: {
type: Number,
required: true
},
name: {
type: String,
required: true
},
code: {
type: String,
required: true
},
type: {
type: String,
enum: ['LEAGUE', 'CUP'], // Assuming only these two types
required: true
},
emblem: {
type: String, // URL to the emblem image
required: true
}
}, { _id: false });
// Schema for Contract
const ContractSchema = new Schema({
start: {
type: String, // You can use Date type if exact dates are available
required: true
},
until: {
type: String, // Same as above regarding Date type
required: true
}
}, { _id: false });
// Schema for Current Team
const CurrentTeamSchema = new Schema({
area: {
type: AreaSchema,
required: true
},
id: {
type: Number,
required: true
},
name: {
type: String,
required: true
},
shortName: {
type: String,
required: true
},
tla: { // Three-Letter Acronym
type: String,
required: true
},
crest: {
type: String, // URL to the crest image
required: true
},
address: {
type: String,
required: true
},
website: {
type: String, // URL to the website
required: true
},
founded: {
type: Number,
required: true
},
clubColors: {
type: String,
required: true
},
venue: {
type: String,
required: true
},
runningCompetitions: {
type: [CompetitionSchema],
default: []
},
contract: {
type: ContractSchema,
required: true
}
}, { _id: false });
// Main Schema for Top Scorer Player
const TopScorerPlayerSchema = new Schema({
id: {
type: Number,
unique: true,
required: true
},
name: {
type: String,
required: true
},
firstName: {
type: String,
required: true
},
lastName: {
type: String,
required: true
},
dateOfBirth: {
type: Date,
required: true
},
nationality: {
type: String,
required: true
},
position: {
type: String,
enum: ['Centre-Forward', 'Goalkeeper', 'Defender', 'Midfielder', 'Forward'], // Add other positions as needed
required: true
},
shirtNumber: {
type: Number,
required: true
},
lastUpdated: {
type: Date,
default: Date.now
},
currentTeam: {
type: CurrentTeamSchema,
required: true
}
}, {
timestamps: true // Adds createdAt and updatedAt timestamps
});
// Create and export the model
module.exports = mongoose.model('TopScorerPlayer', TopScorerPlayerSchema);
Explanation
Dependencies:
Ensure you have Mongoose installed in your project. You can install it using:
bash
npm install mongoose
Schema Breakdown:
AreaSchema: Represents the geographical area related to the team (e.g., country).
CompetitionSchema: Represents each competition the team is running in.
ContractSchema: Details about the player's contract duration.
CurrentTeamSchema: Contains information about the player's current team, including nested schemas for area, competitions, and contract.
TopScorerPlayerSchema: The main schema representing the player, incorporating all the nested schemas.
Field Types:
String: For textual data.
Number: For numerical data.
Date: For date fields. Note that dateOfBirth and lastUpdated are stored as JavaScript Date objects.
Enum: Used for fields with a limited set of values, such as position and type.
URLs: Stored as strings. You might want to add validation using regex if necessary.
Options:
{ _id: false }: Prevents Mongoose from creating an _id field for sub-schemas.
timestamps: true: Automatically adds createdAt and updatedAt fields to the main schema.
Model Export:
The schema is compiled into a Mongoose model named 'TopScorerPlayer' which you can use to interact with the corresponding collection in your MongoDB database.
Usage Example
Here's how you might use the TopScorerPlayer model in your application: