I have the following Schema:
const mongoose = require('mongoose');
const Schema = mongoose.Schema;
// Create Schema
const StatsSchema = new Schema({
CustomStat: {
type: [{ Name: { type: String }, Value: { type: String }, Sport: { type: String } }],
default: [{ Name: "Position", Sport: "Football" },
{ Name: "Age", Sport: "Football" },
{ Name: "Weight", Sport: "Football" },
{ Name: "Height", Sport: "Football" },
{ Name: "Speed", Sport: "Football" },
{ Name: "Bench", Sport: "Football" },
{ Name: "VJump", Sport: "Football" },
{ Name: "BJump", Sport: "Football"},
{ Name: "3-point Accuracy", Sport: "Basketball"}]
}
});
// Create collection and add schema
mongoose.model('stats', StatsSchema, 'stats');
What I want to do is filter the CustomStat array and call something like findOne() where the CustomStat.Sport = "Basketball".
After I do that, the only value that should be passed through is { Name: "3-point Accuracy", Sport: "Basketball"}.
I have the following code:
Stats.find({ CustomStat: { $elemMatch: { Sport: 'Basketball' } } })
.then((stats) => {
res.render('records/displayrecords',
{
stats: stats
});
});
However, that code passes every single value in the array, even including where the Sport = "Football". What I want is for "stats" to be passed that only contains an array element where the Sport is Basketall.
Thank you in advance