简体   繁体   中英

How to select deep nested data in mongodb using mongoose in nodejs

I am new to nodejs and mongoose and i have created a categories document, which have categories and sub categories and sub categories even has sub categories. Now i want to select the values from level 3 and i am unable to get success.

Here is what my categories looks like

[{
"_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": []
    }]
}]

Here i want to get _id and name of Apple category and below is what i am doing to fetch the data

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

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

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

You can use Aggregation framework. You can get detailed information here and here .

This code will work fine.

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

Here we matched the document first to reduce other unnessary documents by $match and after unwinding subCats twice, we again matched the parameter and at last we returned only the innner subCats document.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM