【问题标题】:Mongoose find nested object [duplicate]猫鼬找到嵌套对象[重复]
【发布时间】:2019-10-12 01:58:50
【问题描述】:

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

我想用 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()
    }
})

当我在邮递员中运行它时,我收到一个空白数组。

这是我正在使用的查询字符串的路由: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)

【问题讨论】:

  • 当您只搜索title 而没有嵌套播放器等时会发生什么。那么您得到任何结果吗?搜索没有正则表达式,只搜索标题,看看是否所有的填充都有效。还可以尝试删除所有 populates 并查看是否使用 regEx 等获得结果。
  • @akrion 当我使用正则表达式搜索title 时,我收到了正确的结果,并且所有填充都有效。没有正则表达式,我收到一个空数组。尝试使用正则表达式删除填充,并收到一个空数组。每当我收到一个空数组时,邮递员都会告诉我我有一个200 OK。感谢您的帮助,我很坚持这个。

标签: node.js mongodb mongoose


【解决方案1】:

由于您使用的是populate,请尝试使用match 阶段:

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()
    }
})

可以在here找到这方面的文档

【讨论】:

  • 谢谢,这行得通。知道为什么点符号不起作用吗?
  • 你不能像这样在顶层查询,因为数据还不是populated。这就是比赛阶段存在的原因。
  • 我仔细看了看,这实际上是行不通的。它正在获取最初的发现,但随后忽略了玩家名称上的匹配。
猜你喜欢
  • 1970-01-01
  • 2019-04-25
  • 1970-01-01
  • 2018-04-22
  • 2014-07-26
  • 2014-07-28
  • 1970-01-01
  • 1970-01-01
  • 2017-10-05
相关资源
最近更新 更多