mongodb error: Use of undefined variable for node.js

Viewed 2587

I want to query a collection in mongo which has up to five levels of nested array. I have written the query for the data as it is seen below but I have been having a problem with the query It has been giving me the error Use of undefined variable: fourth_lev

assert: command failed: [
    ok : 0,
    errmsg : Use of undefined variable: fourth_lev,
    code : 17276,
    codeName : Location17276
] : aggregate failed

_getErrorWithCode@src/mongo/shell/utils.js:25:13
doassert@src/mongo/shell/assert.js:16:14
assert.commandWorked@src/mongo/shell/assert.js:403:5
DB.prototype._runAggregate@src/mongo/shell/db.js:260:9
DBCollection.prototype.aggregate@src/mongo/shell/collection.js:1212:12
@(shell):1:1

Basically this error is stating that the variable fourth_lev which I declared is not recognised by the condition calling it.

I have tried many things such as removing the following clause

$and: [ { $eq: [ $$fourth_lev.fourth_lev_one, crab donut] }, ]

and letting it remain

{ $eq: [ $$fourth_lev.fourth_lev_two,Chillout place ] }

but it still keeps on giving me an error.

SyntaxError: invalid property id

When I used $this variable instead of stating a variable name, the output was completely empty.

Any help would be appreciated.

If anyone is asking why this structure?. I needed a system where updating a point can be done once without having to search several places before an update is completed successfully without error. There may be several update operations and if all these arrays are converted to tiny entities then updating an item may become a very serious, tedious and problematic task.

db.getCollection('cuisine').aggregate([
{$match: {
        first_level:{
        $elemMatch:{
            "first_item":"United kingdom",
            "second_item":"Great Britian",
            "third_item":"England",
            second_level:{
            $elemMatch:{
                second_lev_one:"London",
                third_level:{
                $elemMatch:{
                    third_lev_one:"city of london",
                    fourth_level:{
                    $elemMatch:{
                        fourth_lev_one:"crab donut",
                        fourth_lev_two:"Chillout place"
                    }
                    }
                }
                }
            }
            }
        }
        }
    }},

{"$addFields":{
    "first_level":{
    "$filter":{
    "input":{
    "$map":{
        "input":"$first_level",
        "as":"firstlev",
        "in":{
            "_id":"$$firstlev._id",
            "second_level":{
            "$filter":{
            "input":{
            "$map":{
                "input":"$$firstlev.second_level",
                "as":"sec_lev",
                "in":{
                    "_id":"$$sec_lev._id",
                    "second_lev_one":"$$sec_lev.second_lev_one",
                    "third_level":{
                    "$filter":{
                    "input":{
                    "$map":{
                        "input":"$$sec_lev.third_level",
                        "as":"third_lev",
                        "in":{
                            "_id":"$$third_lev._id",
                            "third_lev_one":"$$third_lev.third_lev_one",
                            "fourth_level":{
                            "filter":{
                            "input":"$$third_lev.fourth_level",
                            "as":"fourth_lev",
                                "cond":{
                                "$and": [
                                    { "$eq": [ "$$fourth_lev.fourth_lev_one", "crab donut"] },
                                    { "$eq": [ "$$fourth_lev.fourth_lev_two","Chillout place" ] }
                                ]
                                }
                            }
                            }
                        }
                    }
                    },
                    "as":"third_lev",
                    "cond":{
                    "$and": [
                        { "$eq": [ "$$third_lev.third_lev_one", "city of london"] },
                        { "$ne": [ "$$third_lev.fourth_level",[] ] }
                    ]
                    }
                    }
                    }
                }
            }
            },
            "as":"sec_lev",
            "cond":{
            "$and": [
                { "$eq": [ "$$sec_lev.second_lev_one", "London" ] },
                { "$ne": [ "$$sec_lev.third_level",[] ] }
            ]
            }
            }
            }
        }
    }
    },
    "as":"class",
    "cond": {
    "$and": [
        { "$eq": [ "$$class.first_item", "United kingdom" ] },
        { "$eq": [ "$$class.second_item", "Great Britian" ] },
        { "$eq": [ "$$class.third_item", "England" ] },
        { "$ne": [ "$$class.second_level",[] ] }
    ]
    }
    }
    }
}},
{"$group":{
    "_id":{
        "first_item":"$first_level.first_item",
        "third_item":"$first_level.third_item",
        "second_lev_one":"$first_level.second_level.second_lev_one",
        "third_lev_one": "$first_level.second_level.third_level.third_lev_one",
        "fourth_lev_two":"$first_level.second_level.third_level.fourth_level.fourth_lev_two",
        "fourth_lev_one":"$first_level.second_level.third_level.fourth_level.fourth_lev_one",
        "total_price":"$first_level.second_level.third_level.fourth_level.price"
    },
    "Totalprice":{
        "$sum": "$first_level.second_level.third_level.fourth_level.price"
    }
}}
]);
0 Answers
Related