我有以下架构:
try
{
int o = 9;
A* a = new(o) A;
}
catch (int i) { }
我想做的是过滤CustomStat数组,并调用诸如findOne()之类的方法,其中CustomStat.Sport =“ Basketball”。
执行完此操作后,唯一需要传递的值是{名称:“三点准确性”,运动:“篮球”}。
我有以下代码:
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');
但是,该代码传递数组中的每个单个值,甚至包括Sport =“ Football”的地方。我想要的是传递“统计信息”,其中只包含一个数组元素,其中Sport是Basketall。
提前谢谢
答案 0 :(得分:0)
尝试这个:
Stats.find({ "CustomStat.Sport": "Basketball"},{ $elemMatch: { Sport: 'Basketball' } })
.then((stats) => {
res.render('records/displayrecords',
{
stats: stats
});
});
或访问此主题:Retrieve only the queried element in an object array in MongoDB collection