gpt4 book ai didi

node.js - 如何在nodejs中使用mongoose选择mongodb中的深层嵌套数据

转载 作者:太空宇宙 更新时间:2023-11-04 02:08:22 24 4
gpt4 key购买 nike

我是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);
})});

最佳答案

您可以使用聚合框架。您可以获取详细信息herehere .

这段代码可以正常工作。

MyModel.aggregate([
{
"$match": {
"subCats.subCats.slug": req.params.cat
}
},
{ "$unwind": "$subCats" },
{ "$unwind": "$subCats.subCats" },
{
"$match": {
"subCats.subCats.slug": req.params.cat
}
},
{
"$project": {
"data":"$subCats.subCats"
}
}
], function (err, result) {
console.log(result);
})

这里我们首先匹配文档,通过 $match 减少其他不必要的文档,在展开 subCats 两次后,我们再次匹配参数,最后只返回内部 subCats 文档。

关于node.js - 如何在nodejs中使用mongoose选择mongodb中的深层嵌套数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43328116/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com