I know that there are similar questions to this one, but the answers to those have not yielded the correct result.
I want to query a nested object with mongoose find. This is what I currently have setup:
reportRoutes.route('/:id').get(async (req, res) => {
try{
let id = req.params.id
let author = req.params.author
let regex = new RegExp( id, 'i')
const report = await Report.find({title: regex, 'player.player_name': "James Harden" })
.populate({path: 'like'})
.populate({
path: 'player',
populate: [{ path: 'team' },
{
path: 'team',
populate: {
path: 'league'
}
}
]
})
res.json(report)
} catch (e) {
res.status(500).send()
}
})
When I run this in postman, I receive a blank array.
This is the route that query string that I'm using: localhost:4000/reports/harden
This is the schema for Report:
const mongoose = require('mongoose')
const Schema = mongoose.Schema
let Report = new Schema({
title: {
type: String
},
summary: {
type: String
},
analysis: {
type: String
},
source_title: {
type: String
},
source_link: {
type: String
},
author: {
type: String
},
like: [{
type: mongoose.Schema.Types.ObjectId,
ref: 'Like'
}],
player: {
type: mongoose.Schema.Types.ObjectId,
required: true,
ref: 'Player'
}
}, { timestamps: true })
module.exports = mongoose.model('Report', Report)
And this is the schema for player:
const mongoose = require('mongoose')
const Schema = mongoose.Schema
let Player = new Schema({
player_name: {
type: String
},
player_position: {
type: String
},
team: {
type: mongoose.Schema.Types.ObjectId,
required: true,
ref: 'Team'
}
}, { timestamps: true })
module.exports = mongoose.model('Player', Player)