我是nodejs和mongoose的新手,我创建了一个类别文档,其中有类别和子类别,子类别甚至有子类别。现在我想选择级别 3 中的值,但无法成功。
这是我的类别的样子
[{
"_id": "58eb0ee37c1f817864b5b863",
"name": "Electronics",
"slug": "electronics",
"order": 1,
"__v": 0,
"subCats": [{
"name": "Mobiles",
"slug": "mobiles",
"_id": "58eb0ee37c1f817864b5b867",
"subCats": [{
"name": "Apple",
"slug": "apple",
"_id": "58eb0ee37c1f817864b5b872",
"params": ["RAM", "Storage", "Primary Camera", "Front Camera", "Battery", "Processor", "Display Size", "Resolution", "Resolution Type", "GPU", "Operating System"]
}, {
"name": "HTC",
"slug": "htc",
"_id": "58eb0ee37c1f817864b5b871",
"params": ["RAM", "Storage", "Primary Camera", "Front Camera", "Battery", "Processor", "Display Size", "Resolution", "Resolution Type", "GPU", "Operating System"]
}, {
"name": "Mi",
"slug": "mi",
"_id": "58eb0ee37c1f817864b5b870",
"params": ["RAM", "Storage", "Primary Camera", "Front Camera", "Battery", "Processor", "Display Size", "Resolution", "Resolution Type", "GPU", "Operating System"]
}, {
"name": "OPPO",
"slug": "oppo",
"_id": "58eb0ee37c1f817864b5b86f",
"params": ["RAM", "Storage", "Primary Camera", "Front Camera", "Battery", "Processor", "Display Size", "Resolution", "Resolution Type", "GPU", "Operating System"]
}, {
"name": "Samsung",
"slug": "samsung",
"_id": "58eb0ee37c1f817864b5b86e",
"params": ["RAM", "Storage", "Primary Camera", "Front Camera", "Battery", "Processor", "Display Size", "Resolution", "Resolution Type", "GPU", "Operating System"]
}, {
"name": "Lenovo",
"slug": "lenovo",
"_id": "58eb0ee37c1f817864b5b86d",
"params": ["RAM", "Storage", "Primary Camera", "Front Camera", "Battery", "Processor", "Display Size", "Resolution", "Resolution Type", "GPU", "Operating System"]
}, {
"name": "Motorola",
"slug": "motorola",
"_id": "58eb0ee37c1f817864b5b86c",
"params": ["RAM", "Storage", "Primary Camera", "Front Camera", "Battery", "Processor", "Display Size", "Resolution", "Resolution Type", "GPU", "Operating System"]
}, {
"name": "LG",
"slug": "lg",
"_id": "58eb0ee37c1f817864b5b86b",
"params": ["RAM", "Storage", "Primary Camera", "Front Camera", "Battery", "Processor", "Display Size", "Resolution", "Resolution Type", "GPU", "Operating System"]
}, {
"name": "Huawei",
"slug": "huawei",
"_id": "58eb0ee37c1f817864b5b86a",
"params": ["RAM", "Storage", "Primary Camera", "Front Camera", "Battery", "Processor", "Display Size", "Resolution", "Resolution Type", "GPU", "Operating System"]
}, {
"name": "Nokia",
"slug": "nokia",
"_id": "58eb0ee37c1f817864b5b869",
"params": ["RAM", "Storage", "Primary Camera", "Front Camera", "Battery", "Processor", "Display Size", "Resolution", "Resolution Type", "GPU", "Operating System"]
}, {
"name": "Microsoft",
"slug": "microsoft",
"_id": "58eb0ee37c1f817864b5b868",
"params": ["RAM", "Storage", "Primary Camera", "Front Camera", "Battery", "Processor", "Display Size", "Resolution", "Resolution Type", "GPU", "Operating System"]
}]
}, {
"name": "Mobile Accessories",
"slug": "mobile-accessories",
"_id": "58eb0ee37c1f817864b5b864",
"subCats": [{
"name": "Mobile Cases",
"slug": "mobile-cases",
"_id": "58eb0ee37c1f817864b5b866",
"params": ["Model", "type"]
}, {
"name": "Headphones & Headsets",
"slug": "headphones-headsets",
"_id": "58eb0ee37c1f817864b5b865",
"params": []
}]
}]
这里我想获取 Apple 类别的 _id 和名称,下面是我正在执行的获取数据的操作
router.get('/getparms/:cat', function (req, res, next){
categoriesModel.find({
"slug" : req.params.cat
} ,function(err, categories){
if (err) throw err;
res.send(categories);
})});
我是一名优秀的程序员,十分优秀!