node.js - 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
来查找subCategory
id 或subSubCategory
id 但它显示不存在
const newProduct = await theProduct.save();
const ss = await categorySchema.updateMany(
{
_id: newProduct.category, //im sending a category Id = ' four '
},
{ $push: { productId: newProduct._id }}
);
该代码适用于electro
类别但不是cuisine
或four