Node.js Mongoose查找嵌套对象

Node.js Mongoose查找嵌套对象,node.js,mongodb,mongoose,Node.js,Mongodb,Mongoose,我知道也有类似的问题,但这些问题的答案并没有产生正确的结果 我想用mongoose find查询嵌套对象。这是我目前的设置: reportRoutes.route('/:id').get(async (req, res) => { try{ let id = req.params.id let author = req.params.author let regex = new RegExp( id, 'i') con

我知道也有类似的问题,但这些问题的答案并没有产生正确的结果

我想用mongoose find查询嵌套对象。这是我目前的设置:

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()
    }
})
当我在postman中运行此命令时,我收到一个空白数组

这是我正在使用的查询字符串的路径:
localhost:4000/reports/harden

这是报表的架构:

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)
这是播放器的模式:

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)

由于您正在使用
填充
尝试使用
匹配
阶段:

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 })
        .populate({path: 'like'})
        .populate({
           path: 'player',
           match: { 'player_name': 'James Harden'},  // <-- match here
           populate: [{ path: 'team' },
           {
             path: 'team',
             populate: {
             path: 'league'
           }
         }]
        })
        res.json(report)
    }  catch (e) {
        res.status(500).send()
    }
})
reportRoutes.route('/:id').get(异步(req,res)=>{
试一试{
let id=req.params.id
让author=req.params.author
让regex=newregexp(id'i')
const report=wait report.find({title:regex})
.populate({path:'like'})
.填充({
路径:'玩家',

比赛:{“球员姓名”:“詹姆斯·哈登”},//如果只搜索
title
而不搜索嵌套的播放器等,会发生什么情况?然后会得到任何结果吗?只搜索title而不搜索regEx,看看所有填充是否都有效。另外,尝试删除所有
populate
并查看是否使用regEx等获得结果。@akrion当我使用rege搜索
title
x、 我收到了正确的结果,所有的填充都正常工作。没有正则表达式,我收到了一个空数组。试图用正则表达式删除填充,但收到了一个空数组。每当我收到一个空数组时,邮递员告诉我我有一个
200 OK
。谢谢你的帮助,我在这个问题上非常困惑。谢谢,这是有效的。知道为什么会有点吗表示法不起作用?你不能像这样从上到下查询,因为数据还没有填充。
这就是为什么会有匹配阶段。我仔细看了看,这实际上不起作用。它正在获取初始查找,但随后忽略了玩家名上的匹配。