首页 > 解决方案 > mongodb句柄嵌入sous文档更新

问题描述

嘿,below当我想更新子类别时,我遇到了这个文档的问题,mongodb 找不到给定的 id,但它适用于主类别“电子”

{
    _id: 'electro',
    name: 'electro',
    description: 'this is about electro stuf',
    subCategory: [
        {
            _id: 'cuisin',
            name: 'sub ',
            description: 'sub ',
            subSubCategory: [
                {
                    _id: 'four',
                    name: 'SUB SUB',
                    description: 'SUB SUB ',
                },
                {
                    _id: 'machine',
                    name: 'SUB SUB ',
                    description: 'SUB SUB',
                },
                {
                    _id: 'savonnerie ',
                    name: 'SUB SUB ',
                    description: 'SUB SUB',
                },
            ],
        },
        {
            _id: 'electro 2',
            name: 'sub',
            description: 'sub ',
            subSubCategory: {
                _id: 'phone',
                name: 'SUB SUB',
                description: 'SUB SUB ',
            },
        },
    ],
}

我试过这个code below来查找subCategoryid 或subSubCategoryid 但它显示不存在

        const newProduct = await theProduct.save();
        const ss = await categorySchema.updateMany(
            {
                _id: newProduct.category, //im sending a category Id = ' four '
            },
            { $push: { productId: newProduct._id }}
        );

该代码适用于electro类别但不是cuisinefour

标签: node.jsmongodbexpressmongoose

解决方案


推荐阅读