如何在猫鼬的嵌套对象中查询嵌套数组的ID?

问题描述

这是我的经文模式

{
    "verses": [
        {
            "id": "111","happy": [
                {
                    "id": "112","versePath": "Proverbs 15:13","content": "A glad heart makes a cheerful face,but by sorrow of heart the spirit is crushed"
                }
            ],sad: [
                {
                    id: 113,versePath: "text",content: "text"
                }
            ]
        }
    ]
}

我想查询以id查找每个对象。我尝试使用elemmatch进行此操作,但是没有用。

const verse = await Verses
    .find({ _id: id })
    .elemmatch("empathy",{ _id: req.params.id,});

我也尝试使用聚合,但是它也不起作用。

const verse = await Verses.aggregate([
    { "$unwind": "$verses" },{ "$match": { "verses._id": req.params.id } }
])

不起作用,我的意思是两次尝试都返回了一个空数组或整个json对象。 我希望我的结果返回每个嵌套对象 ID:112, versePath

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)