繁体   English   中英

Mongoose 在 object 内的 object 数组中查找

[英]Mongoose find in array of object within object

下面是我的 mongpdb json 文档

{
        categoryId: '1',
        categoryName: 'Outdoors Equipments',
        items: [
            {
                itemId: '1',
                itemName: 'Camping Tent',
                itemDescription: 'A tent is a shelter consisting of sheets of fabric or other material draped over, attached to a frame of poles or attached to a supporting rope. While smaller tents may be free-standing or attached to the ground, large tents are usually anchored using guy ropes tied to stakes or tent pegs. First used as portable homes by nomads, tents are now more often used for recreational camping and as temporary shelters.',
                itemImage: '/images/camping-tent.jpeg',
                createdAt: DateTime.now().toLocaleString(DateTime.DATETIME_SHORT)
            },
            {
                itemId: '2',
                itemName: 'Camping Chair',
                itemDescription: 'A tent is a shelter consisting of sheets of fabric or other material draped over, attached to a frame of poles or attached to a supporting rope. While smaller tents may be free-standing or attached to the ground, large tents are usually anchored using guy ropes tied to stakes or tent pegs. First used as portable homes by nomads, tents are now more often used for recreational camping and as temporary shelters.',
                itemImage: '/images/camping-chair.jpeg',
                createdAt: DateTime.now().toLocaleString(DateTime.DATETIME_SHORT)
            },
            {
                itemId: '3',
                itemName: 'Camping Table',
                itemDescription: 'A tent is a shelter consisting of sheets of fabric or other material draped over, attached to a frame of poles or attached to a supporting rope. While smaller tents may be free-standing or attached to the ground, large tents are usually anchored using guy ropes tied to stakes or tent pegs. First used as portable homes by nomads, tents are now more often used for recreational camping and as temporary shelters.',
                itemImage: '/images/camping-table.jpeg',
                createdAt: DateTime.now().toLocaleString(DateTime.DATETIME_SHORT)
            },
        ]
    }

如何基于类别和 itemid 进行搜索,并使用 mongoose 和 nodejs express 返回 json 以及该特定项目的类别数据和项目数据

您可以为此$elemMatch 文档使用 $elemMatch 和 $ operator $ operator 文档

这是一个工作示例https://mongoplayground.net/p/GxWp1MIcTcB

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM