如何在nodejs

时间:2017-04-10 16:11:42

标签: node.js mongodb express mongoose

我是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": []
    }]
}]

这里我想得到_id和Apple类别的名称,以下是我正在做的获取数据

router.get('/getparms/:cat', function (req, res, next){

categoriesModel.find({
    "slug" : req.params.cat
} ,function(err, categories){
    if (err) throw err;

    res.send(categories);
})});

1 个答案:

答案 0 :(得分:0)

您可以使用聚合框架。您可以获得详细信息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减少其他unnessary文档,并且在两次解开subCats之后,我们再次匹配参数,最后我们只返回了内置的subCats文档。